Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.48 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. # Return:
  87. # The newly created network data structure
  88. def create_data_structure(string_input):
  89. # Define Variables
  90. network = {} #this will house the names:{connections:[1],[2],games:[1],[2]} setup
  91. username = ""
  92. trimmed_games_list, secondary_connections = [],[]
  93. string_start, string_end = 0,0
  94. ict_char_count = len(" is connected to ")
  95. ltp_char_count = len(" likes to play ")
  96. current_sentence = ""
  97. temp_string = string_input #leaves original string intact and creates a temp string for truncating
  98. string_end = temp_string.find(".") #finds end of first sentence
  99.  
  100. # Code for a blank string
  101. if string_input == "":
  102. return network
  103.  
  104. #begins the loop to split off usersnames, connections, and games
  105. while string_end <> -1: #do this loop until you cannot find any more periods (".")
  106. current_sentence = temp_string[0:string_end]
  107. # #splits off username and creates entry in dictionary
  108. username = current_sentence[0:temp_string.find(" ")]
  109. network[username] = {"games": ["1"], "connections": ["2"]}
  110.  
  111. # creates a string of just names of friends to be broken down by split_string
  112. current_sentence = current_sentence[len(username)+ict_char_count:] #takes out name and filler words
  113. current_sentence = current_sentence.replace(' ','')
  114. connections_list = split_string(current_sentence, ",")
  115. network[username]["connections"] = connections_list
  116.  
  117. # creates a string of just games to be broken down by split_string
  118. string_start = string_end + 1
  119. temp_string = temp_string[string_start:]
  120. string_end = temp_string.find(".")
  121.  
  122. current_sentence = temp_string[0:temp_string.find('.')]
  123. current_sentence = current_sentence[len(username)+ltp_char_count:]
  124. games_list = split_string(current_sentence, ",")
  125. for element in games_list: #takes out the spaces that come over at the beginning of movie titles
  126. trimmed_games_list.append(element.strip())
  127. network[username]["games"] = trimmed_games_list
  128. trimmed_games_list = [] #empty the list after it has been transferred
  129.  
  130. # #once the sentence has been broken down, this adjusts the numbers for the next iteration
  131. string_start = string_end + 1
  132. temp_string = temp_string[string_start:]
  133. string_end = temp_string.find(".")
  134. return network
  135.  
  136. # ----------------------------------------------------------------------------- #
  137. # Note that the first argument to all procedures below is 'network' This is the #
  138. # data structure that you created with your create_data_structure procedure, #
  139. # though it may be modified as you add new users or new connections. Each #
  140. # procedure below will then modify or extract information from 'network' #
  141. # ----------------------------------------------------------------------------- #
  142.  
  143. # -----------------------------------------------------------------------------
  144. # get_connections(network, user):
  145. #
  146. #
  147. # Arguments:
  148. # network: the gamer network data structure
  149. # user: a string containing the name of the user
  150. #
  151. # Return:
  152. # A list of all connections the user has.
  153. # - If the user has no connections, return an empty list.
  154. # - If the user is not in network, return None.
  155. def get_connections(network, user):
  156. if user not in network:
  157. return None
  158. if network[user]["connections"] == []:
  159. return []
  160. return network[user]["connections"]
  161.  
  162. # -----------------------------------------------------------------------------
  163. # get_games_liked(network, user):
  164. # Returns a list of all the games a user likes
  165. #
  166. # Arguments:
  167. # network: the gamer network data structure
  168. # user: a string containing the name of the user
  169. #
  170. # Return:
  171. # A list of all games the user likes.
  172. # - If the user likes no games, return an empty list.
  173. # - If the user is not in network, return None.
  174. def get_games_liked(network,user):
  175. if user not in network:
  176. return None
  177. if network[user]["games"] == []:
  178. return []
  179. return network[user]["games"]
  180.  
  181. # -----------------------------------------------------------------------------
  182. # add_connection(network, user_A, user_B):
  183. # Adds a connection from user_A to user_B. Make sure to check that both users
  184. # exist in network.
  185. #
  186. # Arguments:
  187. # network: the gamer network data structure
  188. # user_A: a string with the name of the user the connection is from
  189. # user_B: a string with the name of the user the connection is to
  190. #
  191. # Return:
  192. # The updated network with the new connection added.
  193. # - If a connection already exists from user_A to user_B, return network unchanged.
  194. # - If user_A or user_B is not in network, return False.
  195. def add_connection(network, user_A, user_B):
  196. if user_A not in network:
  197. return False
  198. if user_B not in network:
  199. return False
  200. if user_B in network[user_A]["connections"]:
  201. return network
  202. else:
  203. network[user_A]["connections"].append(user_B)
  204. return network
  205.  
  206. # -----------------------------------------------------------------------------
  207. # add_new_user(network, user, games):
  208. # Creates a new user profile and adds that user to the network, along with
  209. # any game preferences specified in games. Assume that the user has no
  210. # connections to begin with.
  211. #
  212. # Arguments:
  213. # network: the gamer network data structure
  214. # user: a string containing the name of the user to be added to the network
  215. # games: a list of strings containing the user's favorite games, e.g.:
  216. # ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  217. #
  218. # Return:
  219. # The updated network with the new user and game preferences added. The new user
  220. # should have no connections.
  221. # - If the user already exists in network, return network *UNCHANGED* (do not change
  222. # the user's game preferences)
  223.  
  224. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  225. #
  226. def add_new_user(network, user, games):
  227. if user in network:
  228. return network
  229. if user not in network:
  230. network[user] = {"games": games, "connections": []}
  231. return network
  232.  
  233. # -----------------------------------------------------------------------------
  234. # get_secondary_connections(network, user):
  235. # Finds all the secondary connections (i.e. connections of connections) of a
  236. # given user.
  237. #
  238. # Arguments:
  239. # network: the gamer network data structure
  240. # user: a string containing the name of the user
  241. #
  242. # Return:
  243. # A list containing the secondary connections (connections of connections).
  244. # - If the user is not in the network, return None.
  245. # - If a user has no primary connections to begin with, return an empty list.
  246. #
  247. # NOTE:
  248. # It is OK if a user's list of secondary connections includes the user
  249. # himself/herself. It is also OK if the list contains a user's primary
  250. # connection that is a secondary connection as well.
  251. def get_secondary_connections(network, user):
  252. if user not in network:
  253. return None
  254. secondary_connections_list = []
  255. icount = len(network[user]["connections"])
  256. if icount > 0:
  257. for downstream_connections in network[user]["connections"]:
  258. for secondary_connections in network[downstream_connections]["connections"]:
  259. if secondary_connections not in secondary_connections_list:
  260. secondary_connections_list.append(secondary_connections)
  261. return secondary_connections_list
  262. if icount == 0:
  263. return []
  264. # -----------------------------------------------------------------------------
  265. # connections_in_common(network, user_A, user_B):
  266. # Finds the number of people that user_A and user_B have in common.
  267. #
  268. # Arguments:
  269. # network: the gamer network data structure
  270. # user_A: a string containing the name of user_A
  271. # user_B: a string containing the name of user_B
  272. #
  273. # Return:
  274. # The number of connections in common (as an integer).
  275. # - If user_A or user_B is not in network, return False.
  276. def connections_in_common(network, user_A, user_B):
  277. icount = 0
  278. A_connections, B_connections = [],[]
  279. if user_A not in network or user_B not in network:
  280. return False
  281. A_connections = network[user_A]["connections"]
  282. B_connections = network[user_B]["connections"]
  283. for name in A_connections:
  284. if name in B_connections:
  285. icount = icount + 1
  286. return icount
  287.  
  288. # -----------------------------------------------------------------------------
  289. # path_to_friend(network, user_A, user_B):
  290. # Finds a connections path from user_A to user_B. It has to be an existing
  291. # path but it DOES NOT have to be the shortest path.
  292. #
  293. # Arguments:
  294. # network: The network you created with create_data_structure.
  295. # user_A: String holding the starting username ("Abe")
  296. # user_B: String holding the ending username ("Zed")
  297. #
  298. # Return:
  299. # A list showing the path from user_A to user_B.
  300. # - If such a path does not exist, return None.
  301. # - If user_A or user_B is not in network, return None.
  302. #
  303. # Sample output:
  304. # >>> print path_to_friend(network, "Abe", "Zed")
  305. # >>> ['Abe', 'Gel', 'Sam', 'Zed']
  306. # This implies that Abe is connected with Gel, who is connected with Sam,
  307. # who is connected with Zed.
  308. #
  309. # NOTE:
  310. # You must solve this problem using recursion!
  311. #
  312. # Hints:
  313. # - Be careful how you handle connection loops, for example, A is connected to B.
  314. # B is connected to C. C is connected to B. Make sure your code terminates in
  315. # that case.
  316. # - If you are comfortable with default parameters, you might consider using one
  317. # in this procedure to keep track of nodes already visited in your search. You
  318. # may safely add default parameters since all calls used in the grading script
  319. # will only include the arguments network, user_A, and user_B.
  320. def path_to_friend(network, user_A, user_B, path = None):
  321. if path is None:
  322. path = []
  323. if user_A not in network:
  324. return None
  325. if user_B not in network:
  326. return None
  327. if user_B in network[user_A]["connections"]:
  328. return [user_A, user_B]
  329. path.append(user_A)
  330. for people in network[user_A]["connections"]:
  331. if people not in path:
  332. if path_to_friend(network, people, user_B) != None:
  333. return [user_A] + path_to_friend(network, people, user_B)
  334.  
  335.  
  336.  
  337.  
  338.  
  339. # Make-Your-Own-Procedure (MYOP)
  340. def split_string(source,splitlist):
  341. holder, answer, split2 = [], [],[]
  342. i,a,b,z = 0,0,0,0
  343. goodtransfer = ""
  344. badtransfer = ""
  345.  
  346. while i < len(splitlist):
  347. split2.append(splitlist[i])
  348. i = i + 1
  349.  
  350. i = 0
  351. while i < len(source):
  352. for element in split2:
  353. if source[i] == element and z == 0:
  354. badtransfer = badtransfer + source[i]
  355. if goodtransfer <> "":
  356. answer.append(goodtransfer)
  357. goodtransfer = ""
  358. z = 1
  359. if z == 0:
  360. goodtransfer = goodtransfer + source[i]
  361. z = 0
  362. source = source[1:]
  363. if goodtransfer <> "":
  364. answer.append(goodtransfer)
  365. return answer
  366.  
  367. def MYOP_count_connections(network, user):
  368. return len(network[user]["connections"])
  369.  
  370. # -----------------------------------------------------------------------------
  371. # Your MYOP should either perform some manipulation of your network data
  372. # structure (like add_new_user) or it should perform some valuable analysis of
  373. # your network (like path_to_friend). Don't forget to comment your MYOP. You
  374. # may give this procedure any name you want.
  375.  
  376.  
  377. net = create_data_structure(example_input)
  378. #print MYOP_count_connections(net, "Walter")
  379. #print net
  380. print path_to_friend(net, "John", "Ollie")
  381. #print get_connections(net, "Debra")
  382. #print add_new_user(net, "Debra", [])
  383. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  384. #print get_connections(net, "Mercedes")
  385. #print get_games_liked(net, "John")
  386. #print add_connection(net, "John", "Freda")
  387. #print get_secondary_connections(net, "Mercedes")
  388. #print connections_in_common(net, "Mercedes", "John")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement