A tie rule that reads the route from the front needs the table filled from the other end
On a graph, a tie rule can decide the direction of the whole table. Add one stretch to the park, ("dell", "falls", 4). Now two routes score 12. Say the rule is the one the practice problem uses: return the route whose list of junctions comes first in Python's list order.
Predict
Two routes score 12: top, ash, cliff, falls and top, birch, dell, falls. Which list comes first in Python's order?
Route
Score
['top', 'ash', 'cliff', 'falls']
12
['top', 'birch', 'dell', 'falls']
12
They first differ at the second junction, and 'ash' < 'birch'.
The forward pass with parents returns ['top', 'birch', 'dell', 'falls']. Kahn's order reaches dell before cliff, so dell gives falls its 12 first, and a later tie doesn't replace a parent.
The rule reads the route from the front: the first junction where two routes differ decides. A walk back from the exit makes the last choice first, so it can't apply that rule. Turn the table around instead:
Fill down[j], the best total from j to an exit. It's the best_down you wrote earlier.
Walk forward from the start. At each junction, the stretches worth taking are the ones with score + down[lower] == down[j].
Of those, take the smallest name.
On the park with the extra stretch, down is top 12, birch 10, ash 9, cliff 5, dell 4:
At
Stretches and score + down[lower]
Worth taking
Go to
top
birch 2 + 10 = 12, ash 3 + 9 = 12
both
ash
ash
cliff 4 + 5 = 9
cliff
cliff
cliff
falls 5 + 0 = 5, end 1 + 0 = 1
falls
falls
falls is an exit and down["falls"] is 0, so riding on can't add anything, and the walk stops: ['top', 'ash', 'cliff', 'falls']. Stopping wins over riding on when both are worth the same. A list that stops where another keeps going comes first in list order.
In the editor, finish the walk in best_route(below, order, start, exits). It prints the best route on the park, then on the park with the tie.
Pick an answer above to see the rest
Instructions
1
Finish the walk in best_route(below, order, start, exits), with down filled for you. Return the junctions of a best route as a list, start first.
2
Stop at an exit whose down is 0. Otherwise go to the smallest-named junction among the stretches with score + down[lower] == down[j].