Skip to content

Commit e9e87c1

Browse files
committed
recursion examples
1 parent f73732b commit e9e87c1

File tree

3 files changed

+240
-11
lines changed

3 files changed

+240
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ If you have any suggestions or want to make additions, I would be very happy if
6363
- FizzBuzz [[ IPython nbviewer ](http://nbviewer.ipython.org/github/rasbt/algorithms_in_ipython_notebooks/blob/master/ipython_nbs/essentials/fizzbuzz.ipynb)]
6464
- Introduction to Greedy Algorithms [[ IPython nbviewer ](http://nbviewer.ipython.org/github/rasbt/algorithms_in_ipython_notebooks/blob/master/ipython_nbs/essentials/greedy-algorithm-intro.ipynb)]
6565
- Introduction to Divide-and-Conquer Algorithms [[ IPython nbviewer ](http://nbviewer.ipython.org/github/rasbt/algorithms_in_ipython_notebooks/blob/master/ipython_nbs/essentials/divide-and-conquer-algorithm-intro.ipynb)]
66+
- Recursion Examples [[ IPython nbviewer ](http://nbviewer.ipython.org/github/rasbt/algorithms_in_ipython_notebooks/blob/master/ipython_nbs/essentials/recursion-examples.ipynb)]
6667

6768
### Efficiency
6869

ipython_nbs/essentials/fizzbuzz.ipynb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
{
44
"cell_type": "code",
55
"execution_count": 3,
6-
"metadata": {
7-
"collapsed": false
8-
},
6+
"metadata": {},
97
"outputs": [],
108
"source": [
119
"%load_ext watermark"
@@ -14,9 +12,7 @@
1412
{
1513
"cell_type": "code",
1614
"execution_count": 5,
17-
"metadata": {
18-
"collapsed": false
19-
},
15+
"metadata": {},
2016
"outputs": [
2117
{
2218
"name": "stdout",
@@ -65,9 +61,7 @@
6561
{
6662
"cell_type": "code",
6763
"execution_count": 8,
68-
"metadata": {
69-
"collapsed": false
70-
},
64+
"metadata": {},
7165
"outputs": [
7266
{
7367
"name": "stdout",
@@ -209,9 +203,9 @@
209203
"name": "python",
210204
"nbconvert_exporter": "python",
211205
"pygments_lexer": "ipython3",
212-
"version": "3.5.1"
206+
"version": "3.6.1"
213207
}
214208
},
215209
"nbformat": 4,
216-
"nbformat_minor": 0
210+
"nbformat_minor": 1
217211
}
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Sebastian Raschka \n",
13+
"last updated: 2017-08-03 \n",
14+
"\n",
15+
"CPython 3.6.1\n",
16+
"IPython 6.1.0\n"
17+
]
18+
}
19+
],
20+
"source": [
21+
"%load_ext watermark\n",
22+
"%watermark -a 'Sebastian Raschka' -u -d -v"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"# Examples using Recursion"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"**Important Note**\n",
37+
"\n",
38+
"For most cases, using function recursion should be avoided in Python are better be implemented using for/while loops most of the time (although, I must admit that recursive solutions do look elegant). One of the reasons is that stacking recursive calls can easily blow up memory or at least result in the popular yet nasty \"RuntimeError: maximum recursion depth exceeded\". Also, keep in mind that Python does not optimize tail recursion in favor of having the full tracebacks for debugging; related to that, please see Guido van Rossums blog posts \"[Tail Recursion Elimination](http://neopythonic.blogspot.com.au/2009/04/tail-recursion-elimination.html)\" and \"[Final Words on Tail Calls](http://neopythonic.blogspot.com.au/2009/04/final-words-on-tail-calls.html).\" If you do like to play around with recursion more efficiently, I highly recommend taking a look at [Haskell](https://www.haskell.org) or other functional programming languages. That being said, below are some examples of recursive function implementations in Python for illustrative purposes."
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"## Factorial"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"metadata": {
52+
"collapsed": true
53+
},
54+
"outputs": [],
55+
"source": [
56+
"def factorial(x):\n",
57+
" if x <= 1:\n",
58+
" return x\n",
59+
" else:\n",
60+
" return x * factorial(x-1)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": 3,
66+
"metadata": {},
67+
"outputs": [
68+
{
69+
"data": {
70+
"text/plain": [
71+
"0"
72+
]
73+
},
74+
"execution_count": 3,
75+
"metadata": {},
76+
"output_type": "execute_result"
77+
}
78+
],
79+
"source": [
80+
"factorial(0)"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": 4,
86+
"metadata": {},
87+
"outputs": [
88+
{
89+
"data": {
90+
"text/plain": [
91+
"1"
92+
]
93+
},
94+
"execution_count": 4,
95+
"metadata": {},
96+
"output_type": "execute_result"
97+
}
98+
],
99+
"source": [
100+
"factorial(1)"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"5! = 5 x 4 x 3 x 2 x 1 = 120"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 5,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"data": {
117+
"text/plain": [
118+
"120"
119+
]
120+
},
121+
"execution_count": 5,
122+
"metadata": {},
123+
"output_type": "execute_result"
124+
}
125+
],
126+
"source": [
127+
"factorial(5)"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"## Array Sum"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 6,
140+
"metadata": {
141+
"collapsed": true
142+
},
143+
"outputs": [],
144+
"source": [
145+
"def array_sum(x):\n",
146+
" if not len(x):\n",
147+
" return 0\n",
148+
" else:\n",
149+
" return x[0] + array_sum(x[1:])"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 7,
155+
"metadata": {},
156+
"outputs": [
157+
{
158+
"data": {
159+
"text/plain": [
160+
"0"
161+
]
162+
},
163+
"execution_count": 7,
164+
"metadata": {},
165+
"output_type": "execute_result"
166+
}
167+
],
168+
"source": [
169+
"array_sum([])"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 8,
175+
"metadata": {},
176+
"outputs": [
177+
{
178+
"data": {
179+
"text/plain": [
180+
"5"
181+
]
182+
},
183+
"execution_count": 8,
184+
"metadata": {},
185+
"output_type": "execute_result"
186+
}
187+
],
188+
"source": [
189+
"array_sum([5])"
190+
]
191+
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": 9,
195+
"metadata": {},
196+
"outputs": [
197+
{
198+
"data": {
199+
"text/plain": [
200+
"15"
201+
]
202+
},
203+
"execution_count": 9,
204+
"metadata": {},
205+
"output_type": "execute_result"
206+
}
207+
],
208+
"source": [
209+
"array_sum([1, 2, 3, 4, 5])"
210+
]
211+
}
212+
],
213+
"metadata": {
214+
"kernelspec": {
215+
"display_name": "Python 3",
216+
"language": "python",
217+
"name": "python3"
218+
},
219+
"language_info": {
220+
"codemirror_mode": {
221+
"name": "ipython",
222+
"version": 3
223+
},
224+
"file_extension": ".py",
225+
"mimetype": "text/x-python",
226+
"name": "python",
227+
"nbconvert_exporter": "python",
228+
"pygments_lexer": "ipython3",
229+
"version": "3.6.1"
230+
}
231+
},
232+
"nbformat": 4,
233+
"nbformat_minor": 2
234+
}

0 commit comments

Comments
 (0)