From 2a5839d38ef117d7168b7ac07fe914989ab64b8a Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:40:04 -0400 Subject: [PATCH 1/7] preliminary python autolint --- rewardsPointsSystem.py | 98 ++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 859d9f7..cc9c683 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -34,68 +34,74 @@ """ + +from collections import defaultdict + + class Item: - def __init__(self, itemId, item_price): - self.itemId = itemId - self.item_price = item_price + def __init__(self, itemId, item_price): + self.itemId = itemId + self.item_price = item_price -from collections import defaultdict class RewardsSystem: - REWARDS_RATIO_BELOW = 18 - REWARDS_CUTOFF = 250 + REWARDS_RATIO_BELOW = 18 + REWARDS_CUTOFF = 250 - def __init__(self): - self.rewards_points = defaultdict(int) - self.items_purchased = defaultdict(int) + def __init__(self): + self.rewards_points = defaultdict(int) + self.items_purchased = defaultdict(int) - def process_log(self, log): - amount_spent = defaultdict(int) + def process_log(self, log): + amount_spent = defaultdict(int) - for log_entry in log: - customer_id = log_entry[0] - reward_points_used = log_entry[1] - items_purchased = log_entry[2] + for log_entry in log: + customer_id = log_entry[0] + reward_points_used = log_entry[1] + items_purchased = log_entry[2] - if not customer_id: - total_spent = 0 - for item in items_purchased: - total_spent += item.itemId * item.item_price + if not customer_id: + total_spent = 0 + for item in items_purchased: + total_spent += item.itemId * item.item_price - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get(purchase.itemId, 0) + 1 + # Update items sold + for purchase in items_purchased: + self.items_purchased[purchase.itemId] = self.items_purchased.get( + purchase.itemId, 0) + 1 - items_purchased = len(items_purchased) == 0 - if items_purchased: - raise ValueError('Items purchased were not recorded.') + items_purchased = len(items_purchased) == 0 + if items_purchased: + raise ValueError('Items purchased were not recorded.') - else: + else: - # Subtract rewards points used from customer - self.rewards_points[customer_id] -= reward_points_used + # Subtract rewards points used from customer + self.rewards_points[customer_id] -= reward_points_used - total_spent = 0 - for item in items_purchased: - total_spent += item.itemId * item.item_price + total_spent = 0 + for item in items_purchased: + total_spent += item.itemId * item.item_price - amount_spent[customer_id] = amount_spent.get(customer_id, 0) + total_spent + amount_spent[customer_id] = amount_spent.get( + customer_id, 0) + total_spent - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get(purchase.itemId, 0) + 1 + # Update items sold + for purchase in items_purchased: + self.items_purchased[purchase.itemId] = self.items_purchased.get( + purchase.itemId, 0) + 1 - print(self.reward_points) + print(self.reward_points) - # At end of day, award reward points back to customers based on how much they spent - for customer_id in amount_spent: - # Calculate rewards points received - rewards_points = amount_spent[customer_id] // RewardsSystem.REWARDS_RATIO_BELOW - if amount_spent > RewardsSystem.REWARDS_CUTOFF: - rewards_points = amount_spent[customer_id] // 17 + # At end of day, award reward points back to customers based on how much they spent + for customer_id in amount_spent: + # Calculate rewards points received + rewards_points = amount_spent[customer_id] // RewardsSystem.REWARDS_RATIO_BELOW + if amount_spent > RewardsSystem.REWARDS_CUTOFF: + rewards_points = amount_spent[customer_id] // 17 - # Update customer rewards points - self.rewards_points[customer_id] += rewards_points + # Update customer rewards points + self.rewards_points[customer_id] += rewards_points - def get_items_purchased(self, item_id): - return self.items_purchased[item_id] + def get_items_purchased(self, item_id): + return self.items_purchased[item_id] From 88cd2c1dc5543f6db7af2018e88c4d89826d9c55 Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:44:13 -0400 Subject: [PATCH 2/7] Reduced comment line length to fit on screen --- rewardsPointsSystem.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index cc9c683..7b5be6b 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -1,6 +1,6 @@ """ -PROBLEM: Management needs to know how many of each item they are selling each +PROBLEM: Management needs to know how many of each item they are selling each day and what each customer’s current reward points balance is. Design a system that parses the log file and: @@ -11,7 +11,10 @@ - For each purchase, a customer can spend a certain amount of reward points for their purchase. - Some customers come to the store multiple times per day. -- Customers earn 1 reward point for every 18 dollars spent, unless they spend more than $250 per day, in which case they earn 1 reward point for every 17 dollars spent. These reward points are given to the customers at the **end** of each day, not immediately after the purchase. +- Customers earn 1 reward point for every 18 dollars spent, + unless they spend more than $250 per day, in which case they earn + 1 reward point for every 17 dollars spent. These reward points are given + to the customers at the **end** of each day, not immediately after the purchase. - For each LogEntry, we want to keep track of: - Customer ID - Number of reward points used for that purchase @@ -19,9 +22,11 @@ There can also be malformed logs in which any of the three properties in the LogEntry are 'None'. If this is the case, please address them in the following ways: -- If there is no customer ID, do not count the rewards points. Instead, only calculate the purchase counts for the items +- If there is no customer ID, do not count the rewards points. + Instead, only calculate the purchase counts for the items - If there is no rewards points specified, assume rewards points = 0 -- If there is no list of items purchased, then this is an error in the system. Please add this LogEntry to a error log (a list of LogEntry called 'error') +- If there is no list of items purchased, then this is an error in the system. + Please add this LogEntry to a error log (a list of LogEntry called 'error') Example- this is written in casual terms, and must be modified to actual data structures Items: @@ -31,7 +36,6 @@ - Customer 1 purchased 2 bananas and 1 apple; he used 100 rewards points - Customer 2 purchased 1 banana and 1 apple; she used 0 rewards points - Customer 1 purchased 1 banana; he used 0 rewards points - """ From f001cc23c8be549b3553b13213f8982dad05190c Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:47:48 -0400 Subject: [PATCH 3/7] Added default value for points used --- rewardsPointsSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 7b5be6b..05ae381 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -61,7 +61,7 @@ def process_log(self, log): for log_entry in log: customer_id = log_entry[0] - reward_points_used = log_entry[1] + reward_points_used = log_entry[1] or 0 items_purchased = log_entry[2] if not customer_id: From 8ce4721c0d6bd79a9b54003eaeb4c0cae5986530 Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:52:13 -0400 Subject: [PATCH 4/7] Removed redundant if else --- rewardsPointsSystem.py | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 05ae381..50999d2 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -22,10 +22,10 @@ There can also be malformed logs in which any of the three properties in the LogEntry are 'None'. If this is the case, please address them in the following ways: -- If there is no customer ID, do not count the rewards points. +- If there is no customer ID, do not count the rewards points. Instead, only calculate the purchase counts for the items - If there is no rewards points specified, assume rewards points = 0 -- If there is no list of items purchased, then this is an error in the system. +- If there is no list of items purchased, then this is an error in the system. Please add this LogEntry to a error log (a list of LogEntry called 'error') Example- this is written in casual terms, and must be modified to actual data structures @@ -64,38 +64,20 @@ def process_log(self, log): reward_points_used = log_entry[1] or 0 items_purchased = log_entry[2] - if not customer_id: - total_spent = 0 - for item in items_purchased: - total_spent += item.itemId * item.item_price - - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get( - purchase.itemId, 0) + 1 - - items_purchased = len(items_purchased) == 0 - if items_purchased: - raise ValueError('Items purchased were not recorded.') - - else: - + if customer_id: # Subtract rewards points used from customer self.rewards_points[customer_id] -= reward_points_used - - total_spent = 0 - for item in items_purchased: - total_spent += item.itemId * item.item_price - amount_spent[customer_id] = amount_spent.get( customer_id, 0) + total_spent - # Update items sold - for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get( - purchase.itemId, 0) + 1 + total_spent = 0 + for item in items_purchased: + total_spent += item.itemId * item.item_price - print(self.reward_points) + # Update items sold + for purchase in items_purchased: + self.items_purchased[purchase.itemId] = self.items_purchased.get( + purchase.itemId, 0) + 1 # At end of day, award reward points back to customers based on how much they spent for customer_id in amount_spent: From c0b73b8b95e2c1baff2de603d42efa487b9a6af6 Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:55:03 -0400 Subject: [PATCH 5/7] Changed function name -> more reflective of usage --- rewardsPointsSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 50999d2..68011da 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -89,5 +89,5 @@ def process_log(self, log): # Update customer rewards points self.rewards_points[customer_id] += rewards_points - def get_items_purchased(self, item_id): + def get_purchased_item_by_id(self, item_id): return self.items_purchased[item_id] From 335ab2db79bef0ac9ae0b62b377d67b2454ba634 Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:56:08 -0400 Subject: [PATCH 6/7] Refactored fn to use default values --- rewardsPointsSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 68011da..6248d58 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -90,4 +90,4 @@ def process_log(self, log): self.rewards_points[customer_id] += rewards_points def get_purchased_item_by_id(self, item_id): - return self.items_purchased[item_id] + return self.items_purchased.get(item_id, 0) From 099692d3fc80ce9ed76c2577621034db3245a10c Mon Sep 17 00:00:00 2001 From: Tim Zheng Date: Wed, 30 Sep 2020 12:57:31 -0400 Subject: [PATCH 7/7] Refactored to use method Fixed typoo --- rewardsPointsSystem.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 6248d58..99ad230 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -56,6 +56,9 @@ def __init__(self): self.rewards_points = defaultdict(int) self.items_purchased = defaultdict(int) + def get_purchased_item_by_id(self, item_id): + return self.items_purchased.get(item_id, 0) + def process_log(self, log): amount_spent = defaultdict(int) @@ -76,8 +79,8 @@ def process_log(self, log): # Update items sold for purchase in items_purchased: - self.items_purchased[purchase.itemId] = self.items_purchased.get( - purchase.itemId, 0) + 1 + self.items_purchased[purchase.itemId] = self.get_purchased_item_by_id( + purchase.itemId) + 1 # At end of day, award reward points back to customers based on how much they spent for customer_id in amount_spent: @@ -88,6 +91,3 @@ def process_log(self, log): # Update customer rewards points self.rewards_points[customer_id] += rewards_points - - def get_purchased_item_by_id(self, item_id): - return self.items_purchased.get(item_id, 0)