Advertisement
Guest User

AV CS101 Final Project

a guest
Nov 24th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.58 KB | None | 0 0
  1. # --------------------------- #
  2. # Intro to CS Final Project   #
  3. # Gaming Social Network       #
  4. # --------------------------- #
  5. #
  6. # For students who have subscribed to the course,
  7. # please read the submission instructions in the Instructor Notes below.
  8. # -----------------------------------------------------------------------------
  9.  
  10. # Background
  11. # ==========
  12. # You and your friend have decided to start a company that hosts a gaming
  13. # social network site. Your friend will handle the website creation (they know
  14. # what they are doing, having taken our web development class). However, it is
  15. # up to you to create a data structure that manages the game-network information
  16. # and to define several procedures that operate on the network.
  17. #
  18. # In a website, the data is stored in a database. In our case, however, all the
  19. # information comes in a big string of text. Each pair of sentences in the text
  20. # is formatted as follows:
  21. #
  22. # <user> is connected to <user1>, ..., <userM>.<user> likes to play <game1>, ..., <gameN>.
  23. #
  24. # For example:
  25. #
  26. # John is connected to Bryant, Debra, Walter.John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.
  27. #
  28. # Note that each sentence will be separated from the next by only a period. There will
  29. # not be whitespace or new lines between sentences.
  30. #
  31. # Your friend records the information in that string based on user activity on
  32. # the website and gives it to you to manage. You can think of every pair of
  33. # sentences as defining a user's profile.
  34. #
  35. # Consider the data structures that we have used in class - lists, dictionaries,
  36. # and combinations of the two (e.g. lists of dictionaries). Pick one that
  37. # will allow you to manage the data above and implement the procedures below.
  38. #
  39. # You may assume that <user> is a unique identifier for a user. For example, there
  40. # can be at most one 'John' in the network. Furthermore, connections are not
  41. # symmetric - if 'Bob' is connected to 'Alice', it does not mean that 'Alice' is
  42. # connected to 'Bob'.
  43. #
  44. # Project Description
  45. # ====================
  46. # Your task is to complete the procedures according to the specifications below
  47. # as well as to implement a Make-Your-Own procedure (MYOP). You are encouraged
  48. # to define any additional helper procedures that can assist you in accomplishing
  49. # a task. You are encouraged to test your code by using print statements and the
  50. # Test Run button.
  51. # -----------------------------------------------------------------------------
  52.  
  53. # Example string input. Use it to test your code.
  54. example_input="John is connected to Bryant, Debra, Walter.\
  55. John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
  56. Bryant is connected to Olive, Ollie, Freda, Mercedes.\
  57. Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
  58. Mercedes is connected to Walter, Robin, Bryant.\
  59. Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
  60. Olive is connected to John, Ollie.\
  61. Olive likes to play The Legend of Corgi, Starfleet Commander.\
  62. Debra is connected to Walter, Levi, Jennie, Robin.\
  63. Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
  64. Walter is connected to John, Levi, Bryant.\
  65. Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
  66. Levi is connected to Ollie, John, Walter.\
  67. Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
  68. Ollie is connected to Mercedes, Freda, Bryant.\
  69. Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
  70. Jennie is connected to Levi, John, Freda, Robin.\
  71. Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
  72. Robin is connected to Ollie.\
  73. Robin likes to play Call of Arms, Dwarves and Swords.\
  74. Freda is connected to Olive, John, Debra.\
  75. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
  76.  
  77. # -----------------------------------------------------------------------------
  78. # create_data_structure(string_input):
  79. #   Parses a block of text (such as the one above) and stores relevant
  80. #   information into a data structure. You are free to choose and design any
  81. #   data structure you would like to use to manage the information.
  82. #
  83. # Arguments:
  84. #   string_input: block of text containing the network information
  85. #
  86. #   You may assume that for all the test cases we will use, you will be given the
  87. #   connections and games liked for all users listed on the right-hand side of an
  88. #   'is connected to' statement. For example, we will not use the string
  89. #   "A is connected to B.A likes to play X, Y, Z.C is connected to A.C likes to play X."
  90. #   as a test case for create_data_structure because the string does not
  91. #   list B's connections or liked games.
  92. #  
  93. #   The procedure should be able to handle an empty string (the string '') as input, in
  94. #   which case it should return a network with no users.
  95. #
  96. # Return:
  97. #   The newly created network data structure
  98.  
  99. def create_data_structure(string_input):
  100.     network = {}
  101.     graph = {}
  102.     players = get_all_players(string_input)
  103.     if string_input == '':
  104.         return network, graph
  105.     for i in players:
  106.         if i not in network:
  107.             network[i] = get_user_games(string_input, i)
  108.             graph[i] = get_user_connections(string_input, i)
  109.     return network, graph
  110.  
  111. def get_next_player(string_input):
  112.     start = string_input.find('is connected to')
  113.     name_rev = ''
  114.     if start == -1:
  115.         return None, 0
  116.     for i in reversed(string_input[:start]):
  117.         if i == '.':
  118.             break
  119.         if i.isalpha(): #Fix immediately
  120.             name_rev += i
  121.             player = name_rev[::-1]
  122.             endpos = start + len(' is connected to')
  123.     return player, endpos
  124.  
  125. def get_all_players(string_input):
  126.     user_string = ''
  127.     while get_next_player(string_input) != None:
  128.         player, end = get_next_player(string_input)
  129.         if player:
  130.             user_string += player + " "
  131.             string_input = string_input[end:]
  132.         else:
  133.             break
  134.     return user_string.split()
  135. #above works
  136.  
  137. def get_user_connections(string_input, user):#get the connection
  138.     connection_string = ''
  139.     connections = {}
  140.     connection_start = string_input.find(user + ' is connected to ') + len(user + ' is connected to ')
  141.     if connection_start == -1: #if there is an error, return nothing
  142.         return None
  143.     end_point = string_input.find('.', connection_start)
  144.     connection_string = string_input[connection_start : end_point]
  145.     connections = connection_string.split(', ')
  146.     return connections
  147. #above works
  148.  
  149. def get_user_games(string_input, user):
  150.     games = {}
  151.     game_string = ''
  152.     game_start = string_input.find(user + ' likes to play ') + len(user + ' likes to play ')
  153.     if game_start == -1: #if there is an error, return nothing
  154.         return None
  155.     end_point = string_input.find('.', game_start)
  156.     game_string = string_input[game_start : end_point]
  157.     games = game_string.split(', ')
  158.     return games
  159.  
  160. def not_in_network(network, user_A, user_B):    
  161.     if user_A not in network[1] or user_B not in network[1]:
  162.         return True
  163.     return False
  164.  
  165. # ----------------------------------------------------------------------------- #
  166. # Note that the first argument to all procedures below is 'network' This is the #
  167. # data structure that you created with your create_data_structure procedure,    #
  168. # though it may be modified as you add new users or new connections. Each       #
  169. # procedure below will then modify or extract information from 'network'        #
  170. # ----------------------------------------------------------------------------- #
  171.  
  172. # -----------------------------------------------------------------------------
  173. # get_connections(network, user):
  174. #   Returns a list of all the connections that user has
  175. #
  176. # Arguments:
  177. #   network: the gamer network data structure
  178. #   user:    a string containing the name of the user
  179. #
  180. # Return:
  181. #   A list of all connections the user has.
  182. #   - If the user has no connections, return an empty list.
  183. #   - If the user is not in network, return None.
  184. def get_connections(network, user):
  185.     connections = []
  186.     if user in network[1]:
  187.         if network[1] != []:
  188.             return network[1][user]
  189.         return connections
  190.     else:
  191.         return None
  192.  
  193. # -----------------------------------------------------------------------------
  194. # get_games_liked(network, user):
  195. #   Returns a list of all the games a user likes
  196. #
  197. # Arguments:
  198. #   network: the gamer network data structure
  199. #   user:    a string containing the name of the user
  200. #
  201. # Return:
  202. #   A list of all games the user likes.
  203. #   - If the user likes no games, return an empty list.
  204. #   - If the user is not in network, return None.
  205. def get_games_liked(network, user): #lookup of games
  206.     if user in network[0]:
  207.         return network[0][user]
  208.     else:
  209.         return None
  210.  
  211. # -----------------------------------------------------------------------------
  212. # add_connection(network, user_A, user_B):
  213. #   Adds a connection from user_A to user_B. Make sure to check that both users
  214. #   exist in network.
  215. #
  216. # Arguments:
  217. #   network: the gamer network data structure
  218. #   user_A:  a string with the name of the user the connection is from
  219. #   user_B:  a string with the name of the user the connection is to
  220. #
  221. # Return:
  222. #   The updated network with the new connection added.
  223. #   - If a connection already exists from user_A to user_B, return network unchanged.
  224. #   - If user_A or user_B is not in network, return False.
  225. def add_connection(network, user_A, user_B):
  226.     if not_in_network(network, user_A, user_B):
  227.         return False
  228.     for c in network[1][user_A]:
  229.             if user_B != c:
  230.                 network[1][user_A].append(user_B)
  231.             return None
  232.     return
  233.  
  234. # -----------------------------------------------------------------------------
  235. # add_new_user(network, user, games):
  236. #   Creates a new user profile and adds that user to the network, along with
  237. #   any game preferences specified in games. Assume that the user has no
  238. #   connections to begin with.
  239. #
  240. # Arguments:
  241. #   network: the gamer network data structure
  242. #   user:    a string containing the name of the user to be added to the network
  243. #   games:   a list of strings containing the user's favorite games, e.g.:
  244. #            ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  245. #
  246. # Return:
  247. #   The updated network with the new user and game preferences added. The new user
  248. #   should have no connections.
  249. #   - If the user already exists in network, return network *UNCHANGED* (do not change
  250. #     the user's game preferences)
  251. def add_new_user(network, user, games):    
  252.     if user not in network[0]:
  253.         network[0][user] = games
  254.         network[1][user] = []
  255.     else:
  256.         return network
  257.        
  258. # -----------------------------------------------------------------------------
  259. # get_secondary_connections(network, user):
  260. #   Finds all the secondary connections (i.e. connections of connections) of a
  261. #   given user.
  262. #
  263. # Arguments:
  264. #   network: the gamer network data structure
  265. #   user:    a string containing the name of the user
  266. #
  267. # Return:
  268. #   A list containing the secondary connections (connections of connections).
  269. #   - If the user is not in the network, return None.
  270. #   - If a user has no primary connections to begin with, return an empty list.
  271. #
  272. # NOTE:
  273. #   It is OK if a user's list of secondary connections includes the user
  274. #   himself/herself. It is also OK if the list contains a user's primary
  275. #   connection that is a secondary connection as well.
  276. def get_secondary_connections(network, user):
  277.     no_friends = []
  278.     secondary_connections = []
  279.     if user not in network[1]:
  280.         return None
  281.     if network[1][user] == '':
  282.         return no_friends
  283.     for primary in network[1][user]:
  284.         secondary_connections.extend(network[1][primary])
  285.     return secondary_connections
  286.  
  287. # -----------------------------------------------------------------------------    
  288. # connections_in_common(network, user_A, user_B):
  289. #   Finds the number of people that user_A and user_B have in common.
  290. #  
  291. # Arguments:
  292. #   network: the gamer network data structure
  293. #   user_A:  a string containing the name of user_A
  294. #   user_B:  a string containing the name of user_B
  295. #
  296. # Return:
  297. #   The number of connections in common (as an integer).
  298. #   - If user_A or user_B is not in network, return False.
  299. def connections_in_common(network, user_A, user_B):
  300.     common_connections = 0
  301.     if not_in_network(network, user_A, user_B):
  302.         return False
  303.     for connection in network[1][user_A]:
  304.             if connection in network[1][user_B]:  
  305.                 common_connections += 1
  306.     return common_connections
  307.  
  308. # -----------------------------------------------------------------------------
  309. # path_to_friend(network, user_A, user_B):
  310. #   Finds a connections path from user_A to user_B. It has to be an existing
  311. #   path but it DOES NOT have to be the shortest path.
  312. #  
  313. # Arguments:
  314. #   network: The network you created with create_data_structure.
  315. #   user_A:  String holding the starting username ("Abe")
  316. #   user_B:  String holding the ending username ("Zed")
  317. #
  318. # Return:
  319. #   A list showing the path from user_A to user_B.
  320. #   - If such a path does not exist, return None.
  321. #   - If user_A or user_B is not in network, return None.
  322. #
  323. # Sample output:
  324. #   >>> print path_to_friend(network, "Abe", "Zed")
  325. #   >>> ['Abe', 'Gel', 'Sam', 'Zed']
  326. #   This implies that Abe is connected with Gel, who is connected with Sam,
  327. #   who is connected with Zed.
  328. #
  329. # NOTE:
  330. #   You must solve this problem using recursion!
  331. #
  332. # Hints:
  333. # - Be careful how you handle connection loops, for example, A is connected to B.
  334. #   B is connected to C. C is connected to B. Make sure your code terminates in
  335. #   that case.
  336. # - If you are comfortable with default parameters, you might consider using one
  337. #   in this procedure to keep track of nodes already visited in your search. You
  338. #   may safely add default parameters since all calls used in the grading script
  339. #   will only include the arguments network, user_A, and user_B.
  340.  
  341. path = []
  342. def path_to_friend(network, user_A, user_B):    
  343.     if not_in_network(network, user_A, user_B):
  344.         return None
  345.     if user_B in network[1][user_A]:
  346.         path.append(user_B)
  347.         return [user_A, user_B]
  348.     # your RECURSIVE solution here!    
  349.     else:
  350.         path.append(user_A)
  351.         for connection in network[1][user_A]:        
  352.             if connection not in path:
  353.                 if path_to_friend(network, connection, user_B) != None:
  354.                     return [user_A] + path_to_friend(network, connection, user_B)
  355.     return None
  356.  
  357. # Make-Your-Own-Procedure (MYOP)
  358. # -----------------------------------------------------------------------------
  359. # Your MYOP should either perform some manipulation of your network data
  360. # structure (like add_new_user) or it should perform some valuable analysis of
  361. # your network (like path_to_friend). Don't forget to comment your MYOP. You
  362. # may give this procedure any name you want.
  363.  
  364. # Replace this with your own procedure! You can also uncomment the lines below
  365. # to see how your code behaves. Have fun!
  366.  
  367. def compute_most_popular(social):
  368.     user_data = {}
  369.     game_data = {}
  370.     for user in social[0]:
  371.         for game in social[0][user]:
  372.             if game not in game_data:
  373.                 game_data[game] = 1                
  374.         game_data[game] += 1              
  375.     for user in social[1]:
  376.         for connection in social[1][user]:
  377.             if connection not in user_data:
  378.                 user_data[connection] = 1                
  379.         user_data[connection] += 1              
  380.     return max(game_data), max(user_data)
  381.  
  382. #print get_next_player(example_input)
  383. #print get_all_players(example_input)
  384. #net = create_data_structure(example_input)
  385. network = create_data_structure('')
  386.  
  387. #print get_user_games(example_input, 'Bryant')
  388. #print net
  389. #print path_to_friend(net, "John", "Ollie")
  390. #print get_connections(net, "Debra")
  391. #print add_new_user(net, "Debra", [])
  392. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"])
  393. print add_new_user(network,'Alice',[])
  394. print network
  395. #print get_connections(net, "Mercedes")
  396. #print net
  397. #print get_games_liked(net, "John")
  398. #print add_connection(net, "John", "Freda")
  399. print add_connection(network, "Alice", "Bob")
  400. #print get_secondary_connections(net, "Mercedes")
  401. #print connections_in_common(net, "Mercedes", "John")
  402. #print compute_most_popular(net)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement