Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1.  
  2. #CS101 final project:
  3.  
  4. #Kris Tapper; kristalrasha@comcast.net
  5.  
  6.  
  7. example_input="John is connected to Bryant, Debra, Walter.\
  8. John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
  9. Bryant is connected to Olive, Ollie, Freda, Mercedes.\
  10. Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
  11. Mercedes is connected to Walter, Robin, Bryant.\
  12. Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
  13. Olive is connected to John, Ollie.\
  14. Olive likes to play The Legend of Corgi, Starfleet Commander.\
  15. Debra is connected to Walter, Levi, Jennie, Robin.\
  16. Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
  17. Walter is connected to John, Levi, Bryant.\
  18. Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
  19. Levi is connected to Ollie, John, Walter.\
  20. Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
  21. Ollie is connected to Mercedes, Freda, Bryant.\
  22. Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
  23. Jennie is connected to Levi, John, Freda, Robin.\
  24. Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
  25. Robin is connected to Ollie.\
  26. Robin likes to play Call of Arms, Dwarves and Swords.\
  27. Freda is connected to Olive, John, Debra.\
  28. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
  29.  
  30. def sentence_separate(string_input):
  31. p_del = ' is connected to ' #persons conncted to delimiter
  32. g_del = ' likes to play ' #games liked delimiter
  33. sentences,people,games = {},[],[]
  34. sentence_lists = string_input.split('.') #separating the raw sentences into elements in list
  35. for raw_sent in sentence_lists:
  36. if p_del in raw_sent:
  37. person = raw_sent[0:raw_sent.find(p_del)] #finds person who is connecting
  38. people = raw_sent[raw_sent.find(p_del) + 17:] #finds people who person is connected to
  39. if g_del in raw_sent:
  40. person = raw_sent[0:raw_sent.find(g_del)] #finds person who likes
  41. games = raw_sent[raw_sent.find(g_del) + 15:] #finds games person likes
  42. sentences[person] = str(people).split(', '),str(games).split(', ')
  43. #takes person as key to dictionary and splits strings of connections and likes into lists of entries
  44. return sentences
  45.  
  46. def create_data_structure(string_input):
  47. network = sentence_separate(string_input) #calls the helper function
  48. return network
  49.  
  50. network = create_data_structure(example_input) #adds network to a global variable to be used by the rest of the program
  51.  
  52. def get_connections(network, user): #returns connections THIS user has (outgoing connections only?)
  53. if user not in network:
  54. return None
  55. if network[user][0] == []:
  56. return []
  57. else:
  58. return network[user][0]
  59.  
  60. def get_games_liked(network,user): #returns games liked by this user
  61. if user not in network:
  62. return None
  63. if network[user][1] == []:
  64. return []
  65. else:
  66. return network[user][1]
  67.  
  68. def add_connection(network, user_A, user_B):
  69. if user_A not in network or user_B not in network:
  70. return False
  71. if user_B in network[user_A][0]:
  72. return network
  73. else:
  74. network[user_A][0].append(user_B)
  75. return network
  76. #returns updated network for any new connections by appending the target user's connection list
  77.  
  78. def add_new_user(network, user, games):
  79. if user in network:
  80. return network
  81. else:
  82. network[user] = [], games
  83. return network
  84. #adds user if not already in network, with preset games liked
  85.  
  86. def get_secondary_connections(network, user):
  87. secondary_connections = []
  88. if user not in network:
  89. return None
  90. if network[user][0] == []:
  91. return []
  92. for connection in network[user][0]: #loops through all connected users to target user
  93. for secondary in network[connection][0]: #loops through all connected users' connected users
  94. if secondary not in secondary_connections:
  95. secondary_connections.append(secondary) #appends new list with secondary connections, ignoring duplicates
  96. return secondary_connections
  97.  
  98. def path_finder(path_graph, user_A, user_B, friend_path):
  99. friend_path = friend_path + [user_A] #in the recursion, the intial user_A is the next 'node' to be tested
  100. if user_A == user_B: # if they're the same, return the path as-is, base case
  101. return friend_path
  102. if user_A not in path_graph: # if the user is not in the path, do nothing
  103. return None
  104. for user in path_graph[user_A]: #cycles throgh all users in the connections of user_A
  105. if user not in friend_path: #prevents duplicate connections
  106. 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
  107. if final_path: #returns the path so long as it has connections in it, otherwise ends the loop
  108. return final_path
  109. return None #ends the recursion if the final path from the recursion is empty (ie no path to end user from start user)
  110.  
  111. def path_to_friend(network, user_A, user_B):
  112. path_graph = {} #initializes the graph of all paths from each user
  113. friend_path = [] #a blank starting path for the recursive definition to use to build up
  114. for user_key in network:
  115. path_graph[user_key] = network[user_key][0] #builds new dictionary of JUST connections for each user
  116. 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
  117. return path_to_friend
  118.  
  119. def people_who_play_game(network, game): #function returns list of all users that play selected game
  120. users = []
  121. for user in network:
  122. if game in get_games_liked(network, user): #adds user to list if game is in their liked games list
  123. users.append(user)
  124. return users #returns list of users
  125.  
  126. def games_network(network): # function returns new dictionary separate from the network using game as key
  127. game_net,users = {},[]
  128. for user in network:
  129. for game in get_games_liked(network, user): #cycles through users' games lists
  130. if game not in game_net:
  131. game_net[game] = people_who_play_game(network, game)
  132. #asigns games as key value, then takes game key and\
  133. #uses previous function call to assign value being the list of people who play that game
  134. return game_net
  135.  
  136. #net = create_data_structure(example_input)
  137. #print net
  138. #print path_to_friend(net, "John", "Ollie")
  139. #print get_connections(net, "Debra")
  140. #print add_new_user(net, "Debra", [])
  141. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  142. #print get_connections(net, "Mercedes")
  143. #print get_games_liked(net, "John")
  144. #print add_connection(net, "John", "Freda")
  145. #print get_secondary_connections(net, "Mercedes")
  146. #print connections_in_common(net, "Mercedes", "John")
  147.  
  148. #print example_input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement