fork download
  1. from collections import deque
  2.  
  3. def person_check(name):
  4. return name[-1] == 'm'
  5.  
  6. graph = {}
  7. graph["you"] = ["Angela","Jack","Amy"]
  8. graph["Jack"] = ["Adam","Evan"]
  9. graph["Angela"] = ["Evan"]
  10. graph["Amy"] = ["Justin","Bradon"]
  11. graph["Adam"] = []
  12. graph["Evan"] = []
  13. graph["Justin"] = []
  14. graph["Bradon"] = []
  15.  
  16.  
  17. def search(name):
  18. search_queue = deque()
  19. search_queue += graph[name]
  20. searched = []
  21. while search_queue:
  22. person = search_queue.popleft()
  23. if not person in searched:
  24. if person_check(person):
  25. print person + " has an Apple!"
  26. return True
  27. else:
  28. search_queue += graph[person]
  29. searched.append(person)
  30. return False
  31. search("you")
Success #stdin #stdout 0.05s 63568KB
stdin
Standard input is empty
stdout
Adam has an Apple!