Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.53 KB | None | 0 0
  1. # -------------------------------- #
  2. # Intro to CS Final Project #
  3. # Gaming Social Network [Option 1] #
  4. # -------------------------------- #
  5. #
  6. # For students who have paid for the full course experience:
  7. # please check submission instructions in the Instructor Note 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. # <username> is connected to <name1>, <name2>,...,<nameN>.
  23. # <username> likes to play <game1>,...,<gameN>.
  24. #
  25. # Your friend records the information in that string based on user activity on
  26. # the website and gives it to you to manage. You can think of every pair of
  27. # sentences as defining a gamer profile. For example:
  28. #
  29. # John is connected to Bryant, Debra, Walter.
  30. # John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.
  31. #
  32. # Consider the data structures that we have used in class - lists, dictionaries,
  33. # and combinations of the two - (e.g. lists of dictionaries). Pick one which
  34. # will allow you to manage the data above and implement the procedures below.
  35. #
  36. # You can assume that <username> is a unique identifier for a user. In other
  37. # words, there is only one John in the network. Furthermore, connections are not
  38. # symmetric - if John is connected with Alice, it does not mean that Alice is
  39. # connected with John.
  40. #
  41. # Project Description
  42. # ====================
  43. # Your task is to complete the procedures according to the specifications below
  44. # as well as to implement a Make-Your-Own procedure (MYOP). You are encouraged
  45. # to define any additional helper procedures that can assist you in accomplishing
  46. # a task. You are encouraged to test your code by using print statements and the
  47. # Test Run button.
  48. # -----------------------------------------------------------------------------
  49.  
  50. # Example string input. Use it to test your code.
  51. # Some details: Each sentence will be separated from one another with only
  52. # a period (there will not be whitespace or new lines between sentences)
  53. example_input="John is connected to Bryant, Debra, Walter.\
  54. John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
  55. Bryant is connected to Olive, Ollie, Freda, Mercedes.\
  56. Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
  57. Mercedes is connected to Walter, Robin, Bryant.\
  58. Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
  59. Olive is connected to John, Ollie.\
  60. Olive likes to play The Legend of Corgi, Starfleet Commander.\
  61. Debra is connected to Walter, Levi, Jennie, Robin.\
  62. Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
  63. Walter is connected to John, Levi, Bryant.\
  64. Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
  65. Levi is connected to Ollie, John, Walter.\
  66. Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
  67. Ollie is connected to Mercedes, Freda, Bryant.\
  68. Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
  69. Jennie is connected to Levi, John, Freda, Robin.\
  70. Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
  71. Robin is connected to Ollie.\
  72. Robin likes to play Call of Arms, Dwarves and Swords.\
  73. Freda is connected to Olive, John, Debra.\
  74. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
  75.  
  76. example_input_alternate="""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."""
  77.  
  78. # -----------------------------------------------------------------------------
  79. # create_data_structure(string_input):
  80. # Parses a block of text (such as the one above) and stores relevant
  81. # information into a data structure. You are free to choose and design any
  82. # data structure you would like to use to manage the information.
  83. #
  84. # Arguments:
  85. # string_input: block of text containing the network information
  86. #
  87. # Return:
  88. # The new network data structure
  89.  
  90. def create_data_structure(string_input):
  91. network = {}
  92. if string_input=='':
  93. return network
  94. else:
  95. sentences = string_input.split(".")
  96. for eachSentence in sentences[:-1]:
  97. username_activity = eachSentence.split(" ")
  98. username = username_activity[0]
  99. if(username_activity[1] == "is"):
  100. saveData(username,eachSentence,network,'connection')
  101. elif(username_activity[1] == "likes"):
  102. saveData(username,eachSentence,network,'games')
  103. return network
  104.  
  105.  
  106. def saveData(username,sentence,network,type1):
  107. if(type1=='connection'):
  108. connections=findConnections(sentence,'connection')
  109. if(username not in network):
  110. network[username]=[]
  111. network[username]=[GamesOrConnections]
  112. else:
  113. games=findConnections(sentence,'games')
  114. network[username].append(GamesOrConnections)
  115. return network
  116.  
  117.  
  118. def findGamesOrConnections(sentence,GameOrConnection):
  119. if(GameOrConnection=='connection'):
  120. delimiter='is connected to '
  121. sentenceSpliter(sentence,delimiter)
  122.  
  123. else:
  124. delimiter='likes to play '
  125. sentenceSpliter(sentence,delimiter)
  126.  
  127. def sentenceSpliter(sentence,delimiter):
  128. GamesOrConnections=[]
  129. sentence=sentence.split( delimiter)
  130. for eachSentence in range(1,len(sentence)):
  131. secondSent=sentence[eachSentence].split(', ')
  132. for eachSent in secondSent:
  133. GamesOrConnections.append(eachSent)
  134. return GamesOrConnections
  135.  
  136. #print(create_data_structure(example_input_alternate))
  137. # ----------------------------------------------------------------------------- #
  138. # Note that the first argument to all procedures below is 'network' This is the #
  139. # data structure that you created with your create_data_structure procedure, #
  140. # though it may be modified as you add new users or new connections. Each #
  141. # procedure below will then modify or extract information from 'network' #
  142. # ----------------------------------------------------------------------------- #
  143.  
  144. # -----------------------------------------------------------------------------
  145. # get_connections(network, user):
  146. # Returns a list of all the connections a user has.
  147. #
  148. # Arguments:
  149. # network: The network you created with create_data_structure.
  150. # user: String containing the name of the user.
  151. #
  152. # Return:
  153. # A list of all connections the user has. If the user has no connections,
  154. # return an empty list. If the user is not in network, return None.
  155. def get_connections(network, user):
  156. if user in network:
  157. if (len(network[user][0]) > 0):
  158. return network[user][0]
  159. else:
  160. return []
  161. else:
  162. return None
  163.  
  164.  
  165. # -----------------------------------------------------------------------------
  166. # add_connection(network, user_A, user_B):
  167. # Adds a connection from user_A to user_B. Make sure to check that both users
  168. # exist in network.
  169. #
  170. # Arguments:
  171. # network: The network you created with create_data_structure.
  172. # user_A: String with the name of the user ("Gary")
  173. # user_B: String with the name of the user that will be the new connection.
  174. #
  175. # Return:
  176. # The updated network with the new connection added (if necessary), or False
  177. # if user_A or user_B do not exist in network.
  178. def add_connection(network, user_A, user_B):
  179.  
  180. if (user_A in network and user_B in network):
  181. if(user_B not in network[user_A][0]):
  182. network[user_A][0].append(user_B) #adds user_B to the network of user_A
  183. return network
  184.  
  185. else:
  186. return False
  187.  
  188. # -----------------------------------------------------------------------------
  189. # add_new_user(network, user, games):
  190. # Creates a new user profile and adds that user to the network, along with
  191. # any game preferences specified in games. Assume that the user has no
  192. # connections to begin with.
  193. #
  194. # Arguments:
  195. # network: The network you created with create_data_structure.
  196. # user: String containing the users name to be added (e.g. "Dave")
  197. # games: List containing the user's favorite games, e.g.:
  198. # ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  199. #
  200. # Return:
  201. # The updated network with the new user and game preferences added. If the
  202. # user is already in the network, update their game preferences as necessary.
  203. def add_new_user(network, user, games):
  204. if (user in network):
  205. return network
  206. else:
  207. network[user] = [[], games]
  208. return network
  209.  
  210.  
  211. def get_games_liked(network,user):
  212. if user not in network:
  213. return None
  214. else:
  215. return network[user][1]
  216.  
  217.  
  218.  
  219. # -----------------------------------------------------------------------------
  220. # get_secondary_connections(network, user):
  221. # Finds all the secondary connections, i.e. connections of connections, of a
  222. # given user.
  223. #
  224. # Arguments:
  225. # network: The network you created with create_data_structure.
  226. # user: String containing the name of a user.
  227. #
  228. # Return:
  229. # A list containing the secondary connections (connections of connections).
  230. # If the user is not in the network, return None. If a user has no primary
  231. # connections to begin with, you should return an empty list.
  232. #
  233. # NOTE:
  234. # It is OK if a user's list of secondary connections includes the user
  235. # himself/herself. It is also OK if the list contains a user's primary
  236. # connection that is a secondary connection as well.
  237. def get_secondary_connections(network, user):
  238. if user not in network:
  239. return None
  240. secondaryConnections = network[user][0]
  241. secondaryConnectionsList = []
  242. for eachConnection in secondaryConnections:
  243. secondaryConnectionsList += get_connections(network, eachConnection)
  244. return secondaryConnectionsList
  245.  
  246. # -----------------------------------------------------------------------------
  247. # connections_in_common(network, user_A, user_B):
  248. # Finds the number of people that user_A and user_B have in common.
  249. #
  250. # Arguments:
  251. # network: The network you created with create_data_structure.
  252. # user_A: String containing the name of user_A.
  253. # user_B: String containing the name of user_B.
  254. #
  255. # Return:
  256. # The number of connections in common (integer). Should return False if
  257. # user_A or user_B are not in network.
  258. def connections_in_common(network, user_A, user_B):
  259. numberOfConnections = 0
  260. if (user_A in network and user_B in network):
  261. commonFriends = {}
  262. for Connection in network[user_A][0]:
  263. commonFriends[Connection] = 0
  264. for Connection in network[user_B][0]:
  265. if Connection in commonFriends:
  266. commonFriends[Connection] = 1
  267. for CommonConnections in commonFriends:
  268. if commonFriends[CommonConnections] == 1:
  269. numberOfConnections += 1
  270. return numberOfConnections
  271. else:
  272. return False
  273.  
  274. # -----------------------------------------------------------------------------
  275. # path_to_friend(network, user, connection):
  276. # Finds the connections path from user_A to user_B. It has to be an existing
  277. # path but it DOES NOT have to be the shortest path.
  278. # Solve this problem using recursion.
  279. # Arguments:
  280. # network: The network you created with create_data_structure.
  281. # user_A: String holding the starting username ("Abe")
  282. # user_B: String holding the ending username ("Zed")
  283. #
  284. # Return:
  285. # A List showing the path from user_A to user_B. If such a path does not
  286. # exist, return None
  287. #
  288. # Sample output:
  289. # >>> print path_to_friend(network, "Abe", "Zed")
  290. # >>> ['Abe', 'Gel', 'Sam', 'Zed']
  291. # This implies that Abe is connected with Gel, who is connected with Sam,
  292. # who is connected with Zed.
  293. #
  294. # NOTE:
  295. # You must solve this problem using recursion!
  296. #
  297. # Hint:
  298. # Be careful how you handle connection loops, for example, A is connected to B.
  299. # B is connected to C. C is connected to B. Make sure your code terminates in
  300. # that case.
  301. def path_to_friend_used(network, user, connection, used):
  302. if(user in network):
  303. pathList = []
  304. used.append(user)
  305. if (connection in network[user][0]):
  306. pathList = [user, connection]
  307. return pathList
  308. else:
  309. for eachConnection in network[user][0]:
  310. if eachConnection in network and eachConnection not in used:
  311. pathFound = path_to_friend_used(network, eachConnection, connection, used)
  312. if pathFound != None:
  313. pathList.append(user)
  314. pathList += pathFound
  315. return pathList
  316.  
  317.  
  318. def path_to_friend(network, user, connection):
  319. return path_to_friend_used(network, user, connection, [])
  320.  
  321.  
  322. # Make-Your-Own-Procedure (MYOP)
  323. # -----------------------------------------------------------------------------
  324. # Your MYOP should either perform some manipulation of your network data
  325. # structure (like add_new_user) or it should perform some valuable analysis of
  326. # your network (like path_to_friend). Don't forget to comment your MYOP. You
  327. # may give this procedure any name you want.
  328.  
  329. # Replace this with your own procedure! You can also uncomment the lines below
  330. # to see how your code behaves. Have fun!
  331.  
  332. # -----------------------------------------------------------------------------
  333. #
  334. #
  335. # Arguments:
  336. # network: The network you created with create_data_structure.
  337. #
  338. #
  339. # Return:
  340. # The most popular game in the network.
  341. def get_games(network):
  342. dictionary={}
  343. for keys in network:
  344. for games in network[keys][1]:
  345. if(games not in dictionary):
  346. dictionary[games]=1
  347. else:
  348. dictionary[games]+=1
  349. return get_max(dictionary)
  350.  
  351. def get_max(keys):
  352. maxi=0
  353. for values in keys:
  354. if (keys[values] > maxi):
  355. maxi=keys[values]
  356. most_played=values
  357.  
  358. return most_played
  359.  
  360. #------------------------------------------------------------------------------------
  361.  
  362. #print(example_input_alternate)
  363. #print(example_input)
  364. net = create_data_structure(example_input)
  365. #print net
  366. print path_to_friend(net, 'John', 'Ollie')
  367. #print get_connections(net, 'Freda')
  368. #print add_new_user(net, "Debra", [])
  369. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  370. #print get_connections(net, "Mercedes")
  371. #print add_connection(net, "John", "Freda")
  372. #print get_secondary_connections(net, "Mercedes")
  373. #print connections_in_common(net, "Mercedes", "John")
  374. #print get_games(net)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement