Skip to content

Commit 839e2f5

Browse files
recursion added
1 parent cb49d99 commit 839e2f5

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

Recursion.ipynb

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Recursion\n",
8+
"A function making one or more call to itself.\n",
9+
"A simple example is demonstrated by writing a factorial function."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 4,
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"data": {
19+
"text/plain": [
20+
"24"
21+
]
22+
},
23+
"execution_count": 4,
24+
"metadata": {},
25+
"output_type": "execute_result"
26+
}
27+
],
28+
"source": [
29+
"def factorial(n):\n",
30+
" if n == 0:\n",
31+
" return 1\n",
32+
" if n == 1:\n",
33+
" return 1\n",
34+
" a=n*factorial(n-1)\n",
35+
" return a\n",
36+
"factorial(4)"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 6,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"data": {
46+
"text/plain": [
47+
"10"
48+
]
49+
},
50+
"execution_count": 6,
51+
"metadata": {},
52+
"output_type": "execute_result"
53+
}
54+
],
55+
"source": [
56+
"\"\"\"\n",
57+
"sum from 0 to n, given n is an integer\n",
58+
"\"\"\"\n",
59+
"\n",
60+
"def sum_integer(n):\n",
61+
" if n == 0:\n",
62+
" return 0\n",
63+
" s = n + sum_integer(n-1)\n",
64+
" return s\n",
65+
"sum_integer(4)"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 13,
71+
"metadata": {},
72+
"outputs": [
73+
{
74+
"data": {
75+
"text/plain": [
76+
"26"
77+
]
78+
},
79+
"execution_count": 13,
80+
"metadata": {},
81+
"output_type": "execute_result"
82+
}
83+
],
84+
"source": [
85+
"\"\"\"\n",
86+
"Given a number, return the sum of integers\n",
87+
"\"\"\"\n",
88+
"\n",
89+
"def sum_of_digits(n):\n",
90+
" if n%10 == 0:\n",
91+
" return 0\n",
92+
" s= (n%10) + sum_of_digits(n//10)\n",
93+
" return s\n",
94+
"\n",
95+
"sum_of_digits(7865)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 16,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"['the', 'man', 'ran']"
107+
]
108+
},
109+
"execution_count": 16,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"\"\"\"\n",
116+
"Given a string phrase and a set of list words. Split string such that words from the list are made\n",
117+
"\"\"\"\n",
118+
"def word_split(phrase, l_words, output = None):\n",
119+
" if output is None:\n",
120+
" output = []\n",
121+
" for word in l_words:\n",
122+
" if phrase.startswith(word):\n",
123+
" output.append(word)\n",
124+
" return word_split(phrase[len(word):], l_words, output)\n",
125+
" return output\n",
126+
"word_split('themanran',['the', 'ran', 'man'])"
127+
]
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"## Memoization\n",
134+
"\n",
135+
"## Interview Questions"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 23,
141+
"metadata": {},
142+
"outputs": [
143+
{
144+
"data": {
145+
"text/plain": [
146+
"'kjhgfdcba'"
147+
]
148+
},
149+
"execution_count": 23,
150+
"metadata": {},
151+
"output_type": "execute_result"
152+
}
153+
],
154+
"source": [
155+
"\"\"\"\n",
156+
"Reverse a string using recursion\n",
157+
"\"\"\"\n",
158+
"def reverse(s):\n",
159+
" if len(s) == 1:\n",
160+
" return s\n",
161+
" return reverse(s[1:]) + s[0]\n",
162+
"reverse(\"abcdfghjk\")"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 24,
168+
"metadata": {},
169+
"outputs": [],
170+
"source": [
171+
"\"\"\"\n",
172+
"Find all permutations of a given string. If character is repeating, treat each as distinct\n",
173+
"If not using recursion, you can use itertools library\n",
174+
"\"\"\"\n",
175+
"def permutation(s):\n",
176+
" out = []\n",
177+
" if len(s) == 1:\n",
178+
" out = [s]\n",
179+
" else:\n",
180+
" for i,let in enumerate(s):\n",
181+
" for perm in permute(s[:i] + s[i+1:]):\n",
182+
" out += [let+perm]\n",
183+
" return out\n",
184+
"per "
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {},
191+
"outputs": [],
192+
"source": []
193+
}
194+
],
195+
"metadata": {
196+
"kernelspec": {
197+
"display_name": "Python 3",
198+
"language": "python",
199+
"name": "python3"
200+
},
201+
"language_info": {
202+
"codemirror_mode": {
203+
"name": "ipython",
204+
"version": 3
205+
},
206+
"file_extension": ".py",
207+
"mimetype": "text/x-python",
208+
"name": "python",
209+
"nbconvert_exporter": "python",
210+
"pygments_lexer": "ipython3",
211+
"version": "3.7.3"
212+
}
213+
},
214+
"nbformat": 4,
215+
"nbformat_minor": 2
216+
}

0 commit comments

Comments
 (0)