Skip to content

Commit 3dc6075

Browse files
#21 - python
1 parent 0858cbc commit 3dc6075

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#21 { Retos para Programadores } CALLBACKS
2+
3+
# Bibliography reference
4+
#Secrets of the JavaScript Ninja (John Resig, Bear Bibeault, Josip Maras) (z-lib.org)
5+
#Eloquent Javascript A Modern Introduction to Programming by Marijn Haverbeke (z-lib.org)
6+
#Python Notes for Professionals. 800+ pages of professional hints and tricks (GoalKicker.com) (Z-Library)
7+
# Additionally, I use GPT as a reference and sometimes to correct or generate proper comments.
8+
9+
"""
10+
* EJERCICIO:
11+
* Explora el concepto de callback en tu lenguaje creando un ejemplo
12+
* simple (a tu elección) que muestre su funcionamiento.
13+
*
14+
* DIFICULTAD EXTRA (opcional):
15+
* Crea un simulador de pedidos de un restaurante utilizando callbacks.
16+
* Estará formado por una función que procesa pedidos.
17+
* Debe aceptar el nombre del plato, una callback de confirmación, una
18+
* de listo y otra de entrega.
19+
* - Debe imprimir un confirmación cuando empiece el procesamiento.
20+
* - Debe simular un tiempo aleatorio entre 1 a 10 segundos entre
21+
* procesos.
22+
* - Debe invocar a cada callback siguiendo un orden de procesado.
23+
* - Debe notificar que el plato está listo o ha sido entregado.
24+
25+
"""
26+
27+
import random
28+
import threading
29+
30+
# Function to simulate console.log
31+
log = print
32+
33+
log('Retosparaprogramadores #21.')
34+
35+
# Function to confirm an order
36+
def order_confirm(order, callback):
37+
log(f'The order: {order} is confirmed. Tell you when it\'s ready.')
38+
processing_time = random.randint(1, 10) # Random processing time between 1 and 10 seconds
39+
threading.Timer(processing_time, callback, args=[order]).start()
40+
41+
# Function to indicate that the order is ready
42+
def order_ready(order, callback):
43+
log(f'The order: {order} is ready. Waiting to deliver.')
44+
delivery_time = random.randint(1, 10) # Random delivery time between 1 and 10 seconds
45+
threading.Timer(delivery_time, callback, args=[order]).start()
46+
47+
# Function to deliver the order
48+
def order_deliver(order):
49+
log(f'Order: {order} delivered.')
50+
51+
# Function to make an order
52+
def make_order(list_order):
53+
today_menu = ['pizza', 'hamburger', 'paella', 'arabian food', 'posole',
54+
'pastel azteca', 'carbonara past', 'napolitana past', 'fish', 'beef']
55+
56+
for order in list_order:
57+
if any(elm.lower() == order.lower() for elm in today_menu):
58+
order_confirm(order, lambda confirmed_order: order_ready(confirmed_order, order_deliver))
59+
else:
60+
log(f'The order: {order} is not in today\'s menu. Please choose another dish.')
61+
62+
63+
64+
order_list1 = ['pastiche', 'hamburger', 'pizza']
65+
order_list2 = ['arabian food', 'fish', 'pastel azteca']
66+
make_order(order_list1)
67+
make_order(order_list2)
68+
69+
# Output Example:
70+
"""
71+
Retosparaprogramadores #21.
72+
The order: pastiche is not in today's menu. Please choose another dish.
73+
The order: hamburger is confirmed. Tell you when it's ready.
74+
The order: pizza is confirmed. Tell you when it's ready.
75+
The order: arabian food is confirmed. Tell you when it's ready.
76+
The order: fish is confirmed. Tell you when it's ready.
77+
The order: pastel azteca is confirmed. Tell you when it's ready.
78+
The order: hamburger is ready. Waiting to deliver.
79+
The order: fish is ready. Waiting to deliver.
80+
Order: fish delivered.
81+
The order: arabian food is ready. Waiting to deliver.
82+
The order: pastel azteca is ready. Waiting to deliver.
83+
Order: hamburger delivered.
84+
The order: pizza is ready. Waiting to deliver.
85+
Order: pastel azteca delivered.
86+
Order: arabian food delivered.
87+
Order: pizza delivered.
88+
89+
"""

0 commit comments

Comments
 (0)