Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #CS101 final project:
- #Kris Tapper; kristalrasha@comcast.net
- 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 sentence_separate(string_input):
- p_del = ' is connected to ' #persons conncted to delimiter
- g_del = ' likes to play ' #games liked delimiter
- sentences,people,games = {},[],[]
- sentence_lists = string_input.split('.') #separating the raw sentences into elements in list
- for raw_sent in sentence_lists:
- if p_del in raw_sent:
- person = raw_sent[0:raw_sent.find(p_del)] #finds person who is connecting
- people = raw_sent[raw_sent.find(p_del) + 17:] #finds people who person is connected to
- if g_del in raw_sent:
- person = raw_sent[0:raw_sent.find(g_del)] #finds person who likes
- games = raw_sent[raw_sent.find(g_del) + 15:] #finds games person likes
- sentences[person] = str(people).split(', '),str(games).split(', ')
- #takes person as key to dictionary and splits strings of connections and likes into lists of entries
- return sentences
- def create_data_structure(string_input):
- network = sentence_separate(string_input) #calls the helper function
- return network
- network = create_data_structure(example_input) #adds network to a global variable to be used by the rest of the program
- def get_connections(network, user): #returns connections THIS user has (outgoing connections only?)
- if user not in network:
- return None
- if network[user][0] == []:
- return []
- else:
- return network[user][0]
- def get_games_liked(network,user): #returns games liked by this user
- if user not in network:
- return None
- if network[user][1] == []:
- return []
- else:
- return network[user][1]
- def add_connection(network, user_A, user_B):
- if user_A not in network or user_B not in network:
- return False
- if user_B in network[user_A][0]:
- return network
- else:
- network[user_A][0].append(user_B)
- return network
- #returns updated network for any new connections by appending the target user's connection list
- def add_new_user(network, user, games):
- if user in network:
- return network
- else:
- network[user] = [], games
- return network
- #adds user if not already in network, with preset games liked
- def get_secondary_connections(network, user):
- secondary_connections = []
- if user not in network:
- return None
- if network[user][0] == []:
- return []
- for connection in network[user][0]: #loops through all connected users to target user
- for secondary in network[connection][0]: #loops through all connected users' connected users
- if secondary not in secondary_connections:
- secondary_connections.append(secondary) #appends new list with secondary connections, ignoring duplicates
- return secondary_connections
- def path_finder(path_graph, user_A, user_B, friend_path):
- friend_path = friend_path + [user_A] #in the recursion, the intial user_A is the next 'node' to be tested
- if user_A == user_B: # if they're the same, return the path as-is, base case
- return friend_path
- if user_A not in path_graph: # if the user is not in the path, do nothing
- return None
- for user in path_graph[user_A]: #cycles throgh all users in the connections of user_A
- if user not in friend_path: #prevents duplicate connections
- final_path = path_finder(path_graph, user, user_B, friend_path) #recursive call sets new initial user to the current user selected from the previous user's connection
- if final_path: #returns the path so long as it has connections in it, otherwise ends the loop
- return final_path
- return None #ends the recursion if the final path from the recursion is empty (ie no path to end user from start user)
- def path_to_friend(network, user_A, user_B):
- path_graph = {} #initializes the graph of all paths from each user
- friend_path = [] #a blank starting path for the recursive definition to use to build up
- for user_key in network:
- path_graph[user_key] = network[user_key][0] #builds new dictionary of JUST connections for each user
- path_to_friend = path_finder(path_graph, user_A, user_B, friend_path) #calls the above recursive definition to find the path of each user
- return path_to_friend
- def people_who_play_game(network, game): #function returns list of all users that play selected game
- users = []
- for user in network:
- if game in get_games_liked(network, user): #adds user to list if game is in their liked games list
- users.append(user)
- return users #returns list of users
- def games_network(network): # function returns new dictionary separate from the network using game as key
- game_net,users = {},[]
- for user in network:
- for game in get_games_liked(network, user): #cycles through users' games lists
- if game not in game_net:
- game_net[game] = people_who_play_game(network, game)
- #asigns games as key value, then takes game key and\
- #uses previous function call to assign value being the list of people who play that game
- return game_net
- #net = create_data_structure(example_input)
- #print net
- #print path_to_friend(net, "John", "Ollie")
- #print get_connections(net, "Debra")
- #print add_new_user(net, "Debra", [])
- #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
- #print get_connections(net, "Mercedes")
- #print get_games_liked(net, "John")
- #print add_connection(net, "John", "Freda")
- #print get_secondary_connections(net, "Mercedes")
- #print connections_in_common(net, "Mercedes", "John")
- #print example_input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement