Skip to content

Commit 727ff29

Browse files
authored
Merge pull request #7609 from davidrguez98/main
#21 - Python
2 parents 009b18d + b3e8538 commit 727ff29

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
""" /*
2+
* EJERCICIO:
3+
* Explora el concepto de callback en tu lenguaje creando un ejemplo
4+
* simple (a tu elección) que muestre su funcionamiento.
5+
*
6+
* DIFICULTAD EXTRA (opcional):
7+
* Crea un simulador de pedidos de un restaurante utilizando callbacks.
8+
* Estará formado por una función que procesa pedidos.
9+
* Debe aceptar el nombre del plato, una callback de confirmación, una
10+
* de listo y otra de entrega.
11+
* - Debe imprimir un confirmación cuando empiece el procesamiento.
12+
* - Debe simular un tiempo aleatorio entre 1 a 10 segundos entre
13+
* procesos.
14+
* - Debe invocar a cada callback siguiendo un orden de procesado.
15+
* - Debe notificar que el plato está listo o ha sido entregado.
16+
*/ """
17+
18+
import random
19+
import time
20+
21+
#EJERCICIO
22+
23+
def greeting_process(name: str, callback):
24+
callback(name)
25+
26+
def greet_callback(name: str):
27+
print(f"Hola {name}.")
28+
29+
greeting_process("David", greet_callback)
30+
31+
32+
#DIFICULTAD EXTRA
33+
34+
def order(dish_name: str, confirmation_callback, ready_callback, delivered_callback):
35+
dish_name = input("¿Qué quieres pedir?: ").lower()
36+
confirmation_callback(dish_name)
37+
time.sleep(random.randint(1, 10))
38+
ready_callback(dish_name)
39+
time.sleep(random.randint(1, 10))
40+
delivered_callback(dish_name)
41+
42+
def order_confirmation(dish_name: str):
43+
print(f"Tu pedido {dish_name.lower()} ha sido confirmado.")
44+
45+
46+
def order_ready(dish_name: str):
47+
print(f"Tu pedido {dish_name.lower()} está listo.")
48+
49+
def order_delivered(dish_name: str):
50+
print(f"Tu pedido {dish_name.lower()} ha sido entregado.")
51+
52+
order("", order_confirmation, order_ready, order_delivered)

0 commit comments

Comments
 (0)