לדלג לתוכן

3.3 למבדה, דקורטור, וגנרטור פתרון

ניתוח מחירי מוצרים

def add_currency_symbol(func):
    def wrapper(price):
        return func(price) + " $"
    return wrapper

@add_currency_symbol
def format_price(price):
    return str(price)

def main():
    product_prices = [55, 20, 35, 45, 60, 25]

    # 1. Apply a 10% discount to each product price
    discounted_prices = list(map(lambda x: x * 0.9, product_prices))

    # 2. Extract only the discounted prices below $50
    filtered_prices = list(filter(lambda x: x < 50, discounted_prices))

    # 3. Sort the discounted prices in ascending order
    sorted_prices = sorted(filtered_prices)

    # 4. Decorate with currency symbol
    formatted_prices = [format_price(price) for price in sorted_prices]

    # 5. Print formatted prices
    print("Discounted Prices:")
    for price in formatted_prices:
        print(price)

if __name__ == "__main__":
    main()

יוצר מספרים ראשונים

def is_prime(number):  
    for i in range(2, number):  
        if number % i == 0:  
            return False  
    return True

def generate_prime_numbers(size):
    for number in range(size):
        if is_prime(number):
            yield number

for n in generate_prime_numbers(10000)
    print(n)