Question
- A retailer is having a store-wide "buy 2, get 1 free" sale.
- For legal reasons, they can't charge their customers $0 for an article
- so a discount is applied to all products instead. - --
- Create a function that takes in a list of prices for a customer's shopping cart
- and returns the prices with the discount applied.
- Round all prices to the cent.
Example
For example, if a customer gets three products a, b and c:
Product A | Product B | Product C |
---|---|---|
$15.99 | $23.50 | $23.50 |
She gets the cheapest one for free, so she ends up paying $15.99 + $23.50 = $39.49, but what her receipt says is:
Product A: $15.99 − Special Discount = $12.57
Product B: $23.50 − Special Discount = $18.47
Product C: $10.75 − Special Discount = $8.45
Total: $39.49
My solution
- algorithm
>>calculate the original price of all the products
initialise a variable called original_total
original_total = sum of the shopping cart
>>calculate the discount given in percentage
>>>determine how many product should be free
free_product_number = length of shopping cart // 3
>>>calculate the total price of the free product
>>>>find the lowest (numbers_of_free_product) price in the shopping cart
initialise a free_product list
while length of free_product list is not equal to free_product_number:
for product in shopping cart:
lowest_product = min(shopping cart)
add the lowest_product to free_product list
delete the lowest_product
initialise a variable called discount_given
discount_given = sum of the free_product list
>>>calculate the discount percentage
discount percentage = 1-(discount_given / original_total)
>>return the list of all product price with discounted applied
initialise a discounted_list
for each product in the shopping cart:
new_price = product * discount percentage
then round the new_price to 2 decimal place
store the new_price to the discounted list
return the new_list
- code
def discount(shopping_cart: list):
copy_of_shopping_cart = shopping_cart.copy()
# >>calculate the original price of all the products
original_total = sum(shopping_cart)
free_product_number = len(shopping_cart) // 3
# >>>calculate the total price of the free product
# find the lowest (numbers_of_free_product) product in the shopping cart free_product_list = list()
# set up a loop for free_product_number times:
while len(free_product_list) < free_product_number:
free_product = min(copy_of_shopping_cart)
free_product_list.append(free_product)
# remove the lowest product in the shopping cart, so the next lowest product can be found
copy_of_shopping_cart.remove(free_product)
# calculate the total prise of the free item
discount_given = sum(free_product_list)
discount_percentage = 1 - (discount_given / original_total)
# >>return the list of all product price with discounted applied
discounted_list = list()
# applied discount to each product in the original list , round to 2 decimal place and add to discounted_list
for product in shopping_cart:
discounted_product = product * discount_percentage
discounted_list.append(round(discounted_product, 2))
return discounted_list
Other solution
def discount(lst):
free = len(lst) // 3
# sorted the list in accedning order, and calculate the sum of the non_free item start from the index after the free item.
pct = sum(sorted(lst)[free:]) / sum(lst)
return [round(x*pct, 2) for x in lst]
My reflection
- I have learned a new way to solve the find lowest item in the list by using sorted()function and slicing method
- seem like my code are very long compare to other, need to practice more the shorten code.
Credit
challenge find on edabit
Top comments (0)