Skip to content

Commit 0bd428e

Browse files
committed
Added solutions for chapter 1 creativity exercises
1 parent 153e919 commit 0bd428e

File tree

3 files changed

+137
-39
lines changed

3 files changed

+137
-39
lines changed

book-notes/data-structures-and-algorithms-in-python-by-goodrich-et-al/chapter-01-python-primer/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
## Exercise Progress
22

3-
43
| Reinforcement | Creativity | Projects |
54
| :-----------: | :--------: | :------: |
65
| R-1.1 ✅ | C-1.13 ✅ | P-1.29 ✅ |
76
| R-1.2 ✅ | C-1.14 ✅ | P-1.30 ✅ |
87
| R-1.3 ✅ | C-1.15 ✅ | P-1.31 ⚠️ |
98
| R-1.4 ✅ | C-1.16 ✅ | P-1.32 ⚠️ |
10-
| R-1.5 ✅ | C-1.17 ⚠️ | P-1.33 ⚠️ |
9+
| R-1.5 ✅ | C-1.17 | P-1.33 ⚠️ |
1110
| R-1.6 ✅ | C-1.18 ✅ | P-1.34 ✅ |
1211
| R-1.7 ✅ | C-1.19 ✅ | P-1.35 ✅ |
1312
| R-1.8 ✅ | C-1.20 ✅ | P-1.36 ✅ |
@@ -17,7 +16,7 @@
1716
| R-1.12 ✅ | C-1.24 ✅ | - |
1817
| - | C-1.25 ✅ | - |
1918
| - | C-1.26 ✅ | - |
20-
| - | C-1.27 ⚠️ | - |
19+
| - | C-1.27 | - |
2120
| - | C-1.28 ✅ | - |
2221

2322

book-notes/data-structures-and-algorithms-in-python-by-goodrich-et-al/chapter-01-python-primer/chapter-01-python-primer-exercises-(creativity).ipynb

