Skip to content

Commit da33e97

Browse files
committed
Fix LeetCode tests for 509. Fibonacci Number
1 parent ea8d260 commit da33e97

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ Proposed solutions to some LeetCode problems. The first column links to the prob
8585

8686
First column is the problem difficulty, in descending order, second links to the problem in the Codewars website, the last column links to solutions in this repository.
8787

88-
| Level | Remote Link | Local Link |
89-
| ----- | ------------------------------------------- | -------------------------------------------------------- |
90-
| 6kyu | [Convert string to camel case][cdw517abf8]] | [python](codewars/6-kyu-convert-string-to-camel-case.py) |
91-
| 6kyu | [Who won the election?][cdw554910d]] | [python](codewars/6-kyu-who-won-the-election.py) |
88+
| Level | Remote Link | Local Link |
89+
| ----- | ------------------------------------------ | -------------------------------------------------------- |
90+
| 6kyu | [Convert string to camel case][cdw517abf8] | [python](codewars/6-kyu-convert-string-to-camel-case.py) |
91+
| 6kyu | [Who won the election?][cdw554910d] | [python](codewars/6-kyu-who-won-the-election.py) |
9292

9393
[cdw517abf8]: https://www.codewars.com/kata/517abf86da9663f1d2000003/train/python
9494
[cdw554910d]: https://www.codewars.com/kata/554910d77a3582bbe300009c/train/python

leetcode/fibonacci-number.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
# https://leetcode.com/problems/fibonacci-number/
22

3+
import timeit
34

4-
# Solution using two variables
5+
6+
# Solution using two variables. Tabular dynamic programming.
57
#
68
# Runtime: 48 ms, faster than 54.58% of Python3 online submissions for Fibonacci Number.
79
# Memory Usage: 13.8 MB, less than 54.34 % of Python3 online submissions for Fibonacci Number.
810

9-
10-
class Solution:
11+
class DPTabVar:
1112
def fib(self, n: int) -> int:
1213
if n < 2:
1314
return n
14-
pp, p = 0, 1
15-
for _ in range(2, n+1):
16-
pp, p = p, p + pp
17-
return p
15+
a = b = 1
16+
for _ in range(n-1):
17+
a, b = b, a + b
18+
return a
1819

1920

2021
# Math solution fails for large numbers, fib(200) is short by 1
2122
#
2223
# Runtime: 52 ms, faster than 47.02% of Python3 online submissions for Fibonacci Number.
2324
# Memory Usage: 13.9 MB, less than 54.34 % of Python3 online submissions for Fibonacci Number.
24-
class MathSolution:
25+
class Math:
2526
def fib(self, n: int) -> int:
2627
golden_ratio = (1 + 5 ** 0.5) / 2
2728
return int((golden_ratio ** n + 1) / 5 ** 0.5)
@@ -33,7 +34,7 @@ def fib(self, n: int) -> int:
3334
# Memory Usage: 13.7 MB, less than 95.58 % of Python3 online submissions for Fibonacci Number.
3435

3536

36-
class TSolution:
37+
class DPTab:
3738
def fib(self, n: int) -> int:
3839
f = [0 for _ in range(n + 2)]
3940
f[1] = 1
@@ -44,11 +45,25 @@ def fib(self, n: int) -> int:
4445

4546

4647
def test():
47-
sol = Solution()
48-
assert sol.fib(6) == 8
49-
assert sol.fib(20) == 6765
50-
assert sol.fib(50) == 12586269025
51-
assert sol.fib(200) == 280571172992510140037611932413038677189525
48+
executors = [DPTabVar, DPTab, Math]
49+
tests = [
50+
[6, 8],
51+
[20, 6765],
52+
[50, 12586269025],
53+
# [200, 280571172992510140037611932413038677189525],
54+
]
55+
for executor in executors:
56+
start = timeit.default_timer()
57+
for _ in range(int(float('1'))):
58+
for t in tests:
59+
sol = executor()
60+
result = sol.fib(t[0])
61+
expected = t[1]
62+
assert result == expected, f'{result} != {expected}'
63+
stop = timeit.default_timer()
64+
used = str(round(stop - start, 5))
65+
res = "{0:20}{1:10}{2:10}".format(executor.__name__, used, "seconds")
66+
print(f"\033[92m» {res}\033[0m")
5267

5368

5469
test()

0 commit comments

Comments
 (0)