|
| 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