Advertisement
vikramk3

Untitled

Jul 23rd, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.11 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 and space. 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)
  114.         if 'play' in sub_words:
  115.             #first_game=sub_words[4]
  116.             network[name]['play']=[]
  117.             network=populate_games(network,name,sub_words,words)
  118.     return network
  119.  
  120. def populate_connections(network,name,sub_words,words):
  121.     words[0]=sub_words.pop() #replace first element with name of first connection
  122.     for connections in words:
  123.        network[name]['connected'].append(connections) #add other connections
  124.     return network
  125.    
  126. def populate_games(network,name,sub_words,words):
  127.     first_game_index=sub_words.index('play') + 1 #find index of first word of first game
  128.     first_game=sub_words[first_game_index:] # words for the first game
  129.     words[0]=' '.join(first_game) #replace first element with name of first game
  130.     for games in words:
  131.         network[name]['play'].append(games) #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 or user_B) not in network.keys():
  203.         return False
  204.     elif user_B in network[user_A]['connected']:
  205.         return network
  206.     else:
  207.         network[user_A]['connected'].append(user_B)      
  208.     return network
  209.  
  210. # -----------------------------------------------------------------------------
  211. # add_new_user(network, user, games):
  212. #   Creates a new user profile and adds that user to the network, along with
  213. #   any game preferences specified in games. Assume that the user has no
  214. #   connections to begin with.
  215. #
  216. # Arguments:
  217. #   network: the gamer network data structure
  218. #   user:    a string containing the name of the user to be added to the network
  219. #   games:   a list of strings containing the user's favorite games, e.g.:
  220. #            ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  221. #
  222. # Return:
  223. #   The updated network with the new user and game preferences added. The new user
  224. #   should have no connections.
  225. #   - If the user already exists in network, return network *UNCHANGED* (do not change
  226. #     the user's game preferences)
  227.  
  228. def add_new_user(network, user, games):
  229.     if user in network.keys():
  230.         return network
  231.     else:
  232.         network[user]={}
  233.         network[user]['connected']=[]
  234.         network[user]['play']=games
  235.     return network
  236.        
  237. # -----------------------------------------------------------------------------
  238. # get_secondary_connections(network, user):
  239. #   Finds all the secondary connections (i.e. connections of connections) of a
  240. #   given user.
  241. #
  242. # Arguments:
  243. #   network: the gamer network data structure
  244. #   user:    a string containing the name of the user
  245. #
  246. # Return:
  247. #   A list containing the secondary connections (connections of connections).
  248. #   - If the user is not in the network, return None.
  249. #   - If a user has no primary connections to begin with, return an empty list.
  250. #
  251. # NOTE:
  252. #   It is OK if a user's list of secondary connections includes the user
  253. #   himself/herself. It is also OK if the list contains a user's primary
  254. #   connection that is a secondary connection as well.
  255.  
  256. def get_secondary_connections(network, user):
  257.     second_connection=[]
  258.     if user not in network.keys():
  259.         return None
  260.     elif network[user]['connected']==[]:
  261.         return []
  262.     else:
  263.         for each_connection in network[user]['connected']:
  264.             for each_second_connection in network[each_connection]['connected']:
  265.                 if each_second_connection not in second_connection:
  266.                     second_connection.append(each_second_connection)
  267.         return second_connection
  268.    
  269. #   return []
  270.  
  271. # -----------------------------------------------------------------------------    
  272. # connections_in_common(network, user_A, user_B):
  273. #   Finds the number of people that user_A and user_B have in common.
  274. #  
  275. # Arguments:
  276. #   network: the gamer network data structure
  277. #   user_A:  a string containing the name of user_A
  278. #   user_B:  a string containing the name of user_B
  279. #
  280. # Return:
  281. #   The number of connections in common (as an integer).
  282. #   - If user_A or user_B is not in network, return False.
  283.  
  284. def connections_in_common(network, user_A, user_B):
  285.     if (user_A or user_B) not in network.keys():
  286.         return False
  287.     else:
  288.         count=0
  289.         for contact in network[user_A]['connected']:
  290.             if contact in network[user_B]['connected']:
  291.                 count = count + 1
  292.         return count
  293.    
  294. #    return 0
  295.  
  296. # -----------------------------------------------------------------------------
  297. # path_to_friend(network, user_A, user_B):
  298. #   Finds a connections path from user_A to user_B. It has to be an existing
  299. #   path but it DOES NOT have to be the shortest path.
  300. #  
  301. # Arguments:
  302. #   network: The network you created with create_data_structure.
  303. #   user_A:  String holding the starting username ("Abe")
  304. #   user_B:  String holding the ending username ("Zed")
  305. #
  306. # Return:
  307. #   A list showing the path from user_A to user_B.
  308. #   - If such a path does not exist, return None.
  309. #   - If user_A or user_B is not in network, return None.
  310. #
  311. # Sample output:
  312. #   >>> print path_to_friend(network, "Abe", "Zed")
  313. #   >>> ['Abe', 'Gel', 'Sam', 'Zed']
  314. #   This implies that Abe is connected with Gel, who is connected with Sam,
  315. #   who is connected with Zed.
  316. #
  317. # NOTE:
  318. #   You must solve this problem using recursion!
  319. #
  320. # Hints:
  321. # - Be careful how you handle connection loops, for example, A is connected to B.
  322. #   B is connected to C. C is connected to B. Make sure your code terminates in
  323. #   that case.
  324. # - If you are comfortable with default parameters, you might consider using one
  325. #   in this procedure to keep track of nodes already visited in your search. You
  326. #   may safely add default parameters since all calls used in the grading script
  327. #   will only include the arguments network, user_A, and user_B.
  328.  
  329. def path_to_friend(network, user_A, user_B,path=None):
  330.     if not path: path=[]
  331.     if (user_A or user_B) not in network.keys() :
  332.         return None
  333.     path=path + [user_A]
  334.     connections = get_connections(network,user_A)
  335.     if connections==[]:
  336.         return None
  337.     if user_B in connections:
  338.         path = path + [user_B]
  339.         return path
  340.     for contact in connections:
  341.         if contact not in path:
  342.             final_path=path_to_friend(network,contact,user_B,path)
  343.             if final_path:
  344.                 return final_path
  345.     return None
  346.  
  347.     # your RECURSIVE solution here!
  348. #   return None
  349.  
  350. # Make-Your-Own-Procedure (MYOP)
  351. # -----------------------------------------------------------------------------
  352. # Your MYOP should either perform some manipulation of your network data
  353. # structure (like add_new_user) or it should perform some valuable analysis of
  354. # your network (like path_to_friend). Don't forget to comment your MYOP. You
  355. # may give this procedure any name you want.
  356.  
  357. def recommend_new_contact(network,user): #MYOP Recommend new contact based on playing a common game
  358.     if user not in network.keys():
  359.         return 'User not in Network'
  360.     for game in get_games_liked(network,user):
  361.         for name in network.keys():
  362.             if (game in get_games_liked(network,name)) and (name!=user) and (name not in get_connections(network,user)):
  363.                 return'We recommend you connect with ' + name +'. '+ name +' also plays ' +game+'.'
  364.     return 'No recommendation for a new contact at present'
  365.  
  366. # Replace this with your own procedure! You can also uncomment the lines below
  367. # to see how your code behaves. Have fun!
  368.  
  369. network = create_data_structure(example_input)
  370. print network
  371.  
  372. print path_to_friend(network, "John", "Ollie")
  373. print get_connections(network, "Debra")
  374. print add_new_user(network, "Debra", [])
  375. print add_new_user(network, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  376. print get_connections(network, "Mercedes")
  377. print get_games_liked(network, "John")
  378. print add_connection(network, "John", "Freda")
  379. print get_secondary_connections(network, "Mercedes")
  380. print connections_in_common(network, "Mercedes", "John")
  381. print recommend_new_contact(network,'Debra')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement