Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.99 KB | None | 0 0
  1. example_input="John is connected to Bryant, Debra, Walter.\
  2. John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\
  3. Bryant is connected to Olive, Ollie, Freda, Mercedes.\
  4. Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\
  5. Mercedes is connected to Walter, Robin, Bryant.\
  6. Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\
  7. Olive is connected to John, Ollie.\
  8. Olive likes to play The Legend of Corgi, Starfleet Commander.\
  9. Debra is connected to Walter, Levi, Jennie, Robin.\
  10. Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\
  11. Walter is connected to John, Levi, Bryant.\
  12. Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\
  13. Levi is connected to Ollie, John, Walter.\
  14. Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\
  15. Ollie is connected to Mercedes, Freda, Bryant.\
  16. Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\
  17. Jennie is connected to Levi, John, Freda, Robin.\
  18. Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\
  19. Robin is connected to Ollie.\
  20. Robin likes to play Call of Arms, Dwarves and Swords.\
  21. Freda is connected to Olive, John, Debra.\
  22. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures."
  23.  
  24. # -----------------------------------------------------------------------------
  25. # create_data_structure(string_input):
  26. # Parses a block of text (such as the one above) and stores relevant
  27. # information into a data structure. You are free to choose and design any
  28. # data structure you would like to use to manage the information.
  29. #
  30. # Arguments:
  31. # string_input: block of text containing the network information
  32. #
  33. # You may assume that for all the test cases we will use, you will be given the
  34. # connections and games liked for all users listed on the right-hand side of an
  35. # 'is connected to' statement. For example, we will not use the string
  36. # "A is connected to B.A likes to play X, Y, Z.C is connected to A.C likes to play X."
  37. # as a test case for create_data_structure because the string does not
  38. # list B's connections or liked games.
  39. #
  40. # The procedure should be able to handle an empty string (the string '') as input, in
  41. # which case it should return a network with no users.
  42. #
  43.  
  44. # Return:
  45. # The newly created network data structure
  46. def create_data_structure(string_input):
  47. """ This create the dictionary net from lists: users, connection, gaming"""
  48. network= {}
  49. net = []
  50. users= []
  51. connection= []
  52. gaming=[]
  53. word = "connected"
  54. game = "play"
  55. lines = example_input.split(".") #splits example_input into lines based on "."
  56. for line in lines: #loops through lines splits into words based on spaces
  57. words = line.split(" ")
  58. name = words[0] #get the first word in line
  59.  
  60. if name == "": #if name is empty skip to next;if the name is in user skip
  61. break #to next work; append if name is not in users list
  62. if name in users:
  63. pass
  64. else:
  65. users.append(name)
  66.  
  67. if word in words: #if word-connect is in words, get the position of word
  68. if word == "connected": #plus 2, this removes "is connected to";
  69. pos = words.index(word)+2 #join the words starting afte position of pos
  70. friend = ''.join(words[pos:]) #and split at the comma, append each element into connection list.
  71. friends = friend.split(',')
  72. connection.append(friends)
  73.  
  74. else:
  75. if word =="": # if word is empty move to next block
  76. break
  77. if line.find(" "+'play'+ " ") != -1: #find play in line, checking position is not -1(not there)
  78. foundit= line.index(game) # get the postion of game add 5 to position to
  79. newline= line[foundit+5:] #remove "likes to play",events splits word at the comma
  80. events = newline.split(', ') # appends events to gaming list
  81. gaming.append(events)
  82. else:
  83.  
  84.  
  85. if game =="": # if game is empty move to next block
  86. break
  87.  
  88. for i in range(len(users)): #loops through users, builds dictionary, appending
  89. network[users[i]] = connection[i], gaming[i] # each users their connection and gaming. Users is the key
  90.  
  91.  
  92. return network #return dictionary named network
  93.  
  94. # ----------------------------------------------------------------------------- #
  95. # Note that the first argument to all procedures below is 'network' This is the #
  96. # data structure that you created with your create_data_structure procedure, #
  97. # though it may be modified as you add new users or new connections. Each #
  98. # procedure below will then modify or extract information from 'network' #
  99. # ----------------------------------------------------------------------------- #
  100.  
  101. # -----------------------------------------------------------------------------
  102. # get_connections(network, user):
  103. # Returns a list of all the connections that user has
  104. #
  105. # Arguments:
  106. # network: the gamer network data structure
  107. # user: a string containing the name of the user
  108. #
  109. # Return:
  110. # A list of all connections the user has.
  111. # - If the user has no connections, return an empty list.
  112. # - If the user is not in network, return None.
  113. def get_connections(network, user):
  114. """ return a users connnections, if no connections return [] if users doesn't exist return none"""
  115.  
  116. for users in network: #loops through dictionary, if user is dictionary and
  117. try: # users connections is not empty, retrieve the connects
  118. if user == users:
  119. if len(network[users][0])> 0:
  120. connect= network[users][0]
  121. return connect
  122. else:
  123. return [] #return empty list if no connections
  124. except KeyError: #error handling if user does exist
  125. return None
  126.  
  127.  
  128.  
  129.  
  130. def get_gamer(network, user):
  131. """MYOP - return user connection and games like in network if they exist; if they dont return None"""
  132. for users in network:
  133. try: #loop through network,if user in network, return
  134. if user == users: #users their connection and games liked
  135. if len(network[users])>0:
  136. record = network[users], network[users][0], network[users][1]
  137. return record
  138. else:
  139. return []
  140. except KeyError: #error handling if user doesnt exist
  141. print "No record found"
  142.  
  143.  
  144.  
  145. # -----------------------------------------------------------------------------
  146. # get_games_liked(network, user):
  147. # Returns a list of all the games a user likes
  148. #
  149. # Arguments:
  150. # network: the gamer network data structure
  151. # user: a string containing the name of the user
  152. #
  153. # Return:
  154. # A list of all games the user likes.
  155. # - If the user likes no games, return an empty list.
  156. # - If the user is not in network, return None.
  157. def get_games_liked(network,user):
  158. """ return a users games liked, if no games liked return [] if users doesn't exist return none"""
  159. video= []
  160. for users in network: #loops through network, if user exist, and have some games
  161. try: # returns the liked games in list video
  162. if user == users:
  163. if len(network[users][1])> 0:
  164. video= network[users][1]
  165. return video
  166. else:
  167. return [] # return empty list if no games
  168. except KeyError: #error handling
  169. return None
  170.  
  171. # -----------------------------------------------------------------------------
  172. # add_connection(network, user_A, user_B):
  173. # Adds a connection from user_A to user_B. Make sure to check that both users
  174. # exist in network.
  175. #
  176. # Arguments:
  177. # network: the gamer network data structure
  178. # user_A: a string with the name of the user the connection is from
  179. # user_B: a string with the name of the user the connection is to
  180. #
  181. # Return:
  182. # The updated network with the new connection added.
  183. # - If a connection already exists from user_A to user_B, return network unchanged.
  184. # - If user_A or user_B is not in network, return False.
  185. def add_connection(network, user_A, user_B):
  186. """ Adds a connection if one does not exist; if either user is not in network return false, if connection
  187. already exist does nothing"""
  188.  
  189. if user_A in network== False: #if user A or B do not exist return false
  190. return network
  191. elif user_B in network==False:
  192. return network
  193. else:
  194. if user_B in network[user_A][0]: #if the user B is in user A connections return the dictionary
  195. return network
  196. else:
  197. network[user_A][0].append(user_B) #if user B is not in user A connections append it
  198. return network
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205. # -----------------------------------------------------------------------------
  206. # add_new_user(network, user, games):
  207. # Creates a new user profile and adds that user to the network, along with
  208. # any game preferences specified in games. Assume that the user has no
  209. # connections to begin with.
  210. #
  211. # Arguments:
  212. # network: the gamer network data structure
  213. # user: a string containing the name of the user to be added to the network
  214. # games: a list of strings containing the user's favorite games, e.g.:
  215. # ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']
  216. #
  217. # Return:
  218. # The updated network with the new user and game preferences added. The new user
  219. # should have no connections.
  220. # - If the user already exists in network, return network *UNCHANGED* (do not change
  221. # the user's game preferences)
  222. def add_new_user(network, user, games):
  223. """Adds new user and games liked to the network. If user existed return network unchanged"""
  224. gaming=(games)
  225. if user in network: #if user already exist return network
  226. return network
  227. else:
  228. users = user
  229. network[users]=([],games) #if user doesn't exist add user and games liked
  230. return network
  231.  
  232. # -----------------------------------------------------------------------------
  233. # get_secondary_connections(network, user):
  234. # Finds all the secondary connections (i.e. connections of connections) of a
  235. # given user.
  236. #
  237. # Arguments:
  238. # network: the gamer network data structure
  239. # user: a string containing the name of the user
  240. #
  241. # Return:
  242. # A list containing the secondary connections (connections of connections).
  243. # - If the user is not in the network, return None.
  244. # - If a user has no primary connections to begin with, return an empty list.
  245. #
  246. # NOTE:
  247. # It is OK if a user's list of secondary connections includes the user
  248. # himself/herself. It is also OK if the list contains a user's primary
  249. # connection that is a secondary connection as well.
  250. def get_secondary_connections(network, user):
  251. """return secondary connections from user's direct line connections"""
  252.  
  253.  
  254. if user in network== False: #if user is not in network return empty list
  255.  
  256. return []
  257. try:
  258.  
  259.  
  260. if len(network[user][0])==0: #if user connection is 0 return empty list
  261. return []
  262. if user in network: # is user in network
  263. for ntuser in network[user][0]: #loop through list of connection
  264.  
  265. n= get_connections(network, ntuser) #calls get_connections function to get secondary connections
  266.  
  267.  
  268. return n # subconnections
  269. else:
  270. return [] #return empty if no secondary connections
  271.  
  272. except KeyError: #error handling if user doesnt exist
  273. return None
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301. # -----------------------------------------------------------------------------
  302. # connections_in_common(network, user_A, user_B):
  303. # Finds the number of people that user_A and user_B have in common.
  304. #
  305. # Arguments:
  306. # network: the gamer network data structure
  307. # user_A: a string containing the name of user_A
  308. # user_B: a string containing the name of user_B
  309. #
  310. # Return:
  311. # The number of connections in common (as an integer).
  312. # - If user_A or user_B is not in network, return False.
  313. def connections_in_common(network, user_A, user_B):
  314. """Return the number of common connections two users have; if either user is not in network return false"""
  315. #a_conn =
  316. # b_conn=
  317. common=[]
  318. i= 0
  319. if user_A in network:
  320. a = get_connections(network, user_A) #if both user_A and user_B are in network, call get_connections
  321. #a_conn.append(a) #and return their connections append to list a_conn or b_conn
  322. if user_B in network:
  323. b = get_connections(network, user_B)
  324. #b_conn.append(b)
  325. #print b_conn
  326. for friend in a:
  327. #print friend #go through each friend in list a_conn if that friend is in
  328. if friend in b: #b_conn add 1 to i print i which gives nbr of common friends
  329. i=i+1
  330. return i
  331. return False
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342. # -----------------------------------------------------------------------------
  343. # path_to_friend(network, user_A, user_B):
  344. # Finds a connections path from user_A to user_B. It has to be an existing
  345. # path but it DOES NOT have to be the shortest path.
  346. #
  347. # Arguments:
  348. # network: The network you created with create_data_structure.
  349. # user_A: String holding the starting username ("Abe")
  350. # user_B: String holding the ending username ("Zed")
  351. #
  352. # Return:
  353. # A list showing the path from user_A to user_B.
  354. # - If such a path does not exist, return None.
  355. # - If user_A or user_B is not in network, return None.
  356. #
  357. # Sample output:
  358. # >>> print path_to_friend(network, "Abe", "Zed")
  359. # >>> ['Abe', 'Gel', 'Sam', 'Zed']
  360. # This implies that Abe is connected with Gel, who is connected with Sam,
  361. # who is connected with Zed.
  362. #
  363. # NOTE:
  364. # You must solve this problem using recursion!
  365. #
  366. # Hints:
  367. # - Be careful how you handle connection loops, for example, A is connected to B.
  368. # B is connected to C. C is connected to B. Make sure your code terminates in
  369. # that case.
  370. # - If you are comfortable with default parameters, you might consider using one
  371. # in this procedure to keep track of nodes already visited in your search. You
  372. # may safely add default parameters since all calls used in the grading script
  373. # will only include the arguments network, user_A, and user_B.
  374. def path_to_friend(network, user_A, user_B):
  375. dr= []
  376. if get_gamer(network, user_A) ==False: #calls get_gamer function see if user in dictionary
  377. return False
  378. if get_gamer(network, user_B) == False:
  379. return False
  380. else:
  381.  
  382.  
  383. return find_path(network, user_A, user_B, path=[]) # call recursive to find path
  384.  
  385. def find_path(network, begin, end, path=[]):
  386. """ finds the path of connection if level is deeper than first connection"""
  387. path= path + [begin] #create list to hold variablespatg
  388. if begin == end: # if user_A equal user_B return the path
  389. return [path]
  390. if end in network[begin][0]:
  391. return path +[end]
  392. #for name in network[begin][0]:
  393.  
  394.  
  395.  
  396.  
  397. #for person in network: #look in path to make sure person exist in network
  398. # if person not in path: #if person not in path
  399. # ext_paths= find_path(network, person, end, path) #cycle through calling find_path
  400. # if ext_paths: #once path is found return the extendedpath
  401. # return ext_paths
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423. # your RECURSIVE solution here!
  424.  
  425.  
  426. # Make-Your-Own-Procedure (MYOP)
  427. # -----------------------------------------------------------------------------
  428. # Your MYOP should either perform some manipulation of your network data
  429. # structure (like add_new_user) or it should perform some valuable analysis of
  430. # your network (like path_to_friend). Don't forget to comment your MYOP. You
  431. # may give this procedure any name you want.
  432.  
  433. # Replace this with your own procedure! You can also uncomment the lines below
  434. # to see how your code behaves. Have fun!
  435.  
  436. net = create_data_structure(example_input)
  437. network = create_data_structure(example_input)
  438. #print net
  439.  
  440. #print path_to_friend(net, "John", "Ollie")
  441. #print get_connections(net, "Debra")
  442. #print add_new_user(net, "Debra", [])
  443. #print add_new_user(net, "Nick", ["Seven Schemers", "The Movie: The Game"]) # True
  444. #print add_new_user(network, 'Alice',[])
  445. #print add_connection(network,'Alice','Bob')
  446. #print get_connections(net, "Mercedes")
  447. #print get_games_liked(net, "John")
  448. #print add_connection(net, "John", "Freda")
  449. #print get_secondary_connections(net, "Mercedes")
  450. #print connections_in_common(net, "Mercedes", "John")
  451. print network
  452. print path_to_friend(net, 'Alice', 'Bob')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement