Add exercises for std lib notebooks
This commit is contained in:
117
notebooks/beginner/exercises/std_lib1_exercise.ipynb
Normal file
117
notebooks/beginner/exercises/std_lib1_exercise.ipynb
Normal file
@@ -0,0 +1,117 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# pytz will be needed in the exercise\n",
|
||||
"import sys\n",
|
||||
"!{sys.executable} -m pip install pytz"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 1. Playing with datetimes\n",
|
||||
"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",
|
||||
"\n",
|
||||
"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`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import datetime as dt\n",
|
||||
"import pytz\n",
|
||||
"\n",
|
||||
"NAIVE_DT = dt.datetime(2000, 1, 1, 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If you don't know the timezone name you're looking for, this may be helpful:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for tz in pytz.all_timezones:\n",
|
||||
" print(tz)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now create `utc_dt`, `sydney_dt`, and `la_dt`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Your implementation here"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's verify that the solution is correct."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert utc_dt.isoformat() == '2000-01-01T10:00:00+00:00'\n",
|
||||
"assert sydney_dt.isoformat() == '2000-01-01T21:00:00+11:00'\n",
|
||||
"assert la_dt.isoformat() == '2000-01-01T02:00:00-08:00'\n",
|
||||
"\n",
|
||||
"print('All good!')"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
233
notebooks/intermediate/exercises/std_lib2_exercise.ipynb
Normal file
233
notebooks/intermediate/exercises/std_lib2_exercise.ipynb
Normal file
@@ -0,0 +1,233 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 1. Let's mock things!\n",
|
||||
"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",
|
||||
"\n",
|
||||
"**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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from urllib.request import urlopen\n",
|
||||
"\n",
|
||||
"def get_wiki_article(name):\n",
|
||||
" url = 'https://en.wikipedia.org/wiki/{}'.format(name)\n",
|
||||
" response = urlopen(url)\n",
|
||||
" content = str(response.read())\n",
|
||||
" return content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Your implementation here\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's verify it works as expected."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"article = 'Python_(programming_language)'\n",
|
||||
"res = get_wiki_article(article)\n",
|
||||
"assert 'Guido van Rossum' not in res, 'Guido is still there!'\n",
|
||||
"assert res == 'Python is cool!'\n",
|
||||
"urlopen.assert_called_with('https://en.wikipedia.org/wiki/Python_(programming_language)')\n",
|
||||
"\n",
|
||||
"print('All good!')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# 2. The power of `collections` module"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2.1 Creating a namedtuple\n",
|
||||
"Create a namedtuple `Car` which has fields `price`, `mileage`, and `brand`. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Your implemenation here"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's test it."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"car1 = Car(25000, 2000, 'BMW')\n",
|
||||
"assert car1.price == 25000\n",
|
||||
"assert car1.mileage == 2000\n",
|
||||
"assert car1.brand == 'BMW'\n",
|
||||
"assert isinstance(car1, tuple)\n",
|
||||
"\n",
|
||||
"# Note that indexing works also!\n",
|
||||
"# This means that if you change a tuple into a namedtuple,\n",
|
||||
"# the change will be backwards compatible.\n",
|
||||
"assert car1[2] == 'BMW'\n",
|
||||
"\n",
|
||||
"print('All good!')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2.2 dict of dicts\n",
|
||||
"Implement a `name_mapping` function which takes a collection of names as argument. \n",
|
||||
"\n",
|
||||
"#### The specification for `name_mapping`\n",
|
||||
"* you can assume that all the elements in the names collection are strings\n",
|
||||
"* if the provided names collection is empty, returns an empty dict\n",
|
||||
"* returns a dictionary of dictionaries\n",
|
||||
" * outer dictionary should contain keys `vowel` and `consonant`\n",
|
||||
" * `vowel` and `consonant` keys should have dictionaries of names (keys) and their occurences (values) as values\n",
|
||||
" * names belong to either `vowel` or `consonant` based on their first letter\n",
|
||||
" * vowels are defined by the `VOWELS` constant\n",
|
||||
" * if there are only names starting with a vowel, `consonant` key should not be present in the return value (same applies vice versa)\n",
|
||||
"* see the tests below for complete examples \n",
|
||||
"\n",
|
||||
"Tip: `defaultdict` and `Counter` may be helpful here :)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"VOWELS = ('a', 'e', 'i', 'o', 'u')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def name_mapping(names):\n",
|
||||
" # Your implementation here"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's verify that it works correctly!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"editable": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"names = ('Alice', 'John', 'Lisa', 'John', 'Eric', 'Waldo', 'annie', 'Alice', 'John')\n",
|
||||
"expected = {\n",
|
||||
" 'consonant': {\n",
|
||||
" 'John': 3,\n",
|
||||
" 'Waldo': 1,\n",
|
||||
" 'Lisa': 1\n",
|
||||
" },\n",
|
||||
" 'vowel': {\n",
|
||||
" 'Alice': 2,\n",
|
||||
" 'annie': 1,\n",
|
||||
" 'Eric': 1\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"assert name_mapping(names) == expected\n",
|
||||
"print('First ok!')\n",
|
||||
"\n",
|
||||
"only_consonants = ('John', 'Doe', 'Doe')\n",
|
||||
"expected2 = {\n",
|
||||
" 'consonant': {\n",
|
||||
" 'John': 1,\n",
|
||||
" 'Doe': 2\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"assert name_mapping(only_consonants) == expected2\n",
|
||||
"print('Second ok!')\n",
|
||||
"\n",
|
||||
"assert name_mapping([]) == {}\n",
|
||||
"\n",
|
||||
"print('All ok!')"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user