Created
January 17, 2020 08:07
-
-
Save jykim16/88496122c481d3a9a221d6d68fc88b49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#use: | |
#get_product_ranking('sample_data.tsv', 1) | |
import json | |
from collections import defaultdict | |
def get_product_ranking(file, rank_method=1): | |
# Outputs most popular product(s) based on rank method | |
# rank_method = 1 is based on the unique number of users who purchased each product | |
# rank_method = 2 is based on the total quantity of each product sold | |
rank_method_function_map = { | |
1: product_unique_purchases, | |
2: product_sold | |
} | |
method = rank_method_function_map[rank_method] | |
if not method: | |
return Error('choose a rank_method that is supported') | |
with open(file, 'r') as fd: | |
return method(fd) | |
def product_unique_purchases(fd): | |
product_to_unique_purchases = defaultdict(set) | |
top_products = set() | |
top_product_count = 0 | |
for line in fd: | |
data = json.loads(line.rstrip()) | |
current_product = data['product_id'] | |
product_to_unique_purchases[current_product].add(data['user_id']) | |
current_product_unique_purchases = len(product_to_unique_purchases[current_product]) | |
if current_product_unique_purchases == top_product_count: | |
top_products.add(current_product) | |
elif current_product_unique_purchases > top_product_count: | |
top_products = {current_product} | |
top_product_count = current_product_unique_purchases | |
print("Most popular product(s) based on the number of purchasers: {}".format(top_products)) | |
def product_sold(fd): | |
product_to_purchase_count = defaultdict(lambda :0) | |
top_products = set() | |
top_product_count = 0 | |
for line in fd: | |
data = json.loads(line.rstrip()) | |
current_product = data['product_id'] | |
quantity = data['quantity'] | |
product_to_purchase_count[current_product] += quantity | |
current_product_purchase_count = product_to_purchase_count[current_product] | |
if current_product_purchase_count == top_product_count: | |
top_products.add(current_product) | |
elif current_product_purchase_count > top_product_count: | |
top_product_count = current_product_purchase_count | |
top_products = {current_product} | |
print("Most popular product(s) based on the number of goods sold: {}".format(top_products)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment