From ac9c7c391940e31248cb06f02d0ba0216c3a4a3f Mon Sep 17 00:00:00 2001 From: Constance Shi Date: Tue, 22 Sep 2020 13:08:38 -0700 Subject: [PATCH 1/2] move import and fix naming conventions --- rewardsPointsSystem.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 70759ec..4f9d83b 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -33,13 +33,12 @@ - Customer 1 purchased 1 banana; he used 0 rewards points """ +from collections import defaultdict class Item: - def __init__(self, itemId, item_price): - self.itemId = itemId - self.item_price = item_price - -from collections import defaultdict + def __init__(self, item_id, item_price): + self.item_id = item_id + self.item_price = item_price class RewardsSystem: REWARDS_RATIO_BELOW = 18 From ad5cb8ad32ab0a6f16a35468932107d6b67a34b6 Mon Sep 17 00:00:00 2001 From: Constance Shi Date: Tue, 22 Sep 2020 14:16:53 -0700 Subject: [PATCH 2/2] fixing rest of file changes documented in pr --- rewardsPointsSystem.py | 63 ++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/rewardsPointsSystem.py b/rewardsPointsSystem.py index 4f9d83b..6ffaf1e 100644 --- a/rewardsPointsSystem.py +++ b/rewardsPointsSystem.py @@ -41,60 +41,45 @@ def __init__(self, item_id, item_price): self.item_price = item_price class RewardsSystem: - REWARDS_RATIO_BELOW = 18 + REWARDS_RATIO_BELOW_THRESHOLD = 18 + REWARDS_RATIO_ABOVE_THRESHOLD = 17 REWARDS_CUTOFF = 250 def __init__(self): self.rewards_points = defaultdict(int) self.items_purchased = defaultdict(int) + self.error=[] 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] - - 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) + purchase.item_price - - items_purchased = len(items_purchased) == 0 - if items_purchased: - raise ValueError('Items purchased were not recorded.') - + customer_id = log_entry.customer_id + reward_points_used = log_entry.reward_points_used + items_purchased = log_entry.items_purchased + if not items_purchased: + self.error.append(log_entry) else: - - # Subtract rewards points used from customer - self.rewards_points[customer_id] -= reward_points_used + if not reward_points_used: + reward_points_used = 0 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) + purchase.item_price - - print(self.reward_points) - + total_spent += purchase.item_price + # Update items sold + self.items_purchased[purchase.item_id] = self.items_purchased.get(purchase.item_id, 0) + 1 + + if customer_id: + self.rewards_points[customer_id] -= reward_points_used + amount_spent[customer_id] = amount_spent.get(customer_id, 0) + total_spent + # 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 + for customer_id in amount_spent.keys(): + # Update customer rewards points with rewards points received + if amount_spent[customer_id] > RewardsSystem.REWARDS_CUTOFF: + self.rewards_points[customer_id] += amount_spent[customer_id] // RewardsSystem.REWARDS_RATIO_ABOVE_THRESHOLD + else: + self.rewards_points[customer_id] += amount_spent[customer_id] // RewardsSystem.REWARDS_RATIO_BELOW_THRESHOLD def get_items_purchased(self, item_id): return self.items_purchased[item_id]