Skip to content

Commit 37bf3af

Browse files
committed
update with chapter 4-6 exercises
1 parent 71866d7 commit 37bf3af

File tree

3 files changed

+2708
-0
lines changed

3 files changed

+2708
-0
lines changed

exe4.ipynb

+341
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# 4.1\n",
10+
"import random\n",
11+
"S = [random.randint(0,100) for i in range(15)]\n",
12+
"\n",
13+
"def findmax(S, index):\n",
14+
" if index==len(S)-1:\n",
15+
" return S[index]\n",
16+
" else: return max(S[index], findmax(S, index+1))\n",
17+
" \n",
18+
"\n",
19+
"print(S)\n",
20+
"print(findmax(S, 0))"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"# 4.2\n",
30+
"def power(x,n):\n",
31+
" if n==0:\n",
32+
" return 1\n",
33+
" else: \n",
34+
" return x*(power(x,n-1))\n",
35+
"\n",
36+
"\"\"\"\n",
37+
"given x=2, n=5\n",
38+
"\n",
39+
"power(2,5) --> 2*16 = 32\n",
40+
" power(2,4) --> 2*8 = 16\n",
41+
" power(2,3) --> 2*4 = 8\n",
42+
" power(2,2) --> 2*2 = 4\n",
43+
" power(2,1) --> 2*1 = 2\n",
44+
" power(2,0) --> 1\n",
45+
"\"\"\"\n",
46+
"print(power(2,5))"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"# 4.3\n",
56+
"def power(x,n):\n",
57+
" \"recursian trace method\"\n",
58+
" if n==0:\n",
59+
" return 1\n",
60+
" else:\n",
61+
" partial = power(x,n//2)\n",
62+
" result = partial*partial\n",
63+
" if n%2==1:\n",
64+
" result*=x\n",
65+
" return result\n",
66+
"\n",
67+
"\"\"\"\n",
68+
"power(2,10) --> power(2,5)*power(2,5) = 32*32 = 1024\n",
69+
" power(2,5) --> power(2,2)*power(2,2)*2 = 4*4*2 = 32\n",
70+
" power(2,2) --> power(2,1)*power(2,1) = 2*2 = 4\n",
71+
" power(2,1) --> power(2,0)*power(2,0) = 1*1\n",
72+
" power(2,0) --> 1\n",
73+
"\"\"\""
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"# 4.4\n",
83+
"def reverse(S, start, stop):\n",
84+
" if start < stop - 1:\n",
85+
" S[start], S[stop-1] = S[stop-1], S[start]\n",
86+
" reverse(S, start+1, stop-1)\n",
87+
"\n",
88+
"\"\"\"\n",
89+
"S=[0,1,2,3,4,5,6,7]\n",
90+
"start,stop = 0,8 -> S[0],S[7] <->\n",
91+
" start,stop = 1,7 -> S[1],S[6] <->\n",
92+
" start,stop = 2,6 -> S[2],S[5] <->\n",
93+
" start,stop = 3,5 -> S[3],S[4] <->\n",
94+
" start,stop = 4,4 -> S[4],S[3] skip, and the rest\n",
95+
"\"\"\""
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": [
104+
"# 4.5 -- skip"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"# 4.6\n",
114+
"def HarmonicNum(n):\n",
115+
" if n==1:\n",
116+
" return 1\n",
117+
" else:\n",
118+
" return 1/n+HarmonicNum(n-1)\n",
119+
"\n",
120+
"HarmonicNum(5)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": null,
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"# 4.7, 4.8 skip\n",
130+
"def string2int(string):\n",
131+
" n=len(string)\n",
132+
" if n!=0:\n",
133+
" return int(string[0])*(10**(n-1)) + string2int(string[1:n])\n",
134+
" else:\n",
135+
" return 0\n",
136+
"\n",
137+
"a = string2int('12345607')\n",
138+
"print(a, type(a))"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"# 4.9\n",
148+
"def FindMinMax(egset, index=0):\n",
149+
" if index==len(egset)-1:\n",
150+
" minx, maxx = egset[index],egset[index]\n",
151+
" return minx, maxx\n",
152+
" else:\n",
153+
" minx, maxx = FindMinMax(egset,index+1)\n",
154+
" return min(egset[index], minx), max(egset[index], maxx)"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"s1=[1,3,5,6,3,6,-3,20,4,3]\n",
164+
"s2=[9,3,4,2,5,6,8,34,-40,32]\n",
165+
"print(FindMinMax(s1))\n",
166+
"print(FindMinMax(s2))"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"# 4.10\n",
176+
"def log2int(n):\n",
177+
" assert n>0\n",
178+
" if n<2:\n",
179+
" return 0\n",
180+
" else:\n",
181+
" return 1+log2int(n//2)\n",
182+
"\n",
183+
"log2int(16)"
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
192+
"# 4.11\n",
193+
"def uniquecheck(S, index=0):\n",
194+
" if index==len(S)-1:\n",
195+
" return True\n",
196+
" else:\n",
197+
" unique=True\n",
198+
" for i in range(index+1, len(S)):\n",
199+
" if S[index]==S[i]:\n",
200+
" unique=False\n",
201+
" return unique and uniquecheck(S,index+1)\n",
202+
"\n",
203+
"for S in [[1,2,3,4,5,6], \n",
204+
" [3,4,5,2,3,4,6,7],\n",
205+
" [234,654,32,543,652]\n",
206+
" ]:\n",
207+
" print(uniquecheck(S))"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": [
216+
"# 4.12, 4.13 skipped\n",
217+
"def product(m,n):\n",
218+
" if n==0:\n",
219+
" return 0\n",
220+
" else:\n",
221+
" return m + product(m,n-1)\n",
222+
"\n",
223+
"product(2,6)"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 11,
229+
"metadata": {},
230+
"outputs": [
231+
{
232+
"name": "stdout",
233+
"output_type": "stream",
234+
"text": [
235+
"1\n",
236+
"2\n",
237+
"3\n",
238+
"4\n"
239+
]
240+
}
241+
],
242+
"source": [
243+
"s=[1,2,3,4]\n",
244+
"for i in range(len(s)):\n",
245+
" print(s[i])"
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"execution_count": 25,
251+
"metadata": {},
252+
"outputs": [
253+
{
254+
"name": "stdout",
255+
"output_type": "stream",
256+
"text": [
257+
"[2, 3, 4]\n",
258+
"[1, 3, 4]\n",
259+
"[1, 2, 4]\n",
260+
"[1, 2, 3]\n",
261+
"1\n",
262+
"[3, 4]\n",
263+
"[2, 4]\n",
264+
"[2, 3]\n",
265+
"3\n",
266+
"[4]\n",
267+
"[2]\n"
268+
]
269+
},
270+
{
271+
"ename": "IndexError",
272+
"evalue": "pop index out of range",
273+
"output_type": "error",
274+
"traceback": [
275+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
276+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
277+
"\u001b[1;32m/home/gvh/Desktop/data structure & algo practise/exe4.ipynb Cell 14\u001b[0m in \u001b[0;36m<cell line: 30>\u001b[0;34m()\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m subset(setcopy,n\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=26'>27</a>\u001b[0m \u001b[39melse\u001b[39;00m: \u001b[39mreturn\u001b[39;00m S\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=29'>30</a>\u001b[0m subset([\u001b[39m1\u001b[39;49m,\u001b[39m2\u001b[39;49m,\u001b[39m3\u001b[39;49m,\u001b[39m4\u001b[39;49m])\n",
278+
"\u001b[1;32m/home/gvh/Desktop/data structure & algo practise/exe4.ipynb Cell 14\u001b[0m in \u001b[0;36msubset\u001b[0;34m(S, n)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m \u001b[39m# subset(setcopy2,n+1)\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m \u001b[39mprint\u001b[39m(setcopy\u001b[39m.\u001b[39mpop(n))\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m subset(setcopy,n\u001b[39m+\u001b[39;49m\u001b[39m1\u001b[39;49m)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=26'>27</a>\u001b[0m \u001b[39melse\u001b[39;00m: \u001b[39mreturn\u001b[39;00m S\n",
279+
"\u001b[1;32m/home/gvh/Desktop/data structure & algo practise/exe4.ipynb Cell 14\u001b[0m in \u001b[0;36msubset\u001b[0;34m(S, n)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m \u001b[39m# subset(setcopy2,n+1)\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m \u001b[39mprint\u001b[39m(setcopy\u001b[39m.\u001b[39mpop(n))\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m subset(setcopy,n\u001b[39m+\u001b[39;49m\u001b[39m1\u001b[39;49m)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=26'>27</a>\u001b[0m \u001b[39melse\u001b[39;00m: \u001b[39mreturn\u001b[39;00m S\n",
280+
"\u001b[1;32m/home/gvh/Desktop/data structure & algo practise/exe4.ipynb Cell 14\u001b[0m in \u001b[0;36msubset\u001b[0;34m(S, n)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=22'>23</a>\u001b[0m \u001b[39mprint\u001b[39m(setcopy2)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=23'>24</a>\u001b[0m \u001b[39m# subset(setcopy2,n+1)\u001b[39;00m\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=24'>25</a>\u001b[0m \u001b[39mprint\u001b[39m(setcopy\u001b[39m.\u001b[39;49mpop(n))\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=25'>26</a>\u001b[0m subset(setcopy,n\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m)\n\u001b[1;32m <a href='vscode-notebook-cell:/home/gvh/Desktop/data%20structure%20%26%20algo%20practise/exe4.ipynb#X15sZmlsZQ%3D%3D?line=26'>27</a>\u001b[0m \u001b[39melse\u001b[39;00m: \u001b[39mreturn\u001b[39;00m S\n",
281+
"\u001b[0;31mIndexError\u001b[0m: pop index out of range"
282+
]
283+
}
284+
],
285+
"source": [
286+
"# 4.14 skipped temporarily\n",
287+
"# 4.15\n",
288+
"def subset(S,n=0):\n",
289+
" setcopy=S.copy()\n",
290+
" if len(setcopy)>1:\n",
291+
" for i in range(len(setcopy)):\n",
292+
" setcopy2=setcopy.copy()\n",
293+
" # print(f'minus n={n},i={i}')\n",
294+
" setcopy2.pop(i)\n",
295+
" # return setcopy2\n",
296+
" print(setcopy2)\n",
297+
" # subset(setcopy2,n+1)\n",
298+
" print(setcopy.pop(n))\n",
299+
" subset(setcopy,n+1)\n",
300+
" else: return S\n",
301+
" \n",
302+
"\n",
303+
"subset([1,2,3,4])"
304+
]
305+
},
306+
{
307+
"cell_type": "code",
308+
"execution_count": null,
309+
"metadata": {},
310+
"outputs": [],
311+
"source": []
312+
}
313+
],
314+
"metadata": {
315+
"kernelspec": {
316+
"display_name": "Python 3.9.12 ('deepcv')",
317+
"language": "python",
318+
"name": "python3"
319+
},
320+
"language_info": {
321+
"codemirror_mode": {
322+
"name": "ipython",
323+
"version": 3
324+
},
325+
"file_extension": ".py",
326+
"mimetype": "text/x-python",
327+
"name": "python",
328+
"nbconvert_exporter": "python",
329+
"pygments_lexer": "ipython3",
330+
"version": "3.9.12"
331+
},
332+
"orig_nbformat": 4,
333+
"vscode": {
334+
"interpreter": {
335+
"hash": "38e3adfb29cde9206c861f814c9fa42a1353f77e60ebe68d31b54d2673048b17"
336+
}
337+
}
338+
},
339+
"nbformat": 4,
340+
"nbformat_minor": 2
341+
}

0 commit comments

Comments
 (0)