Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Friends set solution in Clear category for How to Find Friends by hanpari
def check_connection(edges, first, last):
path = {first}
check = 0
# All points who can be acceseed from "first"
# are put in path set.
# At the end end "last" is checked if is in path set.
while(len(path) > check):
check = len(path)
# this part can be easily optimized to avoid
# iterating through edges already accepted in path set.
# I skipped this to keep my solution
# clear and easy to understand
for edge in edges:
one, two = edge.split("-")
if one in path or two in path:
path.add(one)
path.add(two)
else:
return last in path
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"scout2", "scout3") == True, "Scout Brotherhood"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"super", "scout2") == True, "Super Scout"
assert check_connection(
("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
"scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
"dr101", "sscout") == False, "I don't know any scouts."
June 28, 2014
Comments: