Skip to content

Commit 07855e4

Browse files
authored
Merge pull request #6353 from raynerpv2022/main
#21 Python and Go
2 parents e04400c + 55dae7f commit 07855e4

File tree

2 files changed

+190
-0
lines changed

2 files changed

+190
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
package main
19+
20+
import (
21+
"fmt"
22+
"math/rand"
23+
"strings"
24+
"sync"
25+
"time"
26+
)
27+
28+
type OrdersType func(name string) string
29+
30+
type MyFunction func(interface{}) interface{}
31+
32+
func Orders(name string, c, r, d OrdersType, w *sync.WaitGroup) {
33+
defer w.Done()
34+
35+
waitTime := rand.Intn(10) + 1
36+
time.Sleep(time.Duration(waitTime) * time.Second)
37+
fmt.Printf("%v Wait time: %vs\n", c(name), waitTime)
38+
39+
waitTime = rand.Intn(10) + 1
40+
time.Sleep(time.Duration(waitTime) * time.Second)
41+
fmt.Printf("%v Wait time: %vs\n", r(name), waitTime)
42+
43+
waitTime = rand.Intn(10) + 1
44+
time.Sleep(time.Duration(waitTime) * time.Second)
45+
fmt.Printf("%v Wait time: %vs\n", d(name), waitTime)
46+
47+
}
48+
49+
func Confirm(name string) string {
50+
51+
return fmt.Sprintf(" Plato %v confirmado", name)
52+
}
53+
54+
func Ready(name string) string {
55+
56+
return fmt.Sprintf(" Plato %v listo", name)
57+
}
58+
59+
func Delivered(name string) string {
60+
61+
return fmt.Sprintf(" Plato %v entregado", name)
62+
}
63+
64+
func GetSum(list1 interface{}) interface{} {
65+
sum := 0
66+
for _, i := range list1.([]int) {
67+
sum += i
68+
}
69+
return sum
70+
}
71+
72+
func GetNameUpperCase(name interface{}) interface{} {
73+
return strings.ToUpper(name.(string))
74+
}
75+
76+
func Nothing(param interface{}, myF MyFunction) interface{} {
77+
return myF(param)
78+
}
79+
80+
func main() {
81+
fmt.Println(Nothing([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, GetSum))
82+
fmt.Println(Nothing("resbaloso", GetNameUpperCase))
83+
84+
fmt.Println("Extra")
85+
orders := []string{
86+
"Calabaza",
87+
"Brocoli",
88+
"Cebollino"}
89+
var w sync.WaitGroup
90+
fmt.Println("Precesamiento de Pedidos")
91+
for _, i := range orders {
92+
w.Add(1)
93+
go Orders(i, Confirm, Ready, Delivered, &w)
94+
}
95+
w.Wait()
96+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)