1+ """
2+ Ejercicio
3+ """
4+ def apply_discount (prices , discount_function ):
5+ """Aplica un descuento a cada precio en una lista.
6+
7+ Args:
8+ prices: Una lista de precios.
9+
10+ discount_function: Una función que calcula el descuento.
11+ """
12+ discounted_prices = []
13+ for price in prices :
14+ discounted_price = discount_function (price )
15+ discounted_prices .append (discounted_price )
16+ return discounted_prices
17+
18+ def ten_percent_off (price ):
19+ return price * 0.9
20+
21+ def twenty_percent_off (price ):
22+ return price * 0.8
23+
24+ prices = [100 , 50 , 25 , 75 ]
25+
26+ discounted_prices_10 = apply_discount (prices , ten_percent_off )
27+ discounted_prices_20 = apply_discount (prices , twenty_percent_off )
28+
29+ print ("Original Prices:" , prices )
30+ print ("10% Off:" , discounted_prices_10 )
31+ print ("20% Off:" , discounted_prices_20 )
32+
33+
34+ """
35+ Dificultad extra
36+ """
37+
38+ import time
39+ import random
40+
41+ def process_order (dish_name , confirmation_callback , ready_callback , delivery_callback ):
42+ """Simula el procesamiento de un pedido de restaurante con callbacks.
43+
44+ Args:
45+ dish_name: El nombre del plato ordenado.
46+
47+ confirmation_callback: Función a llamar al confirmar el pedido.
48+
49+ ready_callback: Función a llamar cuando el plato esté listo.
50+
51+ delivery_callback: Función a llamar al entregar el pedido.
52+ """
53+ confirmation_callback (dish_name )
54+
55+ print (f"Preparing { dish_name } ..." )
56+ processing_time = random .randint (1 , 10 )
57+ time .sleep (processing_time )
58+
59+ ready_callback (dish_name )
60+
61+ print (f"Delivering { dish_name } ..." )
62+ time .sleep (random .randint (1 , 10 ))
63+
64+ delivery_callback (dish_name )
65+
66+ # Callback
67+ def on_confirmation (dish ):
68+ print (f"Order confirmed: { dish } " )
69+
70+ def on_ready (dish ):
71+ print (f"{ dish } is ready!" )
72+
73+ def on_delivery (dish ):
74+ print (f"{ dish } has been delivered. Enjoy!" )
75+
76+ # Simula una orden
77+ process_order (
78+ "Pizza" ,
79+ on_confirmation ,
80+ on_ready ,
81+ on_delivery
82+ )
0 commit comments