Created
January 13, 2021 18:33
-
-
Save harpiechoise/86bd06af107d74e1547559bfddf16d8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# La función que se llama a si misma | |
def contar(n, i=0): | |
print(f"i: {i}") | |
if i >= n: | |
return | |
return contar(n, i + 1) | |
# acc | |
# 0 + 0, i = 0, n = 4 -> suma(4, 0, 1) | |
# 0 + 1, i = 1, n = 4 -> suma(4, 1, 2) | |
# 1 + 2, i = 2, n = 4 -> suma(4, 3, 3) | |
# 3 + 3, i = 3, n = 4 -> suma(4, 6, 4) | |
# 6 + 4, i = 4, n = 4 -> 6 + 4 = 10 | |
def suma(n, acc=0, i=0): | |
acc += i | |
if i >= n: | |
return acc | |
return suma(n, acc, i + 1) | |
# [5, 6, 7, 8, 9] | |
# n = l[0] -> 5; del l[0] -> [6, 7, 8, 9]; si es par pares.append(n) -> numeros_pares(l, pares=[]) | |
# n = l[0] -> 6; del l[0] -> [7, 8, 9]; si es par pares.append(n) -> numeros_pares(l, pares=[6]) | |
# n = l[0] -> 7; del l[0] -> [8, 9]; Si es par pares.append(n) -> numeros_pares(l, pares=[6]) | |
# n = l[0] -> 8; del l[0] -> [9]; Si es par pares.append(n) -> numeros pares(l, pares=[6, 8]) | |
# n = l[0] -> 9; del l[0] -> [ ]; Si es par pares.append(n) -> numeros pares([ ], pares=[6, 8]) | |
# n la lista esta vacia; -> [6, 8] | |
def numeros_pares(l, pares=[]): | |
if len(l) == 0: | |
return pares | |
print(f"n = l[0] -> {l[0]};del {l[0]}", end="") | |
n = l[0] | |
del l[0] | |
print(f" -> {l} Si es par pares.append(n) ->", end="") | |
if n % 2 == 0: | |
pares.append(n) | |
print(f"numeros_pares({l}, {pares})") | |
return numeros_pares(l, pares) | |
def numeros_pares(l, pares=[], impar=[]): | |
if len(l) == 0: | |
return pares, impar | |
n = l[0] | |
del l[0] | |
if n % 2 == 0: | |
pares.append(n) | |
else: | |
impar.append(n) | |
return numeros_pares(l, pares, impar) | |
# Funcion de Ackermann | |
# https://es.wikipedia.org/wiki/Funci%C3%B3n_de_Ackermann#:~:text=una%20funci%C3%B3n%20constante.-,Medida%20de%20comparaci%C3%B3n,habilidad%20para%20optimizar%20la%20recursi%C3%B3n. | |
if __name__ == "__main__": | |
# Ackermann | |
print(numeros_pares([i for i in range(1, 4)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment