Advertisement
Guest User

sidequest

a guest
Nov 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.32 KB | None | 0 0
  1. #
  2. # CS1010S --- Programming Methodology
  3. #
  4. # Sidequest 12.1 Template
  5. #
  6. # Note that written answers are commented out to allow us to run your
  7. # code easily while grading your problem set.
  8.  
  9. import json
  10.  
  11. # Reading json file
  12. def read_json(filename):
  13. """
  14. Reads a json file and returns a list of modules
  15. To find out more about json, please google ;)
  16.  
  17. For example, cs1010s-fbdata.json contains:
  18. {
  19. "members": {
  20. "data": [
  21. {
  22. "name": "Sunyoung Hwang",
  23. "id": "970478132994592"
  24. },
  25. {
  26. "name": "Nguy\u1ec5n Th\u1ea3o Ng\u00e2n",
  27. "id": "790182697716021"
  28. },
  29. {
  30. "name": "Tu Anh",
  31. "id": "10201625605746698"
  32. },
  33. ...
  34. ]
  35. },
  36. ...
  37. "description": "This is the official FB Group for CS1010S: Programming Methodology taught in Python.",
  38. "name": "CS1010S",
  39. "feed": {
  40. "data": [
  41. {
  42. "message": "Yay! :D \n\nhttps://www.facebook.com/saosin1994/posts/10204422037065788",
  43. "from": {
  44. "name": "Jeffrey Lee",
  45. "id": "718371011538995"
  46. },
  47. "name": "If you're new to coding, this is the programming language you should learn first",
  48. "id": "422428264540999_888704734580014",
  49. "likes": {
  50. "data": [
  51. {
  52. "id": "187291868313225",
  53. "name": "May Tan"
  54. },
  55. {
  56. "id": "10153290038157072",
  57. "name": "Goh See Ting"
  58. }
  59. ],
  60. ...
  61. }
  62. }
  63. ]
  64. }
  65. }
  66. """
  67. datafile = open(filename, 'r', encoding='utf-8')
  68. return json.loads(datafile.read())
  69.  
  70. # CS1010S Facebook Group Data as a dictionary object
  71. fb_data = read_json('cs1010s-fbdata.json')
  72.  
  73. ##########
  74. # Task a #
  75. ##########
  76.  
  77. def count_comments(data):
  78. result = 0
  79. for post in data["feed"]["data"]:
  80. if 'comments' in post.keys():
  81. result += len(post['comments']['data'])
  82. return result
  83.  
  84. print("Number of Comments in CS1010S: ", count_comments(fb_data))
  85.  
  86. ##########
  87. # Task b #
  88. ##########
  89.  
  90. def count_likes(data):
  91. # Returns the total number of likes (in feed posts and comments)
  92. result = 0
  93. for post in data["feed"]["data"]:
  94. if 'likes' in post.keys():
  95. result += len(post['likes']['data'])
  96. if 'comments' in post.keys():
  97. for comment in post['comments']['data']:
  98. result += comment['like_count']
  99. return result
  100.  
  101. print("Number of Likes in CS1010S: ", count_likes(fb_data))
  102.  
  103. ##########
  104. # Task c #
  105. ##########
  106.  
  107. def create_member_dict(data):
  108. # Lookup table where key is id and value is member data object
  109. members_dict = dict()
  110. for member in data["members"]["data"]:
  111. members_dict[member['id']] = dict([(key,value) for key, value in member.items() if key != 'id'])
  112. return members_dict
  113.  
  114. member_dict = create_member_dict(fb_data)
  115. #print(member_dict["10202689170610256"])
  116.  
  117. # Q: Why did we choose the id of the member data object to be the key?
  118. # A:
  119.  
  120. # Q: It is inappropriate to use the name as the key. What will happen if we use the name as the key of member_dict?
  121. # A:
  122.  
  123. ##########
  124. # Task d #
  125. ##########
  126.  
  127. def posts_freq(data):
  128. # Returns a dict where key is fb_id and value is number of posts in feed
  129. result = dict()
  130. for post in data["feed"]["data"]:
  131. if post['from']['id'] in result.keys():
  132. result[post['from']['id']] += 1
  133. else:
  134. result[post['from']['id']] = 1
  135. return result
  136.  
  137. #print("Posts Frequency: ", posts_freq(fb_data))
  138.  
  139. ##########
  140. # Task e #
  141. ##########
  142.  
  143. def comments_freq(data):
  144. # Returns a dict where key is fb_id and value is number of comments in feed
  145. result = dict()
  146. for post in data["feed"]["data"]:
  147. if 'comments' in post.keys():
  148. for comment in post['comments']['data']:
  149. if comment['from']['id'] in result.keys():
  150. result[comment['from']['id']] += 1
  151. else:
  152. result[comment['from']['id']] = 1
  153. return result
  154.  
  155. #print("Comments Frequency: ", comments_freq(fb_data))
  156.  
  157. ##########
  158. # Task f #
  159. ##########
  160.  
  161. def likes_freq(data):
  162. # Returns a dict where key is fb_id and value is number of likes in feed
  163. result = dict()
  164. for post in data["feed"]["data"]:
  165. if 'likes' in post.keys():
  166. for like in post['likes']['data']:
  167. if like['id'] in result.keys():
  168. result[like['id']] += 1
  169. else:
  170. result[like['id']] = 1
  171.  
  172. return result
  173.  
  174. # print("Likes Frequency: ", likes_freq(fb_data))
  175.  
  176. ##########
  177. # Task g #
  178. ##########
  179.  
  180. def popularity_score(data):
  181. # Returns a dict where key is fb_id and value is the number of likes
  182. # a person's posts and comments have
  183. result = dict()
  184. for post in data["feed"]["data"]:
  185. if 'likes' in post.keys():
  186. if post['from']['id'] in result.keys():
  187. result[post['from']['id']] += len(post['likes']['data'])
  188. else:
  189. result[post['from']['id']] = len(post['likes']['data'])
  190. if 'comments' in post.keys():
  191. for comment in post['comments']['data']:
  192. if comment['like_count'] != 0:
  193. if comment['from']['id'] in result.keys():
  194. result[comment['from']['id']] += comment['like_count']
  195. else:
  196. result[comment['from']['id']] = comment['like_count']
  197. return result
  198.  
  199. # print("Popularity Score: ", popularity_score(fb_data))
  200.  
  201. ##########
  202. # Task h #
  203. ##########
  204.  
  205. def member_stats(data):
  206. # Expand the member dict to include the keys:
  207. # 'posts_count', 'comments_count' and 'likes_count'
  208. post = posts_freq(data)
  209. cmt = comments_freq(data)
  210. likes = likes_freq(data)
  211. input_dict = create_member_dict(data)
  212. for member_id in input_dict.keys():
  213. if member_id in post.keys():
  214. input_dict[member_id]['posts_count'] = post[member_id]
  215. else:
  216. input_dict[member_id]['posts_count'] = 0
  217. if member_id in cmt.keys():
  218. input_dict[member_id]['comments_count'] = cmt[member_id]
  219. else:
  220. input_dict[member_id]['comments_count'] = 0
  221. if member_id in likes.keys():
  222. input_dict[member_id]['likes_count'] = likes[member_id]
  223. else:
  224. input_dict[member_id]['likes_count'] = 0
  225. return input_dict
  226.  
  227. stats = member_stats(fb_data)
  228. #print(stats["625742960877771"], '\n')
  229.  
  230. ##########
  231. # Task i #
  232. ##########
  233.  
  234. def activity_score(data):
  235. input_dict = member_stats(data)
  236. result = dict()
  237. for member_id in input_dict.keys():
  238. score = input_dict[member_id]['posts_count'] * 3 + input_dict[member_id]['comments_count'] * 2 + input_dict[member_id]['likes_count'] * 1
  239. result[member_id] = score
  240. return result
  241.  
  242. scores = activity_score(fb_data)
  243. #print(scores["806647796032627"]) # => 25
  244. #print(scores["773847432675142"]) # => 0
  245.  
  246.  
  247. ##########
  248. # Task j #
  249. ##########
  250.  
  251. def active_members_of_type(data, k, type_fn):
  252. # This is a higher order function, where type is a function and
  253. # can be either posts_freq, comments_freq, likes_freq, etc
  254. # and filters out the pairs that have frequency >= k
  255. result =[]
  256. members_dict = create_member_dict(data)
  257. output_type = list(type_fn(data).items())
  258. for mem in output_type:
  259. if mem[1] >= k and mem[0] in members_dict.keys():
  260. result.append([members_dict[mem[0]]['name'],mem[1]])
  261. result.sort()
  262. result.sort(key = lambda x: x[1], reverse = True)
  263. return result
  264.  
  265. print(active_members_of_type(fb_data, 2, posts_freq))
  266.  
  267. #print(active_members_of_type(fb_data, 20, comments_freq))
  268.  
  269. #print(active_members_of_type(fb_data, 40, likes_freq))
  270.  
  271. #print(active_members_of_type(fb_data, 20, popularity_score))
  272.  
  273. print(active_members_of_type(fb_data, 80, activity_score))
  274.  
  275.  
  276. ########### DO NOT REMOVE THE TEST BELOW ###########
  277.  
  278. def gradeit():
  279. print("\n*** Facebook Stalker Autograding ***")
  280. print('==================')
  281. answers = json.loads(open('grading.json', 'r', encoding='utf-8').read())
  282. total, correct = 0, 0
  283. def pass_or_fail(code, answer):
  284. nonlocal total
  285. total += 1
  286. if code == answer:
  287. nonlocal correct
  288. correct += 1
  289. return 'Passed!'
  290. else:
  291. return 'Failed.'
  292.  
  293. print('Testing count_comments... ', pass_or_fail(count_comments(fb_data), answers['count_comments']))
  294. print('Testing count_likes... ', pass_or_fail(count_likes(fb_data), answers['count_likes']))
  295. print('Testing create_member_dict... ', pass_or_fail(create_member_dict(fb_data), answers['create_member_dict']))
  296. print('Testing posts_freq... ', pass_or_fail(posts_freq(fb_data), answers['posts_freq']))
  297. print('Testing comments_freq... ', pass_or_fail(comments_freq(fb_data), answers['comments_freq']))
  298. print('Testing likes_freq... ', pass_or_fail(likes_freq(fb_data), answers['likes_freq']))
  299. print('Testing popularity_score... ', pass_or_fail(popularity_score(fb_data), answers['popularity_score']))
  300. print('Testing member_stats... ', pass_or_fail(member_stats(fb_data), answers['member_stats']))
  301. print('Testing activity_score... ', pass_or_fail(activity_score(fb_data), answers['activity_score']))
  302. print('Testing members with >= 1 posts... ', pass_or_fail(active_members_of_type(fb_data, 1, posts_freq), answers['active_posters']))
  303. print('Testing members with >= 4 comments... ', pass_or_fail(active_members_of_type(fb_data, 4, comments_freq), answers['active_commenters']))
  304. print('Testing members with >= 4 likes... ', pass_or_fail(active_members_of_type(fb_data, 4, likes_freq), answers['active_likers']))
  305. print('Testing members who have >= 3 likes... ', pass_or_fail(active_members_of_type(fb_data, 3, popularity_score), answers['popular']))
  306. print('Testing members with an activity score of >= 10... ', pass_or_fail(active_members_of_type(fb_data, 10, activity_score), answers['overall_active']))
  307. print('==================')
  308. print('Grades: ' + str(correct) + '/' + str(total) + '\n')
  309.  
  310. gradeit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement