Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f4ad6d

Browse files
committed
Add exercises for std lib notebooks
1 parent 872d766 commit 8f4ad6d

File tree

2 files changed

+350
-0
lines changed

2 files changed

+350
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# pytz will be needed in the exercise\n",
10+
"import sys\n",
11+
"!{sys.executable} -m pip install pytz"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"# 1. Playing with datetimes\n",
19+
"You're given a naive datetime, see `NAIVE_DT` variable below. Although this variable is naive, you happen to know that the time specified by `NAIVE_DT` is in UTC.\n",
20+
"\n",
21+
"Based on this information, your task is to create new datetime variables by converting `NAIVE_DT` to UTC and then to time in Sydney and Los Angeles. Use the following variable names: `utc_dt`, `sydney_dt`, and `la_dt`. "
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {
28+
"editable": false
29+
},
30+
"outputs": [],
31+
"source": [
32+
"import datetime as dt\n",
33+
"import pytz\n",
34+
"\n",
35+
"NAIVE_DT = dt.datetime(2000, 1, 1, 10)"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"If you don't know the timezone name you're looking for, this may be helpful:"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {
49+
"editable": false
50+
},
51+
"outputs": [],
52+
"source": [
53+
"for tz in pytz.all_timezones:\n",
54+
" print(tz)"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"Now create `utc_dt`, `sydney_dt`, and `la_dt`."
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"# Your implementation here"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"Let's verify that the solution is correct."
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {
84+
"editable": false
85+
},
86+
"outputs": [],
87+
"source": [
88+
"assert utc_dt.isoformat() == '2000-01-01T10:00:00+00:00'\n",
89+
"assert sydney_dt.isoformat() == '2000-01-01T21:00:00+11:00'\n",
90+
"assert la_dt.isoformat() == '2000-01-01T02:00:00-08:00'\n",
91+
"\n",
92+
"print('All good!')"
93+
]
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "Python 3",
99+
"language": "python",
100+
"name": "python3"
101+
},
102+
"language_info": {
103+
"codemirror_mode": {
104+
"name": "ipython",
105+
"version": 3
106+
},
107+
"file_extension": ".py",
108+
"mimetype": "text/x-python",
109+
"name": "python",
110+
"nbconvert_exporter": "python",
111+
"pygments_lexer": "ipython3",
112+
"version": "3.5.4"
113+
}
114+
},
115+
"nbformat": 4,
116+
"nbformat_minor": 2
117+
}
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 1. Let's mock things!\n",
8+
"Below you can see `get_wiki_article` function which is a very simple implementation for fetching an article from wikipedia. Your task is to mock it's implementation such that it's going to always return `'Python is cool!'`. However, note that you should be able to check which argument is given to `urlopen` when `get_wiki_article` is called.\n",
9+
"\n",
10+
"**Note**: `get_content_of_url` uses [`urrlib`](https://docs.python.org/3/library/urllib.html#module-urllib), which is part of the Standard Library, for creating a HTTP request. Usually it's preferable to use [`requests`](http://docs.python-requests.org/en/master/) library (not part of the Standard Library) for such operations. Actually, `requests` uses `urllib` under the hood so it's good to know what's happening when you start using `requests` - or maybe you have already used it."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"metadata": {
17+
"editable": false
18+
},
19+
"outputs": [],
20+
"source": [
21+
"from urllib.request import urlopen\n",
22+
"\n",
23+
"def get_wiki_article(name):\n",
24+
" url = 'https://en.wikipedia.org/wiki/{}'.format(name)\n",
25+
" response = urlopen(url)\n",
26+
" content = str(response.read())\n",
27+
" return content"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"# Your implementation here\n"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"Let's verify it works as expected."
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {
50+
"editable": false
51+
},
52+
"outputs": [],
53+
"source": [
54+
"article = 'Python_(programming_language)'\n",
55+
"res = get_wiki_article(article)\n",
56+
"assert 'Guido van Rossum' not in res, 'Guido is still there!'\n",
57+
"assert res == 'Python is cool!'\n",
58+
"urlopen.assert_called_with('https://en.wikipedia.org/wiki/Python_(programming_language)')\n",
59+
"\n",
60+
"print('All good!')"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"# 2. The power of `collections` module"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"## 2.1 Creating a namedtuple\n",
75+
"Create a namedtuple `Car` which has fields `price`, `mileage`, and `brand`. "
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"# Your implemenation here"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"Let's test it."
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {
98+
"editable": false
99+
},
100+
"outputs": [],
101+
"source": [
102+
"car1 = Car(25000, 2000, 'BMW')\n",
103+
"assert car1.price == 25000\n",
104+
"assert car1.mileage == 2000\n",
105+
"assert car1.brand == 'BMW'\n",
106+
"assert isinstance(car1, tuple)\n",
107+
"\n",
108+
"# Note that indexing works also!\n",
109+
"# This means that if you change a tuple into a namedtuple,\n",
110+
"# the change will be backwards compatible.\n",
111+
"assert car1[2] == 'BMW'\n",
112+
"\n",
113+
"print('All good!')"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"The power of namedtuples is their simplicity. If `Car` would have been implemented as a class, the implementation would have been notably longer. However, if you would need to be able to e.g. change the `mileage` or `price` during the lifetime of a `Car` instance, consider using `class` because `tuples` are immutable."
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"## 2.2 dict of dicts\n",
128+
"Implement a `name_mapping` function which takes a collection of names as argument. \n",
129+
"\n",
130+
"#### The specification for `name_mapping`\n",
131+
"* you can assume that all the elements in the names collection are strings\n",
132+
"* if the provided names collection is empty, returns an empty dict\n",
133+
"* returns a dictionary of dictionaries\n",
134+
" * outer dictionary should contain keys `vowel` and `consonant`\n",
135+
" * `vowel` and `consonant` keys should have dictionaries of names (keys) and their occurences (values) as values\n",
136+
" * names belong to either `vowel` or `consonant` based on their first letter\n",
137+
" * vowels are defined by the `VOWELS` constant\n",
138+
" * if there are only names starting with a vowel, `consonant` key should not be present in the return value (same applies vice versa)\n",
139+
"* see the tests below for complete examples \n",
140+
"\n",
141+
"Tip: `defaultdict` and `Counter` may be helpful here :)"
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": null,
147+
"metadata": {
148+
"editable": false
149+
},
150+
"outputs": [],
151+
"source": [
152+
"VOWELS = ('a', 'e', 'i', 'o', 'u')"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"def name_mapping(names):\n",
162+
" # Your implementation here"
163+
]
164+
},
165+
{
166+
"cell_type": "markdown",
167+
"metadata": {},
168+
"source": [
169+
"Let's verify that it works correctly!"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {
176+
"editable": false
177+
},
178+
"outputs": [],
179+
"source": [
180+
"names = ('Alice', 'John', 'Lisa', 'John', 'Eric', 'Waldo', 'annie', 'Alice', 'John')\n",
181+
"expected = {\n",
182+
" 'consonant': {\n",
183+
" 'John': 3,\n",
184+
" 'Waldo': 1,\n",
185+
" 'Lisa': 1\n",
186+
" },\n",
187+
" 'vowel': {\n",
188+
" 'Alice': 2,\n",
189+
" 'annie': 1,\n",
190+
" 'Eric': 1\n",
191+
" }\n",
192+
"}\n",
193+
"assert name_mapping(names) == expected\n",
194+
"print('First ok!')\n",
195+
"\n",
196+
"only_consonants = ('John', 'Doe', 'Doe')\n",
197+
"expected2 = {\n",
198+
" 'consonant': {\n",
199+
" 'John': 1,\n",
200+
" 'Doe': 2\n",
201+
" }\n",
202+
"}\n",
203+
"assert name_mapping(only_consonants) == expected2\n",
204+
"print('Second ok!')\n",
205+
"\n",
206+
"assert name_mapping([]) == {}\n",
207+
"\n",
208+
"print('All ok!')"
209+
]
210+
}
211+
],
212+
"metadata": {
213+
"kernelspec": {
214+
"display_name": "Python 3",
215+
"language": "python",
216+
"name": "python3"
217+
},
218+
"language_info": {
219+
"codemirror_mode": {
220+
"name": "ipython",
221+
"version": 3
222+
},
223+
"file_extension": ".py",
224+
"mimetype": "text/x-python",
225+
"name": "python",
226+
"nbconvert_exporter": "python",
227+
"pygments_lexer": "ipython3",
228+
"version": "3.5.4"
229+
}
230+
},
231+
"nbformat": 4,
232+
"nbformat_minor": 2
233+
}

0 commit comments

Comments
 (0)