Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_connection(network, first, second):
- visited = []
- if graph(visited, network, first, second) == True:
- return True
- return False
- def graph(visited, network, first, second):
- for elem in network:
- if first in elem:
- if first in elem and elem not in visited:
- visited.append(elem)
- first = [x for x in elem.split('-') if x != first][0]
- if first == second:
- return True
- return graph(visited, network, first, second)
- 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."
Advertisement
Add Comment
Please, Sign In to add comment