Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import csv
  2. import json
  3.  
  4.  
  5. def f():
  6. map_by_prod = {}
  7.  
  8. with open('SWE sample data - Q2 data.csv', 'r') as csv_file:
  9. lines = csv.reader(csv_file, delimiter=' ', quotechar='"')
  10. for row in lines:
  11. # print(row[0])
  12. line = json.loads(row[0])
  13.  
  14. # by num of unique user
  15. if map_by_prod.get(line['product_id'], None) is None:
  16. map_by_prod[line['product_id']] = {
  17. "num_user": {line['user_id']},
  18. "quantity": line['quantity']
  19. }
  20. else:
  21. map_by_prod[line['product_id']]["num_user"].add(line['user_id'])
  22. map_by_prod[line['product_id']]["quantity"] += line['quantity']
  23.  
  24. best_prod_by_num_user = ("", 0)
  25. best_prod_by_quantity = ("", 0)
  26.  
  27. # sort takes nlog(n) time, might as well loop through it
  28. for (k, v) in map_by_prod.items():
  29. if v["quantity"] > best_prod_by_quantity[1]:
  30. best_prod_by_quantity = (k, v["quantity"])
  31.  
  32. if len(v["num_user"]) > best_prod_by_num_user[1]:
  33. best_prod_by_num_user = (k, len(v["num_user"]))
  34.  
  35. return (
  36. "Most popular product(s) based on the number of purchasers: {0} with {1} unique users."
  37. .format(best_prod_by_num_user[0], best_prod_by_num_user[1]),
  38. "Most popular product(s) based on the quantity of goods sold: {0} with {1} items sold."
  39. .format(best_prod_by_quantity[0], best_prod_by_quantity[1])
  40. )
  41.  
  42.  
  43. print(f())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement