17/38
Part 4 of 7 · PathsLearn and do

Collect everything that contains a, then climb from b until you reach one of them

Two climbs find the smallest region containing two others.

def smallest_common(parent, a, b):
    above_a = set(path_from_top(parent, a))
    while b not in above_a:
        b = parent[b]
    return b


for a, b in [("Sintra", "Porto"), ("Seville", "Lisbon"), ("Catalonia", "Seville"), ("Portugal", "Cascais")]:
    print(a, "and", b, "->", smallest_common(parent, a, b))
Predict

What's the smallest region that contains both Sintra and Porto?

Pick one to see what it prints.
Pick an answer above to see the rest
Predict

Climb from Granada (depth 3) and Catalonia (depth 2) together, one step each, until they stand on the same region. Where do they meet?

Pick an answer above to see the rest