+62-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"def simple_reverse(data):\n",
3737
" return [i for i in reversed(data)]\n",
3838
"\n",
39-
"assert simple_reverse([2,6,3]) == [3,6,2]\n",
39+
"assert simple_reverse([2, 6, 3]) == [3, 6, 2]\n",
4040
"\n",
4141
"# List class method that reverses in place\n",
4242
"\n",
@@ -107,15 +107,14 @@
107107
},
108108
{
109109
"cell_type": "code",
110-
"execution_count": 4,
110+
"execution_count": 3,
111111
"metadata": {},
112112
"outputs": [
113113
{
114-
"name": "stdout",
115114
"output_type": "stream",
115+
"name": "stdout",
116116
"text": [
117-
"[12, 24, 16]\n",
118-
"[12, 24, 16]\n"
117+
"[12, 24, 16]\n[12, 24, 16]\n"
119118
]
120119
}
121120
],
@@ -150,27 +149,38 @@
150149
},
151150
{
152151
"cell_type": "code",
153-
"execution_count": 32,
152+
"execution_count": 9,
154153
"metadata": {},
155154
"outputs": [
156155
{
157156
"output_type": "stream",
158157
"name": "stdout",
159-
"text": "[2, 3, 4]\n"
158+
"text": [
159+
"[5, 10, 15]\n[1, 2, 3]\n"
160+
]
160161
}
161162
],
162163
"source": [
163-
"# \n",
164-
"\n",
165-
"lst = [2,3,4]\n",
164+
"# Original implementation\n",
166165
"\n",
167166
"def scale(data, factor):\n",
167+
" for j in range(len(data)):\n",
168+
" data[j] *= factor\n",
169+
" return data\n",
170+
"\n",
171+
"print(scale([1,2,3], 5))\n",
172+
"\n",
173+
"# Modified implementation\n",
174+
"# This implementation doesn't work as the val\n",
175+
"# variable is a new variable and not referencing the\n",
176+
"# data list \n",
177+
"\n",
178+
"def scale_modified(data, factor):\n",
168179
" for val in data:\n",
169180
" val *= factor\n",
181+
" return data\n",
170182
" \n",
171-
"scale(lst, 3)\n",
172-
"\n",
173-
"print(data)"
183+
"print(scale_modified([1,2,3], 5))"
174184
]
175185
},
176186
{
@@ -433,17 +443,35 @@
433443
},
434444
{
435445
"cell_type": "code",
436-
"execution_count": 28,
446+
"execution_count": 8,
437447
"metadata": {},
438448
"outputs": [
439449
{
440450
"output_type": "stream",
441451
"name": "stdout",
442-
"text": "1\n2\n3\n5\n6\n10\n15\n30\n"
452+
"text": [
453+
"1\n100\n2\n50\n4\n25\n5\n20\n10\n\n1\n2\n4\n5\n10\n20\n25\n50\n100\nCPU times: user 60 µs, sys: 32 µs, total: 92 µs\nWall time: 92.3 µs\nCPU times: user 60 µs, sys: 31 µs, total: 91 µs\nWall time: 92.7 µs\n49 49\n"
454+
]
443455
}
444456
],
445457
"source": [
446-
"def factors(n): # generator that computes factors\n",
458+
"## Original implementation not monotonic.\n",
459+
"def factors(n):\n",
460+
" k = 1\n",
461+
" while k * k < n:\n",
462+
" if n % k == 0:\n",
463+
" yield k\n",
464+
" yield n // k\n",
465+
" k += 1\n",
466+
" if k * k == n:\n",
467+
" yield k\n",
468+
"\n",
469+
"# Modified generator that returns factors in ascending order\n",
470+
"# is probably more efficent memory wise but only by halving \n",
471+
"# the memory usage. I don't think this is quite what the authors\n",
472+
"# are asking for\n",
473+
"\n",
474+
"def factors_modified(n): \n",
447475
" k = 1\n",
448476
" factors_buffer = []\n",
449477
" while k * k < n: # while k < sqrt(n)\n",
@@ -456,8 +484,16 @@
456484
" for i in reversed(factors_buffer):\n",
457485
" yield i\n",
458486
"\n",
459-
"for i in factors(30):\n",
460-
" print(i)"
487+
"for i in factors(100):\n",
488+
" print(i)\n",
489+
"\n",
490+
"print()\n",
491+
"\n",
492+
"for i in factors_modified(100):\n",
493+
" print(i)\n",
494+
"\n",
495+
"%time a = [i for i in factors(1000000)]\n",
496+
"%time b = [i for i in factors_modified(1000000)]\n"
461497
]
462498
},
463499
{
@@ -482,9 +518,13 @@
482518
],
483519
"metadata": {
484520
"kernelspec": {
485-
"display_name": "Python 3.6.4 32-bit",
486-
"language": "python",
487-
"name": "python36432bit1ab04e83ff0a419f815752278ad8527e"
521+
"name": "Python 3.8.5 64-bit",
522+
"display_name": "Python 3.8.5 64-bit",
523+
"metadata": {
524+
"interpreter": {
525+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
526+
}
527+
}
488528
},
489529
"language_info": {
490530
"codemirror_mode": {
@@ -496,7 +536,7 @@
496536
"name": "python",
497537
"nbconvert_exporter": "python",
498538
"pygments_lexer": "ipython3",
499-
"version": "3.6.4-final"
539+
"version": "3.8.5-final"
500540
}
501541
},
502542
"nbformat": 4,

book-notes/data-structures-and-algorithms-in-python-by-goodrich-et-al/chapter-01-python-primer/chapter-01-python-primer-exercises-(projects).ipynb

+73-14
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,54 @@
7474
},
7575
{
7676
"cell_type": "code",
77-
"execution_count": 3,
77+
"execution_count": 12,
7878
"metadata": {},
79-
"outputs": [],
79+
"outputs": [
80+
{
81+
"output_type": "error",
82+
"ename": "TypeError",
83+
"evalue": "__new__() missing 1 required positional argument: 'type'",
84+
"traceback": [
85+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
86+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
87+
"\u001b[0;32m<ipython-input-12-11370aa23c19>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\"Two Pence Coin\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"coin\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \"One Pence Coin\", 1, \"coin\"]\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mCurrency\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmoney\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
88+
"\u001b[0;32m<ipython-input-12-11370aa23c19>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\"Two Pence Coin\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"coin\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \"One Pence Coin\", 1, \"coin\"]\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mCurrency\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mb\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmoney\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
89+
"\u001b[0;31mTypeError\u001b[0m: __new__() missing 1 required positional argument: 'type'"
90+
]
91+
}
92+
],
8093
"source": [
81-
"a,b = 2.34, 1.82\n",
94+
"from collections import namedtuple\n",
95+
"\n",
96+
"Currency = namedtuple(\"NoteorCoin\", \"value type\")\n",
97+
"\n",
98+
"money = [\"Fifty Pound Note\", 5000, \"note\",\n",
99+
" \"Twenty Pound Note\", 2000, \"note\",\n",
100+
" \"Ten Pound Note\", 1000, \"note\",\n",
101+
" \"Five Pound Note\", 500, \"note\",\n",
102+
" \"One Pound Coin\", 100, \"coin\",\n",
103+
" \"Fifty Pence Coin\", 50, \"coin\",\n",
104+
" \"Twenty Pence Coin\", 20, \"coin\",\n",
105+
" \"Ten Pence Coin\", 10, \"coin\",\n",
106+
" \"Five Pence Coin\", 5, \"coin\",\n",
107+
" \"Two Pence Coin\", 2, \"coin\",\n",
108+
" \"One Pence Coin\", 1, \"coin\"]\n",
109+
"a = [Currency(b) for b in money]\n",
82110
"\n",
83-
"money = {\"Fifty Pound Note\": 5000, \n",
84-
" \"Twenty Pound Note\": 2000,\n",
85-
" \"Ten Pound Note\": 1000}"
111+
"\n",
112+
"def make_change(price, cash, pence=False):\n",
113+
" \n",
114+
" if not pence:\n",
115+
" change = round(price * 100 - cash * 100, None)\n",
116+
" else:\n",
117+
" change = price - cash\n",
118+
"\n",
119+
" \n",
120+
" \n",
121+
" return change\n",
122+
"\n",
123+
"print(make_change(2.34, 1.82))\n",
124+
"print(make_change(234, 182, pence=True))"
86125
]
87126
},
88127
{
@@ -108,10 +147,26 @@
108147
},
109148
{
110149
"cell_type": "code",
111-
"execution_count": null,
150+
"execution_count": 18,
112151
"metadata": {},
113-
"outputs": [],
114-
"source": []
152+
"outputs": [
153+
{
154+
"output_type": "stream",
155+
"name": "stdout",
156+
"text": [
157+
"2\n",
158+
"2\n",
159+
"3\n"
160+
]
161+
}
162+
],
163+
"source": [
164+
"calculate = \"\"\n",
165+
"\n",
166+
"while calculate != \"\":\n",
167+
" a = input()\n",
168+
" print(a)"
169+
]
115170
},
116171
{
117172
"cell_type": "markdown",
@@ -268,9 +323,13 @@
268323
],
269324
"metadata": {
270325
"kernelspec": {
271-
"display_name": "Python 3.6.4 32-bit",
272-
"language": "python",
273-
"name": "python36432bit1ab04e83ff0a419f815752278ad8527e"
326+
"name": "Python 3.8.5 64-bit",
327+
"display_name": "Python 3.8.5 64-bit",
328+
"metadata": {
329+
"interpreter": {
330+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
331+
}
332+
}
274333
},
275334
"language_info": {
276335
"codemirror_mode": {
@@ -282,9 +341,9 @@
282341
"name": "python",
283342
"nbconvert_exporter": "python",
284343
"pygments_lexer": "ipython3",
285-
"version": "3.6.4"
344+
"version": "3.8.5-final"
286345
}
287346
},
288347
"nbformat": 4,
289348
"nbformat_minor": 2
290-
}
349+
}

0 commit comments

Comments
 (0)