-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstraccionesconfunciones.py
More file actions
162 lines (116 loc) · 3.07 KB
/
Abstraccionesconfunciones.py
File metadata and controls
162 lines (116 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def incrementa(f, numeros):
if numeros==[]:
return []
else:
return [f(numeros[0])] + incrementa(f, numeros[1:])
valor_agregacion = 2
print(incrementa(lambda n: n+valor_agregacion, [1,2,3]))
def conteo(lista):
if lista==[]:
return 0
else:
return 1 + conteo(lista[1:])
def sumatoria(lista):
if lista==[]:
return 0
else:
return lista[0] + sumatoria(lista[1:])
def conteoSuma(f, lista):
if lista==[]:
return 0
else:
return f(lista[0]) + conteoSuma(f, lista[1:])
print(f'Conteo: {conteoSuma(lambda _:1, [1,2,3,4])}')
print(f'Sumatoria: {conteoSuma(lambda n:n, [1,2,3,4])}')
def mayor(numeros, mayorActuAl=0):
if numeros==[]:
return mayorActuAl
else:
if numeros[0]>mayorActuAl:
mayorN = numeros[0]
else:
mayorN = mayorActuAl
return mayor(numeros[1:], mayorN)
print(mayor([1,5,11,7,4,9,6]))
def reducida(f, lista, acc):
if lista==[]:
return acc
else:
accN=f(lista[0], acc)
return reducida(f, lista[1:], accN)
def mayor(lista):
return reducida(lambda num, acc: num if num>acc else acc, lista, 0)
def conteo(lista):
return reducida(lambda _, acc: acc+1, lista, 0)
def sumatoria(lista):
return reducida(lambda num, acc: num+acc, lista, 0)
def mapeo(f, lista):
return reducida(lambda el, acc: acc+[f(el)], lista, [])
def incrementa1(lista):
return mapeo(lambda n:n+1, lista)
print(f'Mayor {mayor([3, 4, 2, 1, 1])}')
print(f'Conteo {conteo([3, 4, 2, 1, 1])}')
print(f'Sumatoria {sumatoria([3, 4, 2, 1, 1])}')
print(incrementa1([3,4,2,1,1]))
def reducida2(f, lista, acc):
for el in lista:
acc=f(el,acc)
return acc
def mayor2(lista):
return reducida2(lambda num, acc: num if num>acc else acc, lista, 0)
def conteo2(lista):
return reducida2(lambda _, acc: acc+1, lista, 0)
def sumatoria2(lista):
return reducida2(lambda num, acc: num+acc, lista, 0)
def mapeo2(f, lista):
return reducida2(lambda el, acc: acc+[f(el)], lista, [])
def incrementa12(lista):
return mapeo2(lambda n:n+1, lista)
print(f'Mayor {mayor2([3, 4, 2, 1, 1])}')
print(f'Conteo {conteo2([3, 4, 2, 1, 1])}')
print(f'Sumatoria {sumatoria2([3, 4, 2, 1, 1])}')
print(incrementa12([3,4,2,1,1]))
'''
def incrementa(x):
r = x+5
return r
def f1(x):
r = f2(x)+f3(x)
print(r)
return r
def f2(x):
r = x*2
print(r)
return r
def f3(x):
r = x-5
print(r)
return r
f1(10)
'''
'''def calcular(f, val):
return f(val)
def f1(x):
return calcular(lambda x:f2(x)+f3(x), x)
def f2(x):
return calcular(lambda x:x*2, x)
def f3(x):
return calcular(lambda x:x-5, x)
print(f1(2))
print(f2(2))
print(f3(2))
'''
def calcular(f):
return lambda x: f(x)
@calcular
def f1(x):
return f2(x)+f3(x)
@calcular
def f2(x):
return x*2
@calcular
def f3(x):
return x-5
print(f1(2))
print(f2(2))
print(f3(2))