Skip to content

Commit 09711dd

Browse files
committed
[chore] replace py file with notebook
1 parent 36a446f commit 09711dd

File tree

2 files changed

+97
-43
lines changed

2 files changed

+97
-43
lines changed

recursion/fibonacci.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.9.0-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3.9.0 64-bit",
19+
"metadata": {
20+
"interpreter": {
21+
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
22+
}
23+
}
24+
}
25+
},
26+
"nbformat": 4,
27+
"nbformat_minor": 2,
28+
"cells": [
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"def fibonacci(n, memoize={1:0, 2: 1}):\n",
36+
" \"\"\"\n",
37+
" Nth fibonacci number using memoization.\n",
38+
" \n",
39+
" O(n) space and time\n",
40+
" \"\"\"\n",
41+
" if n < 0:\n",
42+
" raise TypeError('Value of n must be a positive integer')\n",
43+
" if n in memoize:\n",
44+
" return memoize[n]\n",
45+
" else:\n",
46+
" memoize[n] = fibonacci(n - 1, memoize) + fibonacci(n - 2, memoize)\n",
47+
" return memoize[n]\n",
48+
"\n",
49+
"\n",
50+
"def efficient_fibonacci(n):\n",
51+
" \"\"\"\n",
52+
" nth fibonacci number iteratively.\n",
53+
"\n",
54+
" Complexity:\n",
55+
" O(n) time,\n",
56+
" O(1) space, since we are only storing two array values at any given time.\n",
57+
" \"\"\"\n",
58+
" last_two = [0, 1]\n",
59+
"\n",
60+
" counter = 3\n",
61+
" while counter <= n:\n",
62+
" next_fibonacci = sum(last_two)\n",
63+
" last_two = [last_two[1], next_fibonacci]\n",
64+
" counter += 1\n",
65+
" # the else clause caters for the edge case of the first fibo number == 0\n",
66+
" return last_two[1] if n > 1 else last_two[0]"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 3,
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"output_type": "execute_result",
76+
"data": {
77+
"text/plain": [
78+
"89"
79+
]
80+
},
81+
"metadata": {},
82+
"execution_count": 3
83+
}
84+
],
85+
"source": [
86+
"efficient_fibonacci(12)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": []
95+
}
96+
]
97+
}

recursion/fibonacci.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)