Skip to content

Commit 71d2dd8

Browse files
committed
mouredev#22 - python
1 parent 5cd534e commit 71d2dd8

File tree

1 file changed

+79
-0
lines changed
  • Roadmap/22 - FUNCIONES DE ORDEN SUPERIOR/python

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# @Author Daniel Grande (Mhayhem)
2+
3+
from datetime import date, datetime
4+
5+
# EJERCICIO:
6+
# Explora el concepto de funciones de orden superior en tu lenguaje
7+
# creando ejemplos simples (a tu elección) que muestren su funcionamiento.
8+
9+
def greet(name):
10+
return f"Hola {name}"
11+
12+
def process_greeting(text: str, function):
13+
return function(text)
14+
15+
print(process_greeting("Dany", greet))
16+
17+
# DIFICULTAD EXTRA (opcional):
18+
# Dada una lista de estudiantes (con sus nombres, fecha de nacimiento y
19+
# lista de calificaciones), utiliza funciones de orden superior para
20+
# realizar las siguientes operaciones de procesamiento y análisis:
21+
# - Promedio calificaciones: Obtiene una lista de estudiantes por nombre
22+
# y promedio de sus calificaciones.
23+
# - Mejores estudiantes: Obtiene una lista con el nombre de los estudiantes
24+
# que tienen calificaciones con un 9 o más de promedio.
25+
# - Nacimiento: Obtiene una lista de estudiantes ordenada desde el más joven.
26+
# - Mayor calificación: Obtiene la calificación más alta de entre todas las
27+
# de los alumnos.
28+
# - Una calificación debe estar comprendida entre 0 y 10 (admite decimales).
29+
30+
students = [
31+
{
32+
"name": "Dany",
33+
"birth_day": date(1999, 7, 15),
34+
"grades": [6, 7.5, 9, 9, 10, 9]
35+
},
36+
{
37+
"name": "Carlos",
38+
"birth_day": date(1999, 2, 13),
39+
"grades": [9, 9, 10, 9, 9, 10]
40+
},
41+
{
42+
"name": "Sandra",
43+
"birth_day": date(2000, 12, 31),
44+
"grades": [9, 8.5, 9, 9, 9, 9.5]
45+
}
46+
]
47+
def students_info(func1, func2, func3, func4, array):
48+
print("Promedios:")
49+
print(func1(array))
50+
print("Promedios mas altos:")
51+
print(func2(array))
52+
print("Estudiantes ordenados de menor a mayor:")
53+
print(func3(array))
54+
print("Mejor promedio:")
55+
print(func4(array))
56+
57+
def average_grades(array: list):
58+
return [
59+
f"{student['name']}; Promedio: {sum(student['grades']) / len(student['grades']):.1f}" for student in array
60+
]
61+
62+
def highest_average(array: list):
63+
return [
64+
student['name']
65+
for student in array
66+
if (sum(student['grades']) / len(student['grades'])) >= 9
67+
]
68+
def youngest(array: list):
69+
today = date.today()
70+
sorted_list = sorted(array, key=lambda s: s["birth_day"], reverse=True)
71+
return [
72+
f"{item['name']} {today.year - (item['birth_day'].year) - ((today.month, today.day) < (item["birth_day"].month, item["birth_day"].day))} años"
73+
for item in sorted_list
74+
]
75+
76+
def the_best(array: list):
77+
return max(array, key=lambda s: sum(s["grades"]) / len(s["grades"]))["name"]
78+
79+
students_info(average_grades, highest_average, youngest, the_best, students)

0 commit comments

Comments
 (0)