Guest User

Untitled

a guest
May 28th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. def check_connection(network, first, second):
  2.     visited = []
  3.     if graph(visited, network, first, second) == True:
  4.         return True
  5.     return False
  6.  
  7.  
  8. def graph(visited, network, first, second):
  9.     for elem in network:
  10.         if first in elem:
  11.             if first in elem and elem not in visited:
  12.                 visited.append(elem)
  13.                 first = [x for x in elem.split('-') if x != first][0]
  14.                 if first == second:
  15.                     return True
  16.                 return graph(visited, network, first, second)
  17.                
  18.    
  19.    
  20.    
  21.    
  22.    
  23. if __name__ == '__main__':
  24.     #These "asserts" using only for self-checking and not necessary for auto-testing
  25.     assert check_connection(
  26.         ("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
  27.          "scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
  28.         "scout2", "scout3") == True, "Scout Brotherhood"
  29.     assert check_connection(
  30.         ("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
  31.          "scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
  32.         "super", "scout2") == True, "Super Scout"
  33.     assert check_connection(
  34.         ("dr101-mr99", "mr99-out00", "dr101-out00", "scout1-scout2",
  35.          "scout3-scout1", "scout1-scout4", "scout4-sscout", "sscout-super"),
  36.         "dr101", "sscout") == False, "I don't know any scouts."
Advertisement
Add Comment
Please, Sign In to add comment