Guest User

Untitled

a guest
Jun 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. def find_path(stages):
  2.  
  3. city_paths = {} #dictionary of strings to strings
  4. potential_start_cities = set()
  5. for start_city, end_city in stages:
  6. city_paths[start_city] = end_city
  7. potential_start_cities.add(start_city)
  8.  
  9. for key, end_city in city_paths.iteritems():
  10. if end_city in potential_start_cities:
  11. potential_start_cities.remove(end_city)
  12.  
  13. assert len(potential_start_cities) == 1
  14.  
  15. current_city = potential_start_cities.pop()
  16.  
  17. paths = [current_city]
  18.  
  19. while len(city_paths) > 0:
  20. paths.append(city_paths[current_city])
  21. start_city_temp = current_city
  22. current_city = city_paths[current_city]
  23. city_paths.pop(start_city_temp)
  24.  
  25. return paths
  26.  
  27.  
  28.  
  29. stages = [('Bergerac','Pau'), ('Nice','Montpelier'), ('Pau','Paris'), ('Marseille','Nice'), ('Montpelier','Bergerac'), ('Chicago', 'NYC')]
  30.  
  31. print find_path(stages)
Add Comment
Please, Sign In to add comment