Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- example_input = "John is connected to Bryant, Debra, Walter.\
- John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
- Bryant is connected to Olive, Ollie, Freda, Mercedes.\
- Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
- Mercedes is connected to Walter, Robin, Bryant.\
- Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
- Olive is connected to John, Ollie.\
- Olive likes to play The Legend of Corgi, Starfleet Commander.\
- Debra is connected to Walter, Levi, Jennie, Robin.\
- Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
- Walter is connected to John, Levi, Bryant.\
- Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
- Levi is connected to Ollie, John, Walter.\
- Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
- Ollie is connected to Mercedes, Freda, Bryant.\
- Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
- Jennie is connected to Levi, John, Freda, Robin.\
- Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
- Robin is connected to Ollie.\
- Robin likes to play Call of Arms, Dwarves and Swords.\
- Freda is connected to Olive, John, Debra.\
- Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
- def create_data_structure(passage):
- network = {}
- sentence_list = passage.split('.') # Splits input at each period.
- play_phrase = 'likes to play '
- connection_phrase = 'is connected to '
- for item in sentence_list:
- if item: # Checks for empty string.
- network[item[0:item.find(" ")]] = {'connections': [], 'games': []} # First word of each sentence,
- # a name, becomes a key.
- if 'likes to play' in item: # Key given list of connections, games.
- games_list = []
- user_games = item[item.find(play_phrase) + len(play_phrase):] # play_phrase indicates sentence
- junk_list = user_games.split(',') # contains games.
- for e in junk_list:
- if e[0] == ' ': # Removes whitespace in first
- games_list.append(e[1:]) # character, adds string to user's
- else: # list of games.
- games_list.append(e)
- network[item[0:item.find(" ")]]['games'] = games_list
- for item in sentence_list:
- if 'connected to' in item:
- connections_list = []
- user_connections = item[item.find(connection_phrase) + len(connection_phrase):]
- crap_list = user_connections.split(',')
- for j in crap_list: # Removes whitespace in first
- if j[0] == ' ': # character, adds user to connections,
- connections_list.append(j[1:])
- else:
- connections_list.append(j)
- network[item[0:item.find(" ")]]['connections'] = connections_list
- return network
- def get_connections(network, user): # Finds a user's connections and returns them in a list.
- if user in network:
- return network[user]['connections']
- def get_games_liked(network, user): # Returns a user's games in a list.
- if user not in network:
- return None
- elif network[user]['games']:
- return network[user]['games']
- else:
- return []
- def add_connection(network, user_A, user_B): # Adds a new connection to an existing user_A
- if user_A and user_B in network:
- if user_B not in network[user_A]['connections']:
- network[user_A]['connections'].append(user_B)
- return network
- def add_new_user(network, user, games): # Adds a new user and their games to the network
- if user not in network:
- network[user] = {'connections': [], 'games': []}
- network[user]['games'] = games
- return network
- def get_secondary_connections(network, user): # Finds connections of connections and return them in a list.
- first_list = [] # If user not in list, returns None.
- true_list = [] # Returns empty list if user has no secondary connections.
- if user in network:
- for item in network[user]['connections']:
- first_list += get_connections(network, item)
- for e in first_list:
- if e not in true_list:
- true_list.append(e)
- if len(true_list) >= 0:
- return true_list
- return None
- def connections_in_common(network, user_A, user_B): # Returns number of connections in common between two users.
- common_conn = 0
- if user_A in network:
- if user_B in network:
- user_A_conn, user_B_conn = network[user_A]['connections'], network[user_B]['connections']
- for item in user_A_conn:
- if item in user_B_conn:
- common_conn = common_conn + 1
- else:
- return False
- return common_conn
- def path_to_friend(network, user_A, user_B, traveled_nodes=None): # Finds a path between two users and returns
- if traveled_nodes is None: # a list of users in that path.
- traveled_nodes = []
- if user_A in network and user_B in network:
- print 'user considered is ' + user_A
- traveled_nodes = traveled_nodes + [user_A]
- if user_B in network[user_A]['connections']: # Checks if user_B is in user_A's connections
- traveled_nodes.append(user_B)
- return traveled_nodes
- for person in network[user_A]['connections']:
- print ' connections to be considered are ' + person
- if person not in traveled_nodes:
- print ' next is ' + person
- unknown_path = path_to_friend(network, person, user_B, traveled_nodes)
- if unknown_path: # If there is a connection to user_B, reiterate procedure
- return unknown_path # with variable 'person' as new user_A
- return None # If there is no connection, or users not in network,
- # 'None' is returned
- # Make your own procedure
- def add_new_game(network, user, games): # Adds a new game to a user's list of games.
- if user in network:
- network[user]['games'].append(games)
- return network
- G = [[0, 0, 0, 1, 0, 0, 1, 0, 1, 0],
- [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
- [1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
- [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
- [1, 1, 0, 1, 0, 0, 1, 0, 1, 0],
- [0, 1, 0, 0, 0, 0, 1, 0, 1, 1],
- [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
- [0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 1, 0, 0, 1, 1, 1, 0]]
- network = create_data_structure(example_input)
- for i in range(len(G)):
- network = add_new_user(network, str(i), [])
- for i in range(len(G)):
- for j in range(len(G[i])):
- if G[i][j] > 0:
- network = add_connection(network, str(i), str(j))
- network = add_new_user(network, 'A', [])
- network = add_new_user(network, 'B', [])
- network = add_new_user(network, 'C', [])
- network = add_new_user(network, 'D', [])
- network = add_new_user(network, 'E', [])
- network = add_new_user(network, 'F', [])
- network = add_new_user(network, 'G', [])
- network = add_connection(network, 'A', 'B')
- network = add_connection(network, 'B', 'C')
- network = add_connection(network, 'B', 'A')
- network = add_connection(network, 'C', 'B')
- network = add_connection(network, 'C', 'D')
- network = add_connection(network, 'D', 'A')
- network = add_connection(network, 'D', 'E')
- network = add_connection(network, 'E', 'F')
- network = add_connection(network, 'E', 'G')
- print path_to_friend(network, 'A', 'G')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement