Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.32 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. def create_data_structure(passage):
  26.     network = {}
  27.     sentence_list = passage.split('.')                                                  # Splits input at each period.
  28.     play_phrase = 'likes to play '
  29.     connection_phrase = 'is connected to '
  30.     for item in sentence_list:
  31.         if item:  # Checks for empty string.
  32.             network[item[0:item.find(" ")]] = {'connections': [], 'games': []}  # First word of each sentence,
  33.                                                                                 # a name, becomes a key.
  34.         if 'likes to play' in item:                                             # Key given list of connections, games.
  35.             games_list = []
  36.             user_games = item[item.find(play_phrase) + len(play_phrase):]       # play_phrase indicates sentence
  37.             junk_list = user_games.split(',')                                   # contains games.
  38.             for e in junk_list:
  39.                 if e[0] == ' ':                                                 # Removes whitespace in first
  40.                     games_list.append(e[1:])                                    # character, adds string to user's
  41.                 else:                                                           # list of games.
  42.                     games_list.append(e)
  43.             network[item[0:item.find(" ")]]['games'] = games_list
  44.  
  45.     for item in sentence_list:
  46.         if 'connected to' in item:
  47.             connections_list = []
  48.             user_connections = item[item.find(connection_phrase) + len(connection_phrase):]
  49.             crap_list = user_connections.split(',')
  50.             for j in crap_list:                                                 # Removes whitespace in first
  51.                 if j[0] == ' ':                                                 # character, adds user to connections,
  52.                     connections_list.append(j[1:])
  53.                 else:
  54.                     connections_list.append(j)
  55.             network[item[0:item.find(" ")]]['connections'] = connections_list
  56.     return network
  57.  
  58.  
  59. def get_connections(network, user):                    # Finds a user's connections and returns them in a list.
  60.     if user in network:
  61.         return network[user]['connections']
  62.  
  63.  
  64. def get_games_liked(network, user):                     # Returns a user's games in a list.
  65.     if user not in network:
  66.         return None
  67.     elif network[user]['games']:
  68.         return network[user]['games']
  69.     else:
  70.         return []
  71.  
  72.  
  73. def add_connection(network, user_A, user_B):                # Adds a new connection to an existing user_A
  74.     if user_A and user_B in network:
  75.         if user_B not in network[user_A]['connections']:
  76.             network[user_A]['connections'].append(user_B)
  77.     return network
  78.  
  79.  
  80. def add_new_user(network, user, games):             # Adds a new user and their games to the network
  81.     if user not in network:
  82.         network[user] = {'connections': [], 'games': []}
  83.         network[user]['games'] = games
  84.     return network
  85.  
  86.  
  87. def get_secondary_connections(network, user):           # Finds connections of connections and return them in a list.
  88.     first_list = []                                     # If user not in list, returns None.
  89.     true_list = []                                      # Returns empty list if user has no secondary connections.
  90.     if user in network:
  91.         for item in network[user]['connections']:
  92.             first_list += get_connections(network, item)
  93.         for e in first_list:
  94.             if e not in true_list:
  95.                 true_list.append(e)
  96.         if len(true_list) >= 0:
  97.             return true_list
  98.     return None
  99.  
  100.  
  101. def connections_in_common(network, user_A, user_B):     # Returns number of connections in common between two users.
  102.     common_conn = 0
  103.     if user_A in network:
  104.         if user_B in network:
  105.             user_A_conn, user_B_conn = network[user_A]['connections'], network[user_B]['connections']
  106.             for item in user_A_conn:
  107.                 if item in user_B_conn:
  108.                     common_conn = common_conn + 1
  109.     else:
  110.         return False
  111.     return common_conn
  112.  
  113.  
  114. def path_to_friend(network, user_A, user_B, traveled_nodes=None):   # Finds a path between two users and returns
  115.     if traveled_nodes is None:                                      # a list of users in that path.
  116.         traveled_nodes = []
  117.     if user_A in network and user_B in network:
  118.         print 'user considered is ' + user_A
  119.  
  120.         traveled_nodes = traveled_nodes + [user_A]
  121.  
  122.         if user_B in network[user_A]['connections']:                # Checks if user_B is in user_A's connections
  123.             traveled_nodes.append(user_B)
  124.             return traveled_nodes
  125.  
  126.         for person in network[user_A]['connections']:
  127.             print '     connections to be considered are ' + person
  128.  
  129.             if person not in traveled_nodes:
  130.                 print '     next is ' + person
  131.  
  132.                 unknown_path = path_to_friend(network, person, user_B, traveled_nodes)
  133.                 if unknown_path:                           # If there is a connection to user_B, reiterate procedure
  134.                     return unknown_path                    # with variable 'person' as new user_A
  135.  
  136.         return None                                        # If there is no connection, or users not in network,
  137.                                                            # 'None' is returned
  138.  
  139.  
  140. # Make your own procedure
  141.  
  142. def add_new_game(network, user, games):      # Adds a new game to a user's list of games.
  143.     if user in network:
  144.         network[user]['games'].append(games)
  145.     return network
  146.  
  147.  
  148.  
  149.  
  150. G = [[0, 0, 0, 1, 0, 0, 1, 0, 1, 0],
  151.      [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
  152.      [1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
  153.      [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  154.      [1, 1, 0, 1, 0, 0, 1, 0, 1, 0],
  155.      [0, 1, 0, 0, 0, 0, 1, 0, 1, 1],
  156.      [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
  157.      [0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  158.      [1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
  159.      [0, 0, 1, 1, 0, 0, 1, 1, 1, 0]]
  160.  
  161. network = create_data_structure(example_input)
  162. for i in range(len(G)):
  163.     network = add_new_user(network, str(i), [])
  164. for i in range(len(G)):
  165.     for j in range(len(G[i])):
  166.         if G[i][j] > 0:
  167.             network = add_connection(network, str(i), str(j))
  168.  
  169.  
  170. network = add_new_user(network, 'A', [])
  171. network = add_new_user(network, 'B', [])
  172. network = add_new_user(network, 'C', [])
  173. network = add_new_user(network, 'D', [])
  174. network = add_new_user(network, 'E', [])
  175. network = add_new_user(network, 'F', [])
  176. network = add_new_user(network, 'G', [])
  177. network = add_connection(network, 'A', 'B')
  178. network = add_connection(network, 'B', 'C')
  179. network = add_connection(network, 'B', 'A')
  180. network = add_connection(network, 'C', 'B')
  181. network = add_connection(network, 'C', 'D')
  182. network = add_connection(network, 'D', 'A')
  183. network = add_connection(network, 'D', 'E')
  184. network = add_connection(network, 'E', 'F')
  185. network = add_connection(network, 'E', 'G')
  186.  
  187. print path_to_friend(network, 'A', 'G')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement