Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_path(stages):
- city_paths = {} #dictionary of strings to strings
- potential_start_cities = set()
- for start_city, end_city in stages:
- city_paths[start_city] = end_city
- potential_start_cities.add(start_city)
- for key, end_city in city_paths.iteritems():
- if end_city in potential_start_cities:
- potential_start_cities.remove(end_city)
- assert len(potential_start_cities) == 1
- current_city = potential_start_cities.pop()
- paths = [current_city]
- while len(city_paths) > 0:
- paths.append(city_paths[current_city])
- start_city_temp = current_city
- current_city = city_paths[current_city]
- city_paths.pop(start_city_temp)
- return paths
- stages = [('Bergerac','Pau'), ('Nice','Montpelier'), ('Pau','Paris'), ('Marseille','Nice'), ('Montpelier','Bergerac'), ('Chicago', 'NYC')]
- print find_path(stages)
Add Comment
Please, Sign In to add comment