8000 feat: memoized fibonnaci · benmtz/codewars-python@012a243 · GitHub
[go: up one dir, main page]

Skip to content

Commit 012a243

Browse files
committed
feat: memoized fibonnaci
1 parent 6ebf20e commit 012a243

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

katas/5-memoized-fibonnaci.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 529adbf7533b761c560004e5
2+
#
3+
# Problem Context
4+
# The Fibonacci sequence is traditionally used to explain tree recursion.
5+
#
6+
# def fibonacci(n):
7+
# if n in [0, 1]:
8+
# return n
9+
# return fibonacci(n - 1) + fibonacci(n - 2)
10+
#
11+
# This algorithm serves welll its educative purpose but it's tremendously
12+
# inefficient, not only because of recursion, but because we invoke the
13+
# fibonacci function twice, and the right branch of recursion
14+
# (i.e. fibonacci(n-2)) recalculates all the Fibonacci numbers already
15+
# calculated by the left branch (i.e. fibonacci(n-1)).
16+
#
17+
# This algorithm is so inefficient that the time to calculate any Fibonacci
18+
# number over 50 is simply too much. You may go for a cup of coffee or go take
19+
# a nap while you wait for the answer. But if you try it here in Code Wars you
20+
# will most likely get a code timeout before any answers.
21+
#
22+
# For this particular Kata we want to implement the memoization solution. This
23+
# will be cool because it will let us keep using the tree recursion algorithm
24+
# while still keeping it sufficiently optimized to get an answer very rapidly.
25+
#
26+
# The trick of the memoized version is that we will keep a cache data structure
27+
# (most likely an associative array) where we will store the Fibonacci numbers
28+
# as we calculate them. When a Fibonacci number is calculated, we first look it
29+
# up in the cache, if it's not there, we calculate it and put it in the cache,
30+
# otherwise we returned the cached number.
31+
#
32+
# Refactor the function into a recursive Fibonacci function that using a
33+
# memoized data structure avoids the deficiencies of tree recursion Can you
34+
# make it so the memoization cache is private to this function?
35+
36+
from unittest import TestCase
37+
38+
39+
# Used this resource
40+
# https://www.geeksforgeeks.org/memoization-using-decorators-in-python/
41+
def cache_result(func):
42+
mem = {}
43+
44+
def inner(n):
45+
if n not in mem:
46+
mem[n] = func(n)
47+
return mem[n]
48+
49+
return inner
50+
51+
# From the solutions this one was cool, since it didn't write it's own
52+
# memoization decorator
53+
#
54+
# from functools import lru_cache
55+
#
56+
# @lru_cache(None)
57+
# def fibonacci(n):
58+
# if n in [0, 1]:
59+
# return n
60+
# return fibonacci(n - 1) + fibonacci(n - 2)
61+
62+
@cache_result
63+
def fibonacci(n):
64+
if n in [0, 1]:
65+
return n
66+
return fibonacci(n - 1) + fibonacci(n - 2)
67+
68+
69+
class Test5MemoizedFibonnaci(TestCase):
70+
def test_1(self):
71+
self.assertEqual(fibonacci(70), 190392490709135)
72+
self.assertEqual(fibonacci(60), 1548008755920)
73+
self.assertEqual(fibonacci(50), 12586269025)

0 commit comments

Comments
 (0)
0