Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 KB | None | 0 0
  1. example_input="John is connected to Bryant, Debra, Walter.\
  2. John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
  3. Bryant is connected to Olive, Ollie, Freda, Mercedes.\
  4. Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
  5. Mercedes is connected to Walter, Robin, Bryant.\
  6. Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
  7. Olive is connected to John, Ollie.\
  8. Olive likes to play The Legend of Corgi, Starfleet Commander.\
  9. Debra is connected to Walter, Levi, Jennie, Robin.\
  10. Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
  11. Walter is connected to John, Levi, Bryant.\
  12. Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
  13. Levi is connected to Ollie, John, Walter.\
  14. Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
  15. Ollie is connected to Mercedes, Freda, Bryant.\
  16. Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
  17. Jennie is connected to Levi, John, Freda, Robin.\
  18. Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
  19. Robin is connected to Ollie.\
  20. Robin likes to play Call of Arms, Dwarves and Swords.\
  21. Freda is connected to Olive, John, Debra.\
  22. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
  23.  
  24.  
  25.  
  26. def create_data_structure(string_input):
  27.     network = {}
  28.     lineList = string_input.split('.')
  29.     del lineList[-1]
  30.     for line in lineList:
  31.         if lineList.index(line) % 2 == 0:
  32.             name = line.split()[0]
  33.             network[name] = [line[line.find('to') + 3:].split(', ')]
  34.         else:
  35.             network[line.split()[0]].append(line[line.find('play') + 5:].split(', '))
  36.     return network
  37.        
  38. def print_network(network):
  39.     for item in network.iteritems():
  40.         print item
  41.  
  42. def get_connections(network, user):
  43.     return network[user][0]
  44.    
  45. def get_games_liked(network,user):
  46.     return network[user][1]
  47.  
  48. def add_connection(network, user_A, user_B):
  49.     if user_A not in network or user_B not in network:
  50.         return False
  51.     elif user_B in network[user_A][0]:
  52.         return network
  53.     else:
  54.         network[user_A][0].append(user_B)
  55.     return network
  56.  
  57. def add_new_user(network, user, games):
  58.     if user in network:
  59.         return network
  60.     else:
  61.         network[user] = [[], games]
  62.     return network
  63.  
  64. def get_secondary_connections(network, user):
  65.     if user not in network:
  66.         return None
  67.     secondaryFriends = []
  68.     for friend in network[user][0]:
  69.         for secondaryFriend in network[friend][0]:
  70.             if secondaryFriend not in secondaryFriends:
  71.                 secondaryFriends.append(secondaryFriend)
  72.     return secondaryFriends
  73.  
  74. def count_common_connections(network, user_A, user_B):
  75.     if user_A not in network or user_B not in network:
  76.         return False
  77.     commonConnections = []
  78.     for friendA in network[user_A][0]:
  79.         for friendB in network[user_B][0]:
  80.             if friendA == friendB and friendA not in commonConnections:
  81.                 commonConnections.append(friendA)
  82.     return commonConnections
  83.  
  84. # def find_path_to_friend(network, user_A, user_B, currentFriends=[]):
  85.     # currentFriends.append(user_A)
  86.     # if user_A == user_B:
  87.         # currentFriends.append('eymomma')
  88.         # return currentFriends
  89.     # friendsOfUser_A = network[user_A][0]
  90.     # for friend in network[user_A][0]:
  91.         # if friend in currentFriends:
  92.             # friendsOfUser_A.remove(friend)
  93.     # if friendsOfUser_A == []:
  94.         # pass
  95.     # for friend in friendsOfUser_A:
  96.         # print 'user_A:', user_A
  97.         # print 'user_B:', user_B
  98.         # print 'friend in currentFriends:', friend
  99.         # print 'friendsOfUser_A:', friendsOfUser_A
  100.         # return find_path_to_friend(network, friend, user_B, currentFriends)
  101. #
  102. # So..this didn't work
  103. # maybe you could find a way to not use a for loop but instead use list splitting...
  104. # the 'None' case would trace all the way back to the original set of friends, using
  105. #   'currentFriends' to loop throught he recursion
  106.  
  107. def find_path_to_friend(network, user_A, user_B, currentFriends=None):
  108.     if user_A not in network or user_B not in network:
  109.         return None
  110.     if currentFriends is None:
  111.         currentFriends = []
  112.     currentFriends.append(user_A)
  113.     if user_A == user_B:
  114.         return currentFriends
  115.     friendsOfUser_A = network[user_A][0]
  116.     for friend in network[user_A][0]:
  117.         if friend in currentFriends:
  118.             friendsOfUser_A.remove(friend)
  119.     if friendsOfUser_A == [] and user_A == currentFriends[0]:
  120.         return None
  121.     elif friendsOfUser_A == []:
  122.         return find_path_to_friend(network, currentFriends[currentFriends.index(user_A) - 1], user_B, currentFriends)
  123.     else:
  124.         for friend in friendsOfUser_A:
  125.             return find_path_to_friend(network, friend, user_B, currentFriends)
  126.  
  127.  
  128. network = create_data_structure(example_input)
  129. print find_path_to_friend(network, 'John', 'Jennie')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement