Created
August 29, 2014 22:42
-
-
Save colltoaction/22a67dab16331e13d5d6 to your computer and use it in GitHub Desktop.
Ejercicio de la escalera implementado con y sin Programación Dinámica.
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
def escalera_feo(n): | |
if n == 1: | |
return 1 | |
elif n == 2: | |
return 2 | |
return escalera_feo(n - 1) + escalera_feo(n - 2) | |
def escalera(n): | |
d = { 1 : 1, 2 : 2 } | |
return escalera_r(n, d) | |
def escalera_r(n, d): | |
if n not in d: | |
d[n] = escalera_r(n - 1, d) + escalera_r(n - 2, d) | |
return d[n] | |
# la diferencia de tiempos es notable (y n es solamente 35!): | |
print(escalera(35)) | |
print(escalera_feo(35)) | |
# probar, el segundo nunca termina: | |
# print(escalera(500)) | |
# print(escalera_feo(500)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment