Python 실습 18: 판매 시즌
의문
--
예시
예를 들어 고객이 a, b, c의 세 가지 제품을 받는 경우:
제품 A
제품 B
제품 C
$15.99
$23.50
$23.50
내 솔루션
>>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
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
기타 솔루션
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]
내 반성
신용 거래
챌린지 찾기edabit
Reference
이 문제에 관하여(Python 실습 18: 판매 시즌), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mathewchan/python-exercise-18sales-season-198b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)