from collections import deque
def person_check(name):
return name[-1] == 'm'
graph = {}
graph["you"] = ["Angela","Jack","Amy"]
graph["Jack"] = ["Adam","Evan"]
graph["Angela"] = ["Evan"]
graph["Amy"] = ["Justin","Bradon"]
graph["Adam"] = []
graph["Evan"] = []
graph["Justin"] = []
graph["Bradon"] = []
def search(name):
search_queue = deque()
search_queue += graph[name]
searched = []
while search_queue:
person = search_queue.popleft()
if not person in searched:
if person_check(person):
print person + " has an Apple!"
return True
else:
search_queue += graph[person]
searched.append(person)
return False
search("you")
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVxdWUKCmRlZiBwZXJzb25fY2hlY2sobmFtZSk6CgkgIHJldHVybiBuYW1lWy0xXSA9PSAnbScKCSAgCmdyYXBoID0ge30KZ3JhcGhbInlvdSJdID0gWyJBbmdlbGEiLCJKYWNrIiwiQW15Il0KZ3JhcGhbIkphY2siXSA9IFsiQWRhbSIsIkV2YW4iXQpncmFwaFsiQW5nZWxhIl0gPSBbIkV2YW4iXQpncmFwaFsiQW15Il0gPSBbIkp1c3RpbiIsIkJyYWRvbiJdCmdyYXBoWyJBZGFtIl0gPSBbXQpncmFwaFsiRXZhbiJdID0gW10KZ3JhcGhbIkp1c3RpbiJdID0gW10KZ3JhcGhbIkJyYWRvbiJdID0gW10KCgpkZWYgc2VhcmNoKG5hbWUpOgoJc2VhcmNoX3F1ZXVlID0gZGVxdWUoKQoJc2VhcmNoX3F1ZXVlICs9IGdyYXBoW25hbWVdCglzZWFyY2hlZCA9IFtdCgl3aGlsZSBzZWFyY2hfcXVldWU6CgkJcGVyc29uID0gc2VhcmNoX3F1ZXVlLnBvcGxlZnQoKQoJCWlmIG5vdCBwZXJzb24gaW4gc2VhcmNoZWQ6CgkJCWlmIHBlcnNvbl9jaGVjayhwZXJzb24pOgoJCQkJcHJpbnQgcGVyc29uICsgIiBoYXMgYW4gQXBwbGUhIgoJCQkJcmV0dXJuIFRydWUKCQkJZWxzZToKCQkJCXNlYXJjaF9xdWV1ZSArPSBncmFwaFtwZXJzb25dCgkJCQlzZWFyY2hlZC5hcHBlbmQocGVyc29uKQoJcmV0dXJuIEZhbHNlCnNlYXJjaCgieW91Iik=