We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 134e683 commit 9b61b96Copy full SHA for 9b61b96
1 file changed
Sprint-2/improve_with_caches/fibonacci/fibonacci.py
@@ -1,4 +1,17 @@
1
+# Create a dictionary to use as our "Notepad" (Cache)
2
+memo = {}
3
+
4
def fibonacci(n):
5
+ # Check if we already calculated this number
6
+ if n in memo:
7
+ return memo[n]
8
9
if n <= 1:
10
return n
- return fibonacci(n - 1) + fibonacci(n - 2)
11
12
+ result = fibonacci(n - 1) + fibonacci(n - 2)
13
14
+ # Save the result in our Notepad before returning
15
+ memo[n] = result
16
17
+ return result
0 commit comments