Advertisement
vikramk3

Untitled

Jul 20th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.83 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(text_input):
  100.     if text_input=='':
  101.         network={}
  102.         return network
  103.     sentences=text_input.split('.')
  104.     sentences.pop() # the above split function creates a blank line. Don't know why?
  105.     network={}
  106.     for element in sentences:
  107.         words=element.split(', ') #split each line by a comma. First element is a partial sentence
  108.         sub_words=words[0].split() #split the first element of the line previously split by comma
  109.         name=sub_words[0]
  110.         if 'connected' in sub_words:
  111.             network[name]={} #create dictionary within a dictionary
  112.             network[name]['connected']=[]
  113.             network= populate_connections(network,name,sub_words,words) #MYOP to populate connections
  114.         if 'play' in sub_words:
  115.             network[name]['play']=[]
  116.             network=populate_games(network,name,sub_words,words) #MYOP to populate games
  117.     return network
  118.  
  119. def populate_connections(network,name,sub_words,words): #MYOP to populate connections
  120.     network[name]['connected'].append(sub_words[-1]) #use last word (i.e. a connection) of the first element of the line
  121.     for n in range(1,len(words)):
  122.        network[name]['connected'].append(words[n]) #add other connections
  123.     return network
  124.    
  125. def populate_games(network,name,sub_words,words): #MYOP to populate games
  126.     first_game=sub_words[4] #first word of the name of the first game
  127.     for i in range(5,len(sub_words)): #after 'play', put words together for full first game name
  128.         first_game= first_game + ' ' +sub_words[i]
  129.     network[name]['play'].append(first_game)
  130.     for n in range(1,len(words)):
  131.         network[name]['play'].append(words[n]) #add other games
  132.     return network
  133. # ----------------------------------------------------------------------------- #
  134. # Note that the first argument to all procedures below is 'network' This is the #
  135. # data structure that you created with your create_data_structure procedure,    #
  136. # though it may be modified as you add new users or new connections. Each       #
  137. # procedure below will then modify or extract information from 'network'        #
  138. # ----------------------------------------------------------------------------- #
  139.  
  140. # -----------------------------------------------------------------------------
  141. # get_connections(network, user):
  142. #   Returns a list of all the connections that user has
  143. #
  144. # Arguments:
  145. #   network: the gamer network data structure
  146. #   user:    a string containing the name of the user
  147. #
  148. # Return:
  149. #   A list of all connections the user has.
  150. #   - If the user has no connections, return an empty list.
  151. #   - If the user is not in network, return None.
  152.  
  153. def get_connections(network, user):
  154.     if user not in network.keys():
  155.         return None
  156.     elif network[user]['connected']==[]:
  157.         return []
  158.     else:
  159.         output=network[user]['connected']
  160.         return output
  161.    
  162. #   return []
  163.  
  164. # -----------------------------------------------------------------------------
  165. # get_games_liked(network, user):
  166. #   Returns a list of all the games a user likes
  167. #
  168. # Arguments:
  169. #   network: the gamer network data structure
  170. #   user:    a string containing the name of the user
  171. #
  172. # Return:
  173. #   A list of all games the user likes.
  174. #   - If the user likes no games, return an empty list.
  175. #   - If the user is not in network, return None.
  176.  
  177. def get_games_liked(network,user):
  178.     if user not in network.keys():
  179.         return None
  180.     else:
  181.         output=network[user]['play']
  182.         return output  
  183.    
  184. #    return []
  185.  
  186. # -----------------------------------------------------------------------------
  187. # add_connection(network, user_A, user_B):
  188. #   Adds a connection from user_A to user_B. Make sure to check that both users
  189. #   exist in network.
  190. #
  191. # Arguments:
  192. #   network: the gamer network data structure
  193. #   user_A:  a string with the name of the user the connection is from
  194. #   user_B:  a string with the name of the user the connection is to
  195. #
  196. # Return:
  197. #   The updated network with the new connection added.
  198. #   - If a connection already exists from user_A to user_B, return network unchanged.
  199. #   - If user_A or user_B is not in network, return False.
  200.  
  201. def add_connection(network, user_A, user_B):
  202.     if user_A not in network.keys():
  203.         return False
  204.     elif user_B not in network.keys():
  205.         return False
  206.     elif user_B in network[user_A]['connected']:
  207.         return network
  208.     else:
  209.         network[user_A]['connected'].append(user_B)      
  210.     return network
  211.  
  212. # -----------------------------------------------------------------------------
  213. # add_new_user(network, user, games):
  214. #   Creates a new user profile and adds that user to the network, along with
  215. #   any game preferences specified in games. Assume that the user has no
  216. #   connections to begin with.
  217. #
  218. # Arguments:
  219. #   network: the gamer network data structure
  220. #   user:    a string containing the name of the user to be added to the network
  221. #   games:   a list of strings containing the user's favorite games, e.g.:
  222. #            ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  223. #
  224. # Return:
  225. #   The updated network with the new user and game preferences added. The new user
  226. #   should have no connections.
  227. #   - If the user already exists in network, return network *UNCHANGED* (do not change
  228. #     the user's game preferences)
  229.  
  230. def add_new_user(network, user, games):
  231.     if user in network.keys():
  232.         return network
  233.     else:
  234.         network[user]={}
  235.         network[user]['connected']=[]
  236.         network[user]['play']=games
  237.     return network
  238.        
  239. # -----------------------------------------------------------------------------
  240. # get_secondary_connections(network, user):
  241. #   Finds all the secondary connections (i.e. connections of connections) of a
  242. #   given user.
  243. #
  244. # Arguments:
  245. #   network: the gamer network data structure
  246. #   user:    a string containing the name of the user
  247. #
  248. # Return:
  249. #   A list containing the secondary connections (connections of connections).
  250. #   - If the user is not in the network, return None.
  251. #   - If a user has no primary connections to begin with, return an empty list.
  252. #
  253. # NOTE:
  254. #   It is OK if a user's list of secondary connections includes the user
  255. #   himself/herself. It is also OK if the list contains a user's primary
  256. #   connection that is a secondary connection as well.
  257.  
  258. def get_secondary_connections(network, user):
  259.     second_connection=[]
  260.     if user not in network.keys():
  261.         return None
  262.     elif network[user]['connected']==[]:
  263.         return []
  264.     else:
  265.         for each_connection in network[user]['connected']:
  266.             for each_second_connection in network[each_connection]['connected']:
  267.                 if each_second_connection not in second_connection:
  268.                     second_connection.append(each_second_connection)
  269.         return second_connection
  270.    
  271. #   return []
  272.  
  273. # -----------------------------------------------------------------------------    
  274. # connections_in_common(network, user_A, user_B):
  275. #   Finds the number of people that user_A and user_B have in common.
  276. #  
  277. # Arguments:
  278. #   network: the gamer network data structure
  279. #   user_A:  a string containing the name of user_A
  280. #   user_B:  a string containing the name of user_B
  281. #
  282. # Return:
  283. #   The number of connections in common (as an integer).
  284. #   - If user_A or user_B is not in network, return False.
  285.  
  286. def connections_in_common(network, user_A, user_B):
  287.     if user_A not in network.keys():
  288.         return False
  289.     elif user_B not in network.keys():
  290.         return False
  291.     else:
  292.         count=0
  293.         for contact in network[user_A]['connected']:
  294.             if contact in network[user_B]['connected']:
  295.                 count = count + 1
  296.         return count
  297.    
  298. #    return 0
  299.  
  300. # -----------------------------------------------------------------------------
  301. # path_to_friend(network, user_A, user_B):
  302. #   Finds a connections path from user_A to user_B. It has to be an existing
  303. #   path but it DOES NOT have to be the shortest path.
  304. #  
  305. # Arguments:
  306. #   network: The network you created with create_data_structure.
  307. #   user_A:  String holding the starting username ("Abe")
  308. #   user_B:  String holding the ending username ("Zed")
  309. #
  310. # Return:
  311. #   A list showing the path from user_A to user_B.
  312. #   - If such a path does not exist, return None.
  313. #   - If user_A or user_B is not in network, return None.
  314. #
  315. # Sample output:
  316. #   >>> print path_to_friend(network, "Abe", "Zed")
  317. #   >>> ['Abe', 'Gel', 'Sam', 'Zed']
  318. #   This implies that Abe is connected with Gel, who is connected with Sam,
  319. #   who is connected with Zed.
  320. #
  321. # NOTE:
  322. #   You must solve this problem using recursion!
  323. #
  324. # Hints:
  325. # - Be careful how you handle connection loops, for example, A is connected to B.
  326. #   B is connected to C. C is connected to B. Make sure your code terminates in
  327. #   that case.
  328. # - If you are comfortable with default parameters, you might consider using one
  329. #   in this procedure to keep track of nodes already visited in your search. You
  330. #   may safely add default parameters since all calls used in the grading script
  331. #   will only include the arguments network, user_A, and user_B.
  332.  
  333. def path_to_friend(network, user_A, user_B,path=None):
  334.     if path is None:
  335.         path=[]
  336.     if user_A not in network.keys():
  337.         return None
  338.     if user_B not in network.keys():
  339.         return None
  340.     path=path + [user_A]
  341.     connections = get_connections(network,user_A)
  342.     if connections==[]:
  343.         return None
  344.     if user_B in connections:
  345.         path = path + [user_B]
  346.         return path
  347.     for contact in connections:
  348.         if contact not in path:
  349.             final_path=path_to_friend(network,contact,user_B,path)
  350.             if final_path != None:
  351.                 return final_path
  352.     return None
  353.  
  354.     # your RECURSIVE solution here!
  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. network = create_data_structure(example_input)
  368. print network
  369.  
  370. print path_to_friend(network, "John", "Ollie")
  371. print get_connections(network, "Debra")
  372. print add_new_user(network, "Debra", [])
  373. print add_new_user(network, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  374. print get_connections(network, "Mercedes")
  375. print get_games_liked(network, "John")
  376. print add_connection(network, "John", "Freda")
  377. print get_secondary_connections(network, "Mercedes")
  378. print connections_in_common(network, "Mercedes", "John")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement