Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raymond's Car makes all code beautiful. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 28 additions & 38 deletions rewardsPointsSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,69 +33,59 @@
- 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
def __init__(self, _id, item_price):
self._id = _id
self.item_price = item_price

from collections import defaultdict

class RewardsSystem:
REWARDS_RATIO_BELOW = 18
REWARDS_RATIO_BELOW_CUTOFF = 18
REWARDS_RATIO_ABOVE_CUTOFF = 17
REWARDS_CUTOFF = 250

def __init__(self):
self.rewards_points = defaultdict(int)
self.items_purchased = defaultdict(int)

def get_items_purchased(self, item_id):
return self.items_purchased[item_id]

def process_log(self, log):
amount_spent = defaultdict(int)
customer_to_amount_spent = defaultdict(int)

# First, process all logs
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) + 1

items_purchased = len(items_purchased) == 0
if items_purchased:
raise ValueError('Items purchased were not recorded.')

else:
if not items_purchased:
# Empty list of purchases, error!
raise ValueError('Items purchased were not recorded.')

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
customer_to_amount_spent[customer_id] += item.item_price

# 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)
# Update purchase counts by item
for purchase in items_purchased:
self.items_purchased[purchase._id] += 1

# 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

for customer_id, amount in customer_to_amount_spent.items():

# Determine rewards ratio to use
if amount > RewardsSystem.REWARDS_CUTOFF:
ratio = RewardsSystem.REWARDS_RATIO_ABOVE_CUTOFF
else:
ratio = RewardsSystem.REWARDS_RATIO_BELOW_CUTOFF

# Update customer rewards points
self.rewards_points[customer_id] += rewards_points
self.rewards_points[customer_id] += amount // ratio

def get_items_purchased(self, item_id):
return self.items_purchased[item_id]