Skip to content

Commit 214bed9

Browse files
author
Fred Baptiste
committed
Notebooks for Part 3
1 parent 52a031d commit 214bed9

File tree

49 files changed

+37196
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+37196
-0
lines changed

Part 3/Section 03 - Dictionaries/01 - Creating Python Dictionaries.ipynb

Lines changed: 1195 additions & 0 deletions
Large diffs are not rendered by default.

Part 3/Section 03 - Dictionaries/02 - Common Operations.ipynb

Lines changed: 1311 additions & 0 deletions
Large diffs are not rendered by default.

Part 3/Section 03 - Dictionaries/03 - Dictionary Views.ipynb

Lines changed: 1293 additions & 0 deletions
Large diffs are not rendered by default.

Part 3/Section 03 - Dictionaries/04 - Updating, Merging and Copying.ipynb

Lines changed: 1260 additions & 0 deletions
Large diffs are not rendered by default.

Part 3/Section 03 - Dictionaries/05 - Custom Classes and Hashing.ipynb

Lines changed: 2193 additions & 0 deletions
Large diffs are not rendered by default.

Part 3/Section 04 - Coding Exercises/01 - Coding Exercises.ipynb

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Coding Exercises - Solution 1"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"#### Exercise 1"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"Write a Python function that will create and return a dictionary from another dictionary, but sorted by value. You can assume the values are all comparable and have a natural sort order."
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"For example, given the following dictionary:"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 1,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"composers = {'Johann': 65, 'Ludwig': 56, 'Frederic': 39, 'Wolfgang': 35}"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"Your function should return a dictionary that looks like the following:"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 2,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"sorted_composers = {'Wolfgang': 35,\n",
54+
" 'Frederic': 39, \n",
55+
" 'Ludwig': 56,\n",
56+
" 'Johann': 65}"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"Remember if you are using Jupyter notebook to use `print()` to view your dictionary in it's natural ordering (Jupyter will display your dictionary sorted by key).\n",
64+
"\n",
65+
"Also try to keep your code Pythonic - i.e. don't start with an empty dictionary and build it up one key at a time - look for a different, more Pythonic, way of doing it. \n",
66+
"\n",
67+
"Hint: you'll likely want to use Python's `sorted` function."
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"##### Solution"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"My approach here is to sort the `items()` view using Python's `sorted` function and a custom `key` that uses the dictionary values (or second element of each tuple in the `items` view):"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 3,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"composers = {'Johann': 65, 'Ludwig': 56, 'Frederic': 39, 'Wolfgang': 35}\n",
91+
"\n",
92+
"def sort_dict_by_value(d):\n",
93+
" d = {k: v\n",
94+
" for k, v in sorted(d.items(), key=lambda el: el[1])}\n",
95+
" return d"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 4,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"name": "stdout",
105+
"output_type": "stream",
106+
"text": [
107+
"{'Wolfgang': 35, 'Frederic': 39, 'Ludwig': 56, 'Johann': 65}\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"print(sort_dict_by_value(composers))"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"Here's a better approach - instead of using a dictionary comprehension, we can simply use the `dict()` function to create a dictionary from the sorted tuples!"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 5,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"def sort_dict_by_value(d):\n",
129+
" return dict(sorted(d.items(), key=lambda el: el[1]))"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"And we end up with the same end result:"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 6,
142+
"metadata": {},
143+
"outputs": [
144+
{
145+
"data": {
146+
"text/plain": [
147+
"{'Wolfgang': 35, 'Frederic': 39, 'Ludwig': 56, 'Johann': 65}"
148+
]
149+
},
150+
"execution_count": 6,
151+
"metadata": {},
152+
"output_type": "execute_result"
153+
}
154+
],
155+
"source": [
156+
"sort_dict_by_value(composers)"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"metadata": {},
163+
"outputs": [],
164+
"source": []
165+
}
166+
],
167+
"metadata": {
168+
"kernelspec": {
169+
"display_name": "Python 3",
170+
"language": "python",
171+
"name": "python3"
172+
},
173+
"language_info": {
174+
"codemirror_mode": {
175+
"name": "ipython",
176+
"version": 3
177+
},
178+
"file_extension": ".py",
179+
"mimetype": "text/x-python",
180+
"name": "python",
181+
"nbconvert_exporter": "python",
182+
"pygments_lexer": "ipython3",
183+
"version": "3.6.7"
184+
}
185+
},
186+
"nbformat": 4,
187+
"nbformat_minor": 2
188+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Coding Exercises - Solution 2"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"#### Exercise 2"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"Given two dictionaries, `d1` and `d2`, write a function that creates a dictionary that contains only the keys common to both dictionaries, with values being a tuple containg the values from `d1` and `d2`. (Order of keys is not important)."
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"For example, given two dictionaries as follows:"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 1,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"d1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n",
38+
"d2 = {'b': 20, 'c': 30, 'y': 40, 'z': 50}"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"Your function should return a dictionary that looks like this:"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"d = {'b': (2, 20), 'c': (3, 30)}"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"Hint: Remember that `s1 & s2` will return the intersection of two sets."
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"Again, try to keep your code Pythonic - don't just start with an empty dictionary and build it up one by one - think of a cleaner approach."
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {},
74+
"source": [
75+
"##### Solution"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"My approach here is to use set intersections to find the keys common to both dictionaries.\n",
83+
"Then I use a dictionary comprehension to build up my new dictionary, making each value in the new dictionary a tuple containing the values from the original dictionaries:"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 3,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"d1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n",
93+
"d2 = {'b': 20, 'c': 30, 'y': 40, 'z': 50}\n",
94+
"\n",
95+
"def intersect(d1, d2):\n",
96+
" d1_keys = d1.keys()\n",
97+
" d2_keys = d2.keys()\n",
98+
" keys = d1_keys & d2_keys\n",
99+
" d = {k: (d1[k], d2[k]) for k in keys}\n",
100+
" return d"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 4,
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"data": {
110+
"text/plain": [
111+
"{'b': (2, 20), 'c': (3, 30)}"
112+
]
113+
},
114+
"execution_count": 4,
115+
"metadata": {},
116+
"output_type": "execute_result"
117+
}
118+
],
119+
"source": [
120+
"intersect(d1, d2)"
121+
]
122+
}
123+
],
124+
"metadata": {
125+
"kernelspec": {
126+
"display_name": "Python 3",
127+
"language": "python",
128+
"name": "python3"
129+
},
130+
"language_info": {
131+
"codemirror_mode": {
132+
"name": "ipython",
133+
"version": 3
134+
},
135+
"file_extension": ".py",
136+
"mimetype": "text/x-python",
137+
"name": "python",
138+
"nbconvert_exporter": "python",
139+
"pygments_lexer": "ipython3",
140+
"version": "3.6.7"
141+
}
142+
},
143+
"nbformat": 4,
144+
"nbformat_minor": 2
145+
}

0 commit comments

Comments
 (0)