Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursion solution in Clear category for How to Find Friends by Gamadzila
def namePresent(data,name,name2):
if name == name2: return True
for i,el in enumerate(data) :
for j in range(2):
if el[j] == name and namePresent(data[:i]+data[i+1:],el[j^1],name2):return True
return False
def check_connection(data,name1,name2):
return namePresent([el.split("-") for el in data],name1,name2)
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 11, 2014
Comments: