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
+ def get_last (list1 ):
19
+
20
+
21
+ return list1 [len (list1 )- 1 ]
22
+
23
+ def get_first (list1 ):
24
+ return list1 [0 ]
25
+
26
+ def print_index (list1 , function ):
27
+ return function (list1 )
28
+
29
+
30
+
31
+
32
+ print (f" LAst items is { print_index ([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ], get_last )} " )
33
+ print (f" First items is { print_index ([1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ], get_first )} " )
34
+
35
+ import tkinter as tk
36
+
37
+ def on_button_click ():
38
+ print ("¡Botón presionado!" )
39
+
40
+ # Crear la ventana principal
41
+ root = tk .Tk ()
42
+ root .title ("Ejemplo de Callback" )
43
+
44
+ # Crear un botón y asignar el callback
45
+ button = tk .Button (root , text = "Presiona aquí" , command = on_button_click )
46
+ button .pack ()
47
+
48
+ # # Iniciar el bucle de eventos
49
+ root .mainloop ()
50
+
51
+
52
+ def Confirm (name : str ):
53
+ return f"El plato { name } esta confirmado"
54
+
55
+ def Ready (name :str ):
56
+ return f"El plato { name } esta listo"
57
+
58
+ def Delivered (name :int , price ):
59
+ return f"El plato { name } cuesta { price } y ha sido entregado"
60
+
61
+ import random , time
62
+
63
+ def Orders (name :str , action_Confirm ,action_Ready ,action_Delivered ):
64
+ stunden = random .randint (1 ,10 )
65
+ time .sleep (stunden )
66
+ print ( f" { action_Confirm (name )} , tiempo de demora { stunden } Horas" )
67
+ stunden = random .randint (1 ,10 )
68
+ time .sleep (stunden )
69
+ print ( f" { action_Ready (name )} , tiempo de demora { stunden } Horas" )
70
+ stunden = random .randint (1 ,10 )
71
+ time .sleep (stunden )
72
+ print ( f" { action_Delivered (name ,12 * stunden )} , tiempo de demora { stunden } Horas" )
73
+
74
+
75
+ import threading
76
+
77
+ def Extra ():
78
+ print ("" )
79
+ print ("Extra" )
80
+ print ("" )
81
+ orders = ["Brocolicon Queso" , "Pizza de Coliflor" , "Tortilla de Helado" ," Helado de Zanahoria" ]
82
+ Threading = []
83
+ for order in orders :
84
+ Hilo = threading .Thread (target = Orders ,args = (order ,Confirm ,Ready ,Delivered ))
85
+ Threading .append (Hilo )
86
+ Hilo .start ()
87
+
88
+ # espera que terminen todos los hilos activos
89
+ for h in Threading :
90
+ h .join ()
91
+ print ("Estamos Cerrados" )
92
+
93
+
94
+ Extra ()
0 commit comments