Skip to content

Instantly share code, notes, and snippets.

@fabian57
Created May 5, 2015 17:39
Show Gist options
  • Save fabian57/7b059f297e8c0912b425 to your computer and use it in GitHub Desktop.
Save fabian57/7b059f297e8c0912b425 to your computer and use it in GitHub Desktop.
OH MON DIEU JE PENSAIS NE JAMAIS LE TROUVER!!!!
SIZE = 20
cache = { (SIZE, SIZE): 0,}
def search(x, y):
if (x, y) not in cache:
if x == SIZE or y == SIZE:
return 1
else:
cache[x+1, y] = search(x+1, y)
cache[x, y+1] = search(x, y+1)
return cache[x+1, y] + cache[x, y+1]
return cache[x, y]
print search(0,0)
@laowantong
Copy link

Je trouve ça un peu bizarre: normalement, le principe de la mémoïsation est de regarder si l'élément à calculer est déjà dans le cache, dans ce cas on le renvoie directement (ce que tu fais), mais sinon on le calcule, on le met dans le cache et on le renvoie: ce n'est pas ce que tu fais, puisque tu calcules et mets en cache d'autres éléments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment