Skip to content

Commit 3b99465

Browse files
authored
Merge pull request #8159 from Gordo-Master/#22-Python
#22 - Python
2 parents 8c074b9 + 3a634cc commit 3b99465

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Funciones de Orden superior #
2+
3+
# Son funciones que reciben como parametros otras funcion y devuelven otra funcion
4+
5+
# Ejemplos
6+
# 1. Recibe una función como parametro
7+
8+
from datetime import datetime
9+
from functools import reduce
10+
11+
def do_operation(a, b, operation):
12+
return operation(a,b)
13+
14+
def suma_2_mumeros(a,b):
15+
return a + b
16+
17+
print(do_operation(5,8,suma_2_mumeros))
18+
19+
# 2. Devuelve una función
20+
21+
def multiplicator_maker(factor):
22+
def multiplicator(num):
23+
return num * factor
24+
return multiplicator
25+
26+
tri = multiplicator_maker(3)
27+
print(tri(5))
28+
29+
# 3. Sistema
30+
31+
numbers = [1, 3, 4, 2, 5]
32+
33+
# map()
34+
35+
def apply_double(n):
36+
return n*2
37+
38+
print(list(map(apply_double,numbers)))
39+
40+
# filter()
41+
42+
def is_two(n):
43+
return n % 2 == 0
44+
45+
print(list(filter(is_two,numbers)))
46+
47+
# Sorted()
48+
49+
print(sorted(numbers))
50+
print(sorted(numbers, reverse=True))
51+
print(sorted(numbers, key= lambda x: -x))
52+
53+
# Reduce()
54+
55+
def sum_two(x, y):
56+
return x + y
57+
58+
print(reduce(sum_two, numbers))
59+
60+
###########################################################################################################################
61+
"Dificultad extra"
62+
###########################################################################################################################
63+
64+
######################### OPCION 1 ################################
65+
66+
students_list = [
67+
{"name": "Gustavo",
68+
"birthdate": datetime.strptime("01/02/2015","%d/%m/%Y"),
69+
"scores":
70+
{"math": 6 ,"english": 8,"science": 5}
71+
},
72+
{"name": "Maria",
73+
"birthdate": datetime.strptime("05/05/2014","%d/%m/%Y"),
74+
"scores":
75+
{"math": 5 ,"english": 10,"science": 7}
76+
},
77+
{"name": "John",
78+
"birthdate": datetime.strptime("20/10/2016","%d/%m/%Y"),
79+
"scores":
80+
{"math": 10 ,"english": 8,"science": 10}
81+
}
82+
]
83+
84+
# Toma un diccionario de materias y notas, para sacar un promedio total
85+
def score_prom(scores: dict):
86+
total_sum = 0
87+
flag = 0
88+
for key, value in scores.items():
89+
total_sum += value
90+
flag += 1
91+
return total_sum/flag
92+
93+
# Toma un diccionario que tiene el nombre, y le aplica una función al valor del key (name) dado.
94+
def student_duo(student: dict, function2, name):
95+
return student["name"], function2(student[name])
96+
97+
# print(student_prom(students_list[0],score_prom))
98+
99+
# Forma una lista, a partir de un diccionario mas grande con muchas dependencias, y le apica 2 funciones.
100+
def student_form_list(student_list: list, function1,function2, item):
101+
list_of_student = []
102+
for i in student_list:
103+
list_of_student.append(function1(i,function2, item))
104+
return list_of_student
105+
106+
# Toma la lista de estudiantes con su promedio y elige los que tienen mayor promedio que 9
107+
def student_best_list(student_form_list):
108+
student_list_of_best = []
109+
for i in student_form_list:
110+
if i[1]>9:
111+
student_list_of_best.append(i)
112+
return student_list_of_best
113+
114+
# def student_date(student: dict):
115+
# return student["name"], student["birthdate"]
116+
117+
def identidad(name):
118+
return name
119+
120+
def age(date_given):
121+
return date_given.strftime("%d/%m/%Y")
122+
123+
print(student_form_list(students_list,student_duo,score_prom,"scores"))
124+
print(student_best_list(student_form_list(students_list,student_duo,score_prom, "scores")))
125+
126+
print(student_duo(students_list[0],identidad,"birthdate"))
127+
128+
print(sorted(student_form_list(students_list,student_duo,identidad,"birthdate"),key=lambda x: x[1]))
129+
130+
def max_score(scores: dict):
131+
score_data = list(scores.items())
132+
# for key, value in scores.items():
133+
return max(score_data, key=lambda x : x[1])
134+
135+
print(max_score(students_list[0]["scores"]))
136+
137+
print(max(student_form_list(students_list,student_duo,max_score,"scores")))
138+
139+
140+
141+
######################### OPCION 2 ################################
142+
143+
students = [
144+
{"name": "Brais", "birthdate" : "29-04-1987", "grades": [5, 8.5, 3, 10]},
145+
{"name": "moure", "birthdate" : "04-08-1995", "grades": [1, 9.5, 2, 4]},
146+
{"name": "mouredev", "birthdate" : "15-12-2000", "grades": [4, 6.5, 5, 2]},
147+
{"name": "supermouredev", "birthdate" : "25-01-1980", "grades": [10, 9, 9.7, 9.9]}
148+
]
149+
150+
def average(grades):
151+
return sum(grades) / len(grades)
152+
153+
154+
# Promedio
155+
156+
print(
157+
list(map(lambda student: {
158+
"name": student["name"],
159+
"average": average(student["grades"])},students)
160+
)
161+
)
162+
163+
164+
# Mejores
165+
166+
print(
167+
list(map(lambda student:
168+
student["name"],
169+
filter(lambda student: average(student["grades"]) >= 9 ,students)
170+
)
171+
)
172+
)
173+
174+
# Fecha de nacimiento ordenada
175+
176+
print(sorted(students, key= lambda student: datetime.strptime(
177+
student["birthdate"], "%d-%m-%Y"), reverse= True))
178+
179+
180+
# Calificación mas alta
181+
182+
print(max(list(map(lambda student: max(student["grades"]), students))))

0 commit comments

Comments
 (0)