├── lectures ├── 5_System_demonstration.md └── type_demo.py ├── assignments ├── 5_System_demonstration.md ├── 3_Object_oriented_programming_lab.ipynb ├── 4_List_comprehension_functional_lab.ipynb ├── 2_Builtin_types_exercises.ipynb └── 1_Python_introduction_exercises.ipynb ├── LICENSE ├── .gitignore ├── README.md ├── dopp_test_sample_questions.ipynb └── self_assessment.ipynb /lectures/5_System_demonstration.md: -------------------------------------------------------------------------------- 1 | # TUW Programming in Python 2022WS - Lecture 5 2 | 3 | For the fifth lecture/exercise you can find all the material in the [public lecture repository]( 4 | https://github.com/adaamko/tuw-python-2022ws-assignment5-starter-code). You can also find all the instructions in the [README.md](https://github.com/adaamko/tuw-python-2022ws-assignment5-starter-code/blob/main/README.md) file. -------------------------------------------------------------------------------- /assignments/5_System_demonstration.md: -------------------------------------------------------------------------------- 1 | # TUW Programming in Python 2022WS - Lab 5 2 | 3 | For the tasks of the fifth exercise you can find all the material in the [public lecture repository]( 4 | https://github.com/adaamko/tuw-python-2022ws-assignment5-starter-code). You can also find all the instructions in the [README.md](https://github.com/adaamko/tuw-python-2022ws-assignment5-starter-code/blob/main/README.md) file. -------------------------------------------------------------------------------- /lectures/type_demo.py: -------------------------------------------------------------------------------- 1 | from typing import Any, Union, Optional, Sequence 2 | 3 | 4 | def foo(value: float) -> None: 5 | print(value, value + 1) 6 | 7 | 8 | def bar(value: int) -> int: 9 | foo(value) 10 | return 0 11 | 12 | 13 | def happy_birthday(name: str, age: Union[int, str]) -> str: 14 | return f"Happy {age}th birthday, {name}" 15 | 16 | 17 | def print_all(elements: Sequence, prefix: Optional[str] = None) -> None: 18 | for e in elements: 19 | if prefix: 20 | print(f"{prefix}: {e}") 21 | else: 22 | print(e) 23 | 24 | 25 | def call_happy(name: str, age: Union[list, int, str]) -> None: 26 | print(happy_birthday(name, age)) 27 | 28 | 29 | def main(): 30 | # bar(42) 31 | call_happy("John", [1, 2, 3]) 32 | # print_all([1, "abc", "def"]) 33 | 34 | 35 | if __name__ == '__main__': 36 | main() 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 tuw-python 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 194.123 Programming in Python 2 | ### TU Wien, 2022 WS 3 | 4 | [TISS page](https://tiss.tuwien.ac.at/course/educationDetails.xhtml?dswid=2845&dsrid=656&courseNr=194123&semester=2022W) 5 | 6 | [Lecture videos](https://www.youtube.com/playlist?list=PLrrW7I2LNLRYROQzeCKFhmDjd2vH7BJSi) 7 | 8 | ### Time and place 9 | 10 | - Every day between 2022.09.26 (Mo) - 09.30 (Fr) 11 | 12 | - Daily from 10.15 - 11.45 (lectures) and 13.15 - 15.30 (practice) 13 | 14 | - Using [Zoom](https://tuwien.zoom.us/j/94257349304?pwd=dVE4WmJMSlVoTEtRbDJvOWExU2wxQT09) and [Slack](https://join.slack.com/t/tuw-python/shared_invite/zt-1g2dom5fa-M~lZwH56tsfuLn67U3jGrw) 15 | 16 | ### Instructors 17 | 18 | - [Judit Ács](https://hlt.bme.hu/en/judit) 19 | - [Gabriel Breiner](https://tiss.tuwien.ac.at/person/324397) 20 | - [Kinga Gémes](https://tiss.tuwien.ac.at/person/341880.html) 21 | - [Ádám Kovács](https://tiss.tuwien.ac.at/person/341881.html) 22 | - [Gábor Recski](https://tiss.tuwien.ac.at/person/336863.html) 23 | 24 | Administrative questions should be directed to Gabor Recski 25 | 26 | ### Schedule 27 | 28 | Date|Topic| | 29 | ----|-----|--| 30 | 09/26/2022 | Introduction to Python, basic types. Using git for version control. | | 31 | 09/27/2022 | Advanced types, operators, strings. Functions, lambda functions. | | 32 | 09/28/2022 | Object-oriented programming. Classes, attributes, inheritence, magic functions, static methods. | | 33 | 09/29/2022 | List comprehensions, decorators, functional programming. | | 34 | 09/30/2022 | Common Python modules. collection, re, networkx, itertools | | 35 | 36 | ### Who should take this course 37 | 38 | Anyone with knowledge of programming basics who would like to learn (more) about 39 | Python and get practical experience. 40 | 41 | [Self-assessment questions](self_assessment.ipynb) are provided, please have a look 42 | if you are about to attend [188.995 Data-oriented Programming Paradigms](https://tiss.tuwien.ac.at/course/educationDetails.xhtml?dswid=2344&dsrid=881&courseNr=188995&semester=2022W). 43 | 44 | 45 | ### Evaluation 46 | 47 | Homework exercises will be released on each of the first 4 days. Solutions must be 48 | submitted via GitHub classroom, links for this will be available to registered students 49 | on [TUWEL](https://tuwel.tuwien.ac.at/course/view.php?idnumber=194123-2022W). The 50 | deadline for submitting all exercises is Thursday (9/29) 23.59 (CEST), scores will be 51 | published the next morning. The exercises on Friday can be used to earn 52 | bonus points. 53 | 54 | __DEADLINE EXTENSION: We decided to extend the deadline for submissions. The new deadline for __ALL EXERCISES__ is Sunday (10/2) 23.59 (CEST). You can expect the scores to be published next week.__ 55 | 56 | ### Grading 57 | 58 | Each of the first 4 daily exercises are worth 25 points, for a maximum of 100 points. 59 | The Friday exercise is worth a maximum of 15 bonus points. 60 | 61 | Final grades will be determined using the following ranges: 62 | 63 | 1: 89 – 115, 2: 76 – 88, 3: 63 – 75, 4: 50 – 62 64 | -------------------------------------------------------------------------------- /dopp_test_sample_questions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### The DOPP Python test will be an in-person paper-based test. Below are some sample test questions from each topic." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Built-in types\n", 15 | "\n", 16 | "## 1. What is the return value of the last expression of the following cells?" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": null, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "i = 3\n", 26 | "i ** 3, i % 2, abs(i - 5)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": null, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "l = list(range(0, 20, 2))\n", 36 | "l[-5:-2]" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": {}, 43 | "outputs": [], 44 | "source": [ 45 | "t = tuple([3, 2, 1, 1])\n", 46 | "t[1] += 1" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "## 2. Turn string A into string B using string formatting methods alone? Do not try to change the individual letters of the string and do not take substrings.\n", 54 | "\n", 55 | "| input | output |\n", 56 | "| ---- | ---- |\n", 57 | "|`\"ABC\"` | `\"abc\"` |\n", 58 | "|`\"ABC \\n\"` | `\"abc\"` |\n", 59 | "|`\"word1_word2_word3\"` | `\"word1 word2 word3\"` |" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "## 3. Write a function that takes three parameters with default values. Demonstrate that it can be called with zero, one, two or three parameters." 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "# OOP" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "## 1. How do you define a class in Python? Write an example that can be initialized with 2 parameters!" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "## 2. Consider the following code snippet. What will the output be?" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "class A:\n", 104 | " def method(self):\n", 105 | " print(\"A\")\n", 106 | " \n", 107 | "class B:\n", 108 | " def method(self):\n", 109 | " print(\"B\")\n", 110 | "\n", 111 | "b = B()\n", 112 | "b.method()" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": null, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "a = 12\n", 122 | "b = 13\n", 123 | "assert a == b, \"a and b are not the same\"" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "## 3. How can we prevent a code from raising an error? The following function will raise a ZeroDivisionException. Wrap it so, that it will only print out `Error` instead of raising one." 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "def problem():\n", 140 | " return 1 / 0" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "# List comprehension, context managers and functional programming" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "## 1. Transform the following code into a list comprehension!" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "l = []\n", 164 | "for i in range(10):\n", 165 | " l.append(i * 3)" 166 | ] 167 | }, 168 | { 169 | "cell_type": "markdown", 170 | "metadata": {}, 171 | "source": [ 172 | "## 2. What will be the output of the following code snippet?" 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": null, 178 | "metadata": {}, 179 | "outputs": [], 180 | "source": [ 181 | "def func(i: int) -> int:\n", 182 | " return i * 2\n", 183 | "\n", 184 | "l = [1, 2, 3, 4, 5]\n", 185 | "print(list(map(func, l)))" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "## 3. What elements will the following list contain?" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "l = [i for i in range(10) if i % 2 == 0]" 202 | ] 203 | } 204 | ], 205 | "metadata": { 206 | "kernelspec": { 207 | "display_name": "Python 3 (ipykernel)", 208 | "language": "python", 209 | "name": "python3" 210 | }, 211 | "language_info": { 212 | "codemirror_mode": { 213 | "name": "ipython", 214 | "version": 3 215 | }, 216 | "file_extension": ".py", 217 | "mimetype": "text/x-python", 218 | "name": "python", 219 | "nbconvert_exporter": "python", 220 | "pygments_lexer": "ipython3", 221 | "version": "3.7.9" 222 | }, 223 | "toc": { 224 | "base_numbering": 1, 225 | "nav_menu": {}, 226 | "number_sections": true, 227 | "sideBar": true, 228 | "skip_h1_title": false, 229 | "title_cell": "Table of Contents", 230 | "title_sidebar": "Contents", 231 | "toc_cell": false, 232 | "toc_position": {}, 233 | "toc_section_display": true, 234 | "toc_window_display": false 235 | }, 236 | "vscode": { 237 | "interpreter": { 238 | "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" 239 | } 240 | } 241 | }, 242 | "nbformat": 4, 243 | "nbformat_minor": 2 244 | } 245 | -------------------------------------------------------------------------------- /self_assessment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "494a8b10", 6 | "metadata": {}, 7 | "source": [ 8 | "# Self-assessment\n", 9 | "### 194.123 Programming in Python\n", 10 | "**The questions below provide a quick overview of the material covered in the course. If there are more than a few questions that you are unsure about, we recommend that you take this course as a prerequisite to [188.995 Data-oriented Programming Paradigms](https://tiss.tuwien.ac.at/course/educationDetails.xhtml?dswid=2344&dsrid=881&courseNr=188995&semester=2022W)**" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "id": "ef14703e", 16 | "metadata": {}, 17 | "source": [ 18 | "### What is the difference between these import methods? Why is the last one not recommended?" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "id": "8fab73e9", 25 | "metadata": { 26 | "scrolled": true 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "import foo\n", 31 | "from foo import bar\n", 32 | "from foo import *" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "id": "a5f74956", 38 | "metadata": {}, 39 | "source": [ 40 | "### What is the output?" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": null, 46 | "id": "9b6f728e", 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "def add_numbers(param1, param2=2):\n", 51 | " return param1 + param2\n", 52 | "\n", 53 | "print(add_numbers(1, 3))\n", 54 | "print(add_numbers(10))" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "id": "a29b32b0", 60 | "metadata": {}, 61 | "source": [ 62 | "### Why does this fail and how would you fix it?" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "id": "81473025", 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "\"I am \" + 25 + \" years old.\"" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "id": "0b2d214a", 78 | "metadata": {}, 79 | "source": [ 80 | "### What do these string methods do?" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "85c2db97", 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "s = \"AbCD \\n\"\n", 91 | "s.strip()\n", 92 | "s.split()\n", 93 | "s.startswith(\"abc\")\n", 94 | "s.lower().startswith(\"abc\")\n", 95 | "\"CD\" in s" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "id": "7c6c0c70", 101 | "metadata": {}, 102 | "source": [ 103 | "### What is the difference between lists and tuples? Why does the following fail for tuples?" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "186970a3", 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "l = [1, 2, 3]\n", 114 | "l[1] = 10\n", 115 | "l" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "id": "62e7b01e", 122 | "metadata": {}, 123 | "outputs": [], 124 | "source": [ 125 | "t = (1, 2, 3)\n", 126 | "t[1] = 10" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "d26d02b5", 132 | "metadata": {}, 133 | "source": [ 134 | "### What is the size of this dictionary?" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "id": "5819c70f", 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "d = {}\n", 145 | "d[\"a\"] = 1\n", 146 | "len(d)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "id": "ccd9aff0", 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "d[\"b\"] = 2\n", 157 | "d[\"a\"] = 3\n", 158 | "len(d)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "id": "ed972041", 164 | "metadata": {}, 165 | "source": [ 166 | "### What are type hints and what do they check for in this example? Are the function calls correct?" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "517014ee", 173 | "metadata": {}, 174 | "outputs": [], 175 | "source": [ 176 | "def get_sum_string_length(str1: str, str2: str) -> int:\n", 177 | " return len(str1) + len(str2)\n", 178 | "\n", 179 | "\n", 180 | "get_sum_string_length(\"abc\", \"defg\")\n", 181 | "get_sum_string_length(\"abc\", [1, 2, 3])" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "id": "4320de8c", 187 | "metadata": {}, 188 | "source": [ 189 | "### Are both of these class definitions correct?" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": null, 195 | "id": "90bf8d08", 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "class A:\n", 200 | " pass\n", 201 | "\n", 202 | "class B:\n", 203 | " def __init__(self):\n", 204 | " pass" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "id": "19cfd1ca", 210 | "metadata": {}, 211 | "source": [ 212 | "### What will this snippet print out?" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "id": "88ebdd8b", 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [ 222 | "class Fruit:\n", 223 | " def __init__(self):\n", 224 | " print(\"This is a fruit\")\n", 225 | "\n", 226 | "class Apple(Fruit):\n", 227 | " pass\n", 228 | "\n", 229 | "apple = Apple()" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "id": "b7b34229", 235 | "metadata": {}, 236 | "source": [ 237 | "### Why is this code incorrect? How would you fix it?" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "id": "e5c16148", 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "class A:\n", 248 | " @staticmethod\n", 249 | " def method(a):\n", 250 | " self.a = a" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "id": "2b20013f", 256 | "metadata": {}, 257 | "source": [ 258 | "### What will this snippet return?" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": null, 264 | "id": "b18cf8f3", 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [ 268 | "class A:\n", 269 | " def __init__(self):\n", 270 | " self.value = 42\n", 271 | " \n", 272 | " def method(self):\n", 273 | " return self.value\n", 274 | "\n", 275 | "class B(A):\n", 276 | " def method(self):\n", 277 | " return self.value * 10\n", 278 | "\n", 279 | "b = B()\n", 280 | "b.method()" 281 | ] 282 | }, 283 | { 284 | "cell_type": "markdown", 285 | "id": "22e4fc52", 286 | "metadata": {}, 287 | "source": [ 288 | "### What will the following snippet return?" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "id": "dff8c46c", 295 | "metadata": {}, 296 | "outputs": [], 297 | "source": [ 298 | "class A:\n", 299 | " def __init__(self):\n", 300 | " self.attr1 = 12\n", 301 | " \n", 302 | "getattr(A(), 'attr1')" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "id": "29f824dc", 308 | "metadata": {}, 309 | "source": [ 310 | "### What are the elements (in order) of the following list?\n", 311 | "\n", 312 | "Hint: think of flattening a matrix." 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": null, 318 | "id": "f8d0556b", 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "l1 = [[0, 1], [2, 3]]\n", 323 | "\n", 324 | "l2 = [element for l in l1 for element in l]" 325 | ] 326 | }, 327 | { 328 | "cell_type": "markdown", 329 | "id": "c2e1ce56", 330 | "metadata": {}, 331 | "source": [ 332 | "### What will the following snippet print out?" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "id": "99dfa008", 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "def my_generator(limit: int):\n", 343 | " for i in range(limit):\n", 344 | " yield i\n", 345 | "\n", 346 | "gen_obj = my_generator(10)\n", 347 | "for element in gen_obj:\n", 348 | " print(element)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "id": "ff3497ee", 354 | "metadata": {}, 355 | "source": [ 356 | "### What does the `map` function do in this intance? What will the output be?" 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": null, 362 | "id": "32465560", 363 | "metadata": {}, 364 | "outputs": [], 365 | "source": [ 366 | "from typing import Any\n", 367 | "\n", 368 | "def print_with_type(obj: Any):\n", 369 | " print(f\"The value of the object is: {obj}\")\n", 370 | " print(f\"The type of the object is: {type(obj).__name__}\\n\")\n", 371 | "\n", 372 | "mixed_type_list = [[0, 1, 2], (3, 4, 5), \"apples\", {\"apple\": \"fruit\"}]\n", 373 | "\n", 374 | "map_obj = map(print_with_type, mixed_type_list)\n", 375 | "for m in map_obj:\n", 376 | " pass" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "id": "badc426d", 382 | "metadata": {}, 383 | "source": [ 384 | "### How many elements will be printed in the following snippet?" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": null, 390 | "id": "f96f91a7", 391 | "metadata": {}, 392 | "outputs": [], 393 | "source": [ 394 | "l1 = [i for i in range(10)]\n", 395 | "l2 = [i for i in range(100, 2000)]\n", 396 | "\n", 397 | "for e1, e2 in zip(l1, l2):\n", 398 | " print(e1, e2)" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "id": "b2dba463", 404 | "metadata": {}, 405 | "source": [ 406 | "### Why is this code incorrect?" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "id": "2a2fa47a", 413 | "metadata": {}, 414 | "outputs": [], 415 | "source": [ 416 | "with open(\"file.txt\", \"w\") as my_file:\n", 417 | " my_file.write(\"This is a file.\\n\")\n", 418 | "\n", 419 | "my_file.write(\"Oh no!\")" 420 | ] 421 | } 422 | ], 423 | "metadata": { 424 | "kernelspec": { 425 | "display_name": "Python 3 (ipykernel)", 426 | "language": "python", 427 | "name": "python3" 428 | }, 429 | "language_info": { 430 | "codemirror_mode": { 431 | "name": "ipython", 432 | "version": 3 433 | }, 434 | "file_extension": ".py", 435 | "mimetype": "text/x-python", 436 | "name": "python", 437 | "nbconvert_exporter": "python", 438 | "pygments_lexer": "ipython3", 439 | "version": "3.7.9" 440 | }, 441 | "toc": { 442 | "base_numbering": 1, 443 | "nav_menu": {}, 444 | "number_sections": true, 445 | "sideBar": true, 446 | "skip_h1_title": false, 447 | "title_cell": "Table of Contents", 448 | "title_sidebar": "Contents", 449 | "toc_cell": false, 450 | "toc_position": {}, 451 | "toc_section_display": true, 452 | "toc_window_display": false 453 | }, 454 | "vscode": { 455 | "interpreter": { 456 | "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" 457 | } 458 | } 459 | }, 460 | "nbformat": 4, 461 | "nbformat_minor": 5 462 | } 463 | -------------------------------------------------------------------------------- /assignments/3_Object_oriented_programming_lab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", 8 | "\n", 9 | "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "NAME = \"\"\n", 19 | "COLLABORATORS = \"\"" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "---" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "deletable": false, 33 | "editable": false, 34 | "nbgrader": { 35 | "cell_type": "markdown", 36 | "checksum": "b5eeeae7b733b3c7620087ebe9e60fe6", 37 | "grade": false, 38 | "grade_id": "cell-ff8429d25fbc1051", 39 | "locked": true, 40 | "schema_version": 3, 41 | "solution": false, 42 | "task": false 43 | } 44 | }, 45 | "source": [ 46 | "# __Assignment 3: Object oriented programming__" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Imports" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": null, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "from typing import Any, Dict, List" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "## 1. Basic class definition" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": { 75 | "deletable": false, 76 | "editable": false, 77 | "nbgrader": { 78 | "cell_type": "markdown", 79 | "checksum": "1cd6e12a6c722606fc64dec22c013a02", 80 | "grade": false, 81 | "grade_id": "cell-63fe714ed0f893bc", 82 | "locked": true, 83 | "schema_version": 3, 84 | "solution": false, 85 | "task": false 86 | } 87 | }, 88 | "source": [ 89 | "### 1.1 Define a class named `A` with an `__init__` function that takes a single parameter and stores it in an attribute named `value`. Add a `print_value` method to the class, that prints out the value set by the `__init__` function.\n", 90 | "\n", 91 | "Instantiate the class and call the `print_value` method. Please use type hints, and the value parameter can be Any type." 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "deletable": false, 99 | "nbgrader": { 100 | "cell_type": "code", 101 | "checksum": "1884c0bb1ad2ddf35b244aea4610b67f", 102 | "grade": false, 103 | "grade_id": "cell-6c7ba38d59f0b533", 104 | "locked": false, 105 | "schema_version": 3, 106 | "solution": true, 107 | "task": false 108 | } 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "# YOUR CODE HERE\n", 113 | "raise NotImplementedError()" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": null, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "a = A(\"abc\")\n", 123 | "a.print_value()" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": { 129 | "deletable": false, 130 | "editable": false, 131 | "nbgrader": { 132 | "cell_type": "markdown", 133 | "checksum": "b6d1bc549ae892236292b879b1d9aaca", 134 | "grade": false, 135 | "grade_id": "cell-455dea62bb20bf6e", 136 | "locked": true, 137 | "schema_version": 3, 138 | "solution": false, 139 | "task": false 140 | } 141 | }, 142 | "source": [ 143 | "### 1.2 Redefine the class's `__init__` so that it can be instantiated without a parameter. If it is called without a parameter, value should be 42." 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": { 150 | "deletable": false, 151 | "nbgrader": { 152 | "cell_type": "code", 153 | "checksum": "57b581ed21b424578424efe6eb8e99b5", 154 | "grade": false, 155 | "grade_id": "cell-1fa4f4a04cc3b613", 156 | "locked": false, 157 | "schema_version": 3, 158 | "solution": true, 159 | "task": false 160 | } 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "# YOUR CODE HERE\n", 165 | "raise NotImplementedError()" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "a = A(\"abc\")\n", 175 | "a.print_value() # prints abc\n", 176 | "a = A()\n", 177 | "a.print_value() # prints 42" 178 | ] 179 | }, 180 | { 181 | "cell_type": "markdown", 182 | "metadata": {}, 183 | "source": [ 184 | "### 1.3 Define a class named `B`, whose `__init__` takes two parameters and stores one in a public attribute and the other in a private attribute named `this_is_public` and `__this_is_private` respectively.\n", 185 | "\n", 186 | "Check the class's __dict__ attribute and find out the mangled name of the private attribute. Don't forget the type hints!" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "metadata": { 193 | "deletable": false, 194 | "nbgrader": { 195 | "cell_type": "code", 196 | "checksum": "a3eb8a29eecdf1d8e45e2d729e081f49", 197 | "grade": false, 198 | "grade_id": "cell-f4c8b3c52d081013", 199 | "locked": false, 200 | "schema_version": 3, 201 | "solution": true, 202 | "task": false 203 | } 204 | }, 205 | "outputs": [], 206 | "source": [ 207 | "# YOUR CODE HERE\n", 208 | "raise NotImplementedError()" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": null, 214 | "metadata": { 215 | "deletable": false, 216 | "editable": false, 217 | "nbgrader": { 218 | "cell_type": "code", 219 | "checksum": "b6cb190b2a85627def67c637d8f7d429", 220 | "grade": true, 221 | "grade_id": "cell-6480bb6183af4ff0", 222 | "locked": true, 223 | "points": 0, 224 | "schema_version": 3, 225 | "solution": false, 226 | "task": false 227 | } 228 | }, 229 | "outputs": [], 230 | "source": [ 231 | "b = B(1, 2)\n", 232 | "assert b.this_is_public == 1\n", 233 | "try:\n", 234 | " b.__this_is_private\n", 235 | " print(\"This should not happen.\")\n", 236 | "except AttributeError:\n", 237 | " print(\"Failed to access private attribute, this is good :)\")" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": { 243 | "deletable": false, 244 | "editable": false, 245 | "nbgrader": { 246 | "cell_type": "markdown", 247 | "checksum": "be86b5932a6b250e44bdebf483714951", 248 | "grade": false, 249 | "grade_id": "cell-9422317b93395ca9", 250 | "locked": true, 251 | "schema_version": 3, 252 | "solution": false, 253 | "task": false 254 | } 255 | }, 256 | "source": [ 257 | "## 2. Inheritance" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "### 2.1 Guess the output without running the cell." 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": {}, 271 | "outputs": [], 272 | "source": [ 273 | "class A(object): pass\n", 274 | "class B(A): pass\n", 275 | "class C(A): pass\n", 276 | "class D(B): pass\n", 277 | "\n", 278 | "a = A()\n", 279 | "b = B()\n", 280 | "c = C()\n", 281 | "d = D()\n", 282 | "\n", 283 | "print(isinstance(a, object))\n", 284 | "print(isinstance(b, object))\n", 285 | "print(isinstance(a, B))\n", 286 | "print(isinstance(b, A))\n", 287 | "print(isinstance(d, A))" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": null, 293 | "metadata": {}, 294 | "outputs": [], 295 | "source": [ 296 | "print(issubclass(C, object))\n", 297 | "print(issubclass(D, B))\n", 298 | "print(issubclass(B, D))\n", 299 | "print(issubclass(B, B))" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": { 305 | "deletable": false, 306 | "editable": false, 307 | "nbgrader": { 308 | "cell_type": "markdown", 309 | "checksum": "25897ef1b34e09ec7ace0fbd10cd6376", 310 | "grade": false, 311 | "grade_id": "cell-19b4c9b97bf2c344", 312 | "locked": true, 313 | "schema_version": 3, 314 | "solution": false, 315 | "task": false 316 | } 317 | }, 318 | "source": [ 319 | "### 2.2 Create a Cat, a Dog, a Fish and a Eagle class.\n", 320 | "\n", 321 | "The animals have the following attributes:\n", 322 | "\n", 323 | "1. cats, dogs and eagles can make a sound (this should be a make_sound function that prints the animals sound),\n", 324 | "2. all animals have an age and a number_of_legs attribute,\n", 325 | "3. cats and dogs have a fur_color attribute. They can be instantiated with a single color or a list or tuple of colors.\n", 326 | "\n", 327 | "Use inheritance and avoid repeating code. Use default values in the constructors." 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": { 334 | "deletable": false, 335 | "nbgrader": { 336 | "cell_type": "code", 337 | "checksum": "ba492ad176d178ebcd77f30de3505ef7", 338 | "grade": false, 339 | "grade_id": "cell-45b3470b12151035", 340 | "locked": false, 341 | "schema_version": 3, 342 | "solution": true, 343 | "task": false 344 | } 345 | }, 346 | "outputs": [], 347 | "source": [ 348 | "# YOUR CODE HERE\n", 349 | "raise NotImplementedError()" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": { 356 | "deletable": false, 357 | "editable": false, 358 | "nbgrader": { 359 | "cell_type": "code", 360 | "checksum": "9b91e9e029b5c9f792119c07e18b3ab6", 361 | "grade": true, 362 | "grade_id": "cell-b584e9c19590038b", 363 | "locked": true, 364 | "points": 0, 365 | "schema_version": 3, 366 | "solution": false, 367 | "task": false 368 | } 369 | }, 370 | "outputs": [], 371 | "source": [ 372 | "try:\n", 373 | " cat = Cat(\"Fluffy\", age=3, fur=\"white\")\n", 374 | " dog = Dog(\"Cherry\", age=1, fur=(\"white\", \"brown\", \"black\"))\n", 375 | " fish = Fish(\"King\")\n", 376 | " eagle = Eagle(\"Bruce\", age=2)\n", 377 | " animals = [cat, dog, fish, eagle]\n", 378 | "except Exception as e:\n", 379 | " print(f'Could not initialize an animal: {e}')" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "Iterate over the list animals and call `make_sound` for each animal. Print either the sound the animal makes or \"XY does not make a sound\" if the animal does not make a sound (fish). This is an example of duck typing." 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [ 395 | "for animal in animals:\n", 396 | " animal.make_sound()" 397 | ] 398 | }, 399 | { 400 | "cell_type": "markdown", 401 | "metadata": {}, 402 | "source": [ 403 | "## ================ GRADED PART ================" 404 | ] 405 | }, 406 | { 407 | "cell_type": "markdown", 408 | "metadata": {}, 409 | "source": [ 410 | "## 3. Graded exercises" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": {}, 416 | "source": [ 417 | "### 3.1 Cookbook [sum 15 points]" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": { 423 | "deletable": false, 424 | "editable": false, 425 | "nbgrader": { 426 | "cell_type": "markdown", 427 | "checksum": "4f70fce8d9081ad51f8d7c5718a56a14", 428 | "grade": false, 429 | "grade_id": "cell-99d15f432edde7de", 430 | "locked": true, 431 | "schema_version": 3, 432 | "solution": false, 433 | "task": false 434 | } 435 | }, 436 | "source": [ 437 | "#### 3.1.1 Recipe class [5 points]\n", 438 | "Create a new class called Recipe. A recipe should have a name (string), a dictionary of ingredients (name of the ingredient and the amount of it) and a list of steps (string instructions). Define a `list_ingredients` function, that returns a string of ingredients, with one line per ingredient and the ingredient and the amount are tabulator separated format. Define a `list_steps` function that returns a string of the steps as a numbered list.\n", 439 | "\n", 440 | "Additionally make it so that the `len()` function called on a Recipe will return the number of ingredients." 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "metadata": { 447 | "deletable": false, 448 | "nbgrader": { 449 | "cell_type": "code", 450 | "checksum": "623a2f09b01c585afddca543607b36c4", 451 | "grade": false, 452 | "grade_id": "cell-f51366c897f51556", 453 | "locked": false, 454 | "schema_version": 3, 455 | "solution": true, 456 | "task": false 457 | } 458 | }, 459 | "outputs": [], 460 | "source": [ 461 | "# YOUR CODE HERE\n", 462 | "raise NotImplementedError()" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": null, 468 | "metadata": { 469 | "deletable": false, 470 | "editable": false, 471 | "nbgrader": { 472 | "cell_type": "code", 473 | "checksum": "82011b75f0f8d02ff2a39c2b66bb4f99", 474 | "grade": false, 475 | "grade_id": "cell-b88122543b789cf2", 476 | "locked": true, 477 | "schema_version": 3, 478 | "solution": false, 479 | "task": false 480 | } 481 | }, 482 | "outputs": [], 483 | "source": [ 484 | "ingredients = {\"flour\": 1, \"egg\": 2}\n", 485 | "steps = [\"Mix the ingredients well\", \"Roll the dough thinly\"]\n", 486 | "pasta = Recipe(\"pasta\", ingredients, steps)" 487 | ] 488 | }, 489 | { 490 | "cell_type": "markdown", 491 | "metadata": {}, 492 | "source": [ 493 | "The output should look like this:\n", 494 | "\n", 495 | "pasta.list_ingredients()\n", 496 | "\n", 497 | "```\n", 498 | "flour 1\n", 499 | "egg 2\n", 500 | "```\n", 501 | "\n", 502 | "pasta.list_steps()\n", 503 | "\n", 504 | "```\n", 505 | "1. Mix the ingredients well\n", 506 | "2. Roll the dough thinly\n", 507 | "```" 508 | ] 509 | }, 510 | { 511 | "cell_type": "code", 512 | "execution_count": null, 513 | "metadata": { 514 | "deletable": false, 515 | "editable": false, 516 | "nbgrader": { 517 | "cell_type": "code", 518 | "checksum": "ae3a16eec922ee67e18978feacd6b393", 519 | "grade": true, 520 | "grade_id": "cell-607ca863bc22b530", 521 | "locked": true, 522 | "points": 5, 523 | "schema_version": 3, 524 | "solution": false, 525 | "task": false 526 | } 527 | }, 528 | "outputs": [], 529 | "source": [ 530 | "assert 'flour\\t1\\negg\\t2' in pasta.list_ingredients()\n", 531 | "assert pasta.list_steps().startswith(\"1.\")\n", 532 | "assert len(pasta) == 2" 533 | ] 534 | }, 535 | { 536 | "cell_type": "markdown", 537 | "metadata": {}, 538 | "source": [ 539 | "#### 3.1.2 ComplexRecipe class [7 points]\n", 540 | "\n", 541 | "Define a new class based on the previous Recipe class, that can also take a list of recipes as an optional parameter (by default it should be None). If the recipes attribute is not None when the `list_ingredients` or the `list_steps` it should start the output string with the the name and ingredients/steps of the recipes in the recipe list and then after a new line, print out it's own ingredients/steps.\n", 542 | "\n", 543 | "Redifine the lenght of the object, so it includes the lengths of the included recipes as well." 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": null, 549 | "metadata": { 550 | "deletable": false, 551 | "nbgrader": { 552 | "cell_type": "code", 553 | "checksum": "5e0cde48780d373b29585caaa8ace540", 554 | "grade": false, 555 | "grade_id": "cell-71160792c789ff85", 556 | "locked": false, 557 | "schema_version": 3, 558 | "solution": true, 559 | "task": false 560 | } 561 | }, 562 | "outputs": [], 563 | "source": [ 564 | "# YOUR CODE HERE\n", 565 | "raise NotImplementedError()" 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": null, 571 | "metadata": { 572 | "deletable": false, 573 | "editable": false, 574 | "nbgrader": { 575 | "cell_type": "code", 576 | "checksum": "191f389350c10c21f8f600b0de63e59d", 577 | "grade": false, 578 | "grade_id": "cell-9173dba4be22ef96", 579 | "locked": true, 580 | "schema_version": 3, 581 | "solution": false, 582 | "task": false 583 | } 584 | }, 585 | "outputs": [], 586 | "source": [ 587 | "ragout_ingredients = {\"meat\": 1, \"celery\": 2, \"carot\": 3, \"tomato paste\": 1, \"spices\": 2}\n", 588 | "ragout_steps = [\"Cook the vegetables with spices\", \"Add the meat and let it brown\", \"Add tomato paste and cook.\"]\n", 589 | "ragout = Recipe(\"ragout\", ragout_ingredients, ragout_steps)\n", 590 | "besamel_ingredients = {\"flour\": 3, \"butter\": 3, \"milk\": 5, \"nutmeg\": 1}\n", 591 | "besamel_steps = [\"Melt the butter\", \"Add flour and nutmeg\", \"Slowly stir in the milk until incorporated\"]\n", 592 | "besamel = Recipe(\"besamel\", besamel_ingredients, besamel_steps)\n", 593 | "\n", 594 | "lasagne_ingredients = {\"parmesan\": 3}\n", 595 | "lasagne_steps = [\"Layer ragout, besamel, and cooked pasta\", \"Top it off with parmesan\", \"Cook in the oven\"]\n", 596 | "lasagne = ComplexRecipe(\"lasagne\", lasagne_ingredients, lasagne_steps, recipes=[pasta, ragout, besamel])" 597 | ] 598 | }, 599 | { 600 | "cell_type": "markdown", 601 | "metadata": {}, 602 | "source": [ 603 | "The output should look like this:\n", 604 | "\n", 605 | "lasagne.list_ingredients()\n", 606 | "\n", 607 | "```\n", 608 | "pasta\n", 609 | "flour\t1\n", 610 | "egg\t2\n", 611 | "ragout\n", 612 | "meat\t1\n", 613 | "celery\t2\n", 614 | "carot\t3\n", 615 | "tomato paste\t1\n", 616 | "spices\t2\n", 617 | "besamel\n", 618 | "flour\t3\n", 619 | "butter\t3\n", 620 | "milk\t5\n", 621 | "nutmeg\t1\n", 622 | "\n", 623 | "parmesan\t3\n", 624 | "```\n", 625 | "\n", 626 | "lasagne.list_steps()\n", 627 | "\n", 628 | "```\n", 629 | "pasta\n", 630 | "1. Mix the ingredients well\n", 631 | "2. Roll the dough thinly\n", 632 | "ragout\n", 633 | "1. Cook the vegetables with spices\n", 634 | "2. Add the meat and let it brown\n", 635 | "3. Add tomato paste and cook.\n", 636 | "besamel\n", 637 | "1. Melt the butter\n", 638 | "2. Add flour and nutmeg\n", 639 | "3. Slowly stir in the milk until incorporated\n", 640 | "\n", 641 | "1. Layer ragout, besamel, and cooked pasta\n", 642 | "2. Top it off with parmesan\n", 643 | "3. Cook in the oven\n", 644 | "```" 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": null, 650 | "metadata": { 651 | "deletable": false, 652 | "editable": false, 653 | "nbgrader": { 654 | "cell_type": "code", 655 | "checksum": "d1aa43dd4849dbfa23341ee3cf90fff8", 656 | "grade": true, 657 | "grade_id": "cell-2ef9ed4bc1003b26", 658 | "locked": true, 659 | "points": 7, 660 | "schema_version": 3, 661 | "solution": false, 662 | "task": false 663 | } 664 | }, 665 | "outputs": [], 666 | "source": [ 667 | "assert \"pasta\" in lasagne.list_ingredients()\n", 668 | "assert lasagne.list_steps().count(\"1\") == 4\n", 669 | "assert len(lasagne) == 12" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": {}, 675 | "source": [ 676 | "#### 3.1.3 Cookbook class [3 points]\n", 677 | "\n", 678 | "The Cookbook class should have an author, a title and a list of recipes. Make it so that the whole book can be printed in a nice format by overriding the `__str__` function. The outpus should contain the name of the author, the title of the book, and the recipes using the previously defined functions, or adding a `__str__` to those classes as well." 679 | ] 680 | }, 681 | { 682 | "cell_type": "code", 683 | "execution_count": null, 684 | "metadata": { 685 | "deletable": false, 686 | "nbgrader": { 687 | "cell_type": "code", 688 | "checksum": "dce330dea6c9c9c6a5e5673c26daebf4", 689 | "grade": false, 690 | "grade_id": "cell-5e027ff98851d395", 691 | "locked": false, 692 | "schema_version": 3, 693 | "solution": true, 694 | "task": false 695 | } 696 | }, 697 | "outputs": [], 698 | "source": [ 699 | "# YOUR CODE HERE\n", 700 | "raise NotImplementedError()" 701 | ] 702 | }, 703 | { 704 | "cell_type": "code", 705 | "execution_count": null, 706 | "metadata": { 707 | "deletable": false, 708 | "editable": false, 709 | "nbgrader": { 710 | "cell_type": "code", 711 | "checksum": "7b0682050cd1173a4e0ebc58d605ff51", 712 | "grade": false, 713 | "grade_id": "cell-de6fc17ab0704db7", 714 | "locked": true, 715 | "schema_version": 3, 716 | "solution": false, 717 | "task": false 718 | } 719 | }, 720 | "outputs": [], 721 | "source": [ 722 | "cookbook = Cookbook(\"John Doe\", \"Big Lasagne Book\", [pasta, lasagne])" 723 | ] 724 | }, 725 | { 726 | "cell_type": "markdown", 727 | "metadata": {}, 728 | "source": [ 729 | "The output can be something like this:\n", 730 | "```\n", 731 | "John Doe\n", 732 | "Big Lasagne Book\n", 733 | "pasta\n", 734 | "flour\t1\n", 735 | "egg\t2\n", 736 | "\n", 737 | "1. Mix the ingredients well\n", 738 | "2. Roll the dough thinly\n", 739 | "\n", 740 | "\n", 741 | "lasagne\n", 742 | "pasta\n", 743 | "flour\t1\n", 744 | "egg\t2\n", 745 | "ragout\n", 746 | "meat\t1\n", 747 | "celery\t2\n", 748 | "carot\t3\n", 749 | "tomato paste\t1\n", 750 | "spices\t2\n", 751 | "besamel\n", 752 | "flour\t3\n", 753 | "butter\t3\n", 754 | "milk\t5\n", 755 | "nutmeg\t1\n", 756 | "\n", 757 | "parmesan\t3\n", 758 | "\n", 759 | "pasta\n", 760 | "1. Mix the ingredients well\n", 761 | "2. Roll the dough thinly\n", 762 | "ragout\n", 763 | "1. Cook the vegetables with spices\n", 764 | "2. Add the meat and let it brown\n", 765 | "3. Add tomato paste and cook.\n", 766 | "besamel\n", 767 | "1. Melt the butter\n", 768 | "2. Add flour and nutmeg\n", 769 | "3. Slowly stir in the milk until incorporated\n", 770 | "\n", 771 | "1. Layer ragout, besamel, and cooked pasta\n", 772 | "2. Top it off with parmesan\n", 773 | "3. Cook in the oven\n", 774 | "```" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": null, 780 | "metadata": { 781 | "deletable": false, 782 | "editable": false, 783 | "nbgrader": { 784 | "cell_type": "code", 785 | "checksum": "576991156c728a8cf849661213e7954c", 786 | "grade": true, 787 | "grade_id": "cell-4ead2233848edb80", 788 | "locked": true, 789 | "points": 3, 790 | "schema_version": 3, 791 | "solution": false, 792 | "task": false 793 | } 794 | }, 795 | "outputs": [], 796 | "source": [ 797 | "assert str(cookbook).count(\"pasta\") == 4" 798 | ] 799 | }, 800 | { 801 | "cell_type": "markdown", 802 | "metadata": {}, 803 | "source": [ 804 | "### 3.2 Exceptions [sum 10 points]" 805 | ] 806 | }, 807 | { 808 | "cell_type": "markdown", 809 | "metadata": {}, 810 | "source": [ 811 | "#### 3.2.1 Negative Exception [3 points]\n", 812 | "\n", 813 | "Write a `NegativeException` class that is derived from the Exception! Write a `simple_function` that takes an int argument and raises a NegativeException if the argument was negative and returns the argument otherwise." 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": null, 819 | "metadata": { 820 | "deletable": false, 821 | "nbgrader": { 822 | "cell_type": "code", 823 | "checksum": "f0ea1867c72da679e9b2c8cbf7559327", 824 | "grade": false, 825 | "grade_id": "cell-82844608c165f0ee", 826 | "locked": false, 827 | "schema_version": 3, 828 | "solution": true, 829 | "task": false 830 | } 831 | }, 832 | "outputs": [], 833 | "source": [ 834 | "# YOUR CODE HERE\n", 835 | "raise NotImplementedError()" 836 | ] 837 | }, 838 | { 839 | "cell_type": "code", 840 | "execution_count": null, 841 | "metadata": { 842 | "deletable": false, 843 | "editable": false, 844 | "nbgrader": { 845 | "cell_type": "code", 846 | "checksum": "dd77199f72297608ad97ac159a1d26c5", 847 | "grade": true, 848 | "grade_id": "cell-f1f6b331e2ebf81e", 849 | "locked": true, 850 | "points": 3, 851 | "schema_version": 3, 852 | "solution": false, 853 | "task": false 854 | } 855 | }, 856 | "outputs": [], 857 | "source": [ 858 | "assert simple_function(2) == 2\n", 859 | "\n", 860 | "raised = False\n", 861 | "try:\n", 862 | " simple_function(-2)\n", 863 | "except NegativeException:\n", 864 | " raised = True\n", 865 | "\n", 866 | "assert raised" 867 | ] 868 | }, 869 | { 870 | "cell_type": "markdown", 871 | "metadata": {}, 872 | "source": [ 873 | "#### 3.2.2 Cascading exceptions [4 points]\n", 874 | "\n", 875 | "Write a `cascade_function` that takes one argument with cascading try-except block that calls the `simple_function` and raises a `ValueError` if a `NegativeException` occured." 876 | ] 877 | }, 878 | { 879 | "cell_type": "code", 880 | "execution_count": null, 881 | "metadata": { 882 | "deletable": false, 883 | "nbgrader": { 884 | "cell_type": "code", 885 | "checksum": "719b24da5e333aecb03e7856c1b29770", 886 | "grade": false, 887 | "grade_id": "cell-8a7b97c572177d2a", 888 | "locked": false, 889 | "schema_version": 3, 890 | "solution": true, 891 | "task": false 892 | } 893 | }, 894 | "outputs": [], 895 | "source": [ 896 | "# YOUR CODE HERE\n", 897 | "raise NotImplementedError()" 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": null, 903 | "metadata": { 904 | "deletable": false, 905 | "editable": false, 906 | "nbgrader": { 907 | "cell_type": "code", 908 | "checksum": "641df4a2fbd46d9c39b14495771e041d", 909 | "grade": true, 910 | "grade_id": "cell-809ca2a34720dfa8", 911 | "locked": true, 912 | "points": 4, 913 | "schema_version": 3, 914 | "solution": false, 915 | "task": false 916 | } 917 | }, 918 | "outputs": [], 919 | "source": [ 920 | "negative_raised = False\n", 921 | "value_raised = False\n", 922 | "try:\n", 923 | " cascade_function(-2)\n", 924 | "except NegativeException:\n", 925 | " negative_raised = True\n", 926 | "except ValueError:\n", 927 | " value_raised = True\n", 928 | "\n", 929 | "assert not negative_raised\n", 930 | "assert value_raised" 931 | ] 932 | }, 933 | { 934 | "cell_type": "markdown", 935 | "metadata": {}, 936 | "source": [ 937 | "#### 3.2.3 Finally [3 points]\n", 938 | "\n", 939 | "Write a `finally_function` that takes an argument, calls the `cascade_function`, catches any error and as a clean-up action it returns True." 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "execution_count": null, 945 | "metadata": { 946 | "deletable": false, 947 | "nbgrader": { 948 | "cell_type": "code", 949 | "checksum": "a59404510a7a0554a18e92f74a4e6eda", 950 | "grade": false, 951 | "grade_id": "cell-fe8bae55d49a6ec0", 952 | "locked": false, 953 | "schema_version": 3, 954 | "solution": true, 955 | "task": false 956 | } 957 | }, 958 | "outputs": [], 959 | "source": [ 960 | "# YOUR CODE HERE\n", 961 | "raise NotImplementedError()" 962 | ] 963 | }, 964 | { 965 | "cell_type": "code", 966 | "execution_count": null, 967 | "metadata": { 968 | "deletable": false, 969 | "editable": false, 970 | "nbgrader": { 971 | "cell_type": "code", 972 | "checksum": "dd4266ebfb6dc41d29ea43ac7dc0f0bd", 973 | "grade": true, 974 | "grade_id": "cell-bb0801a5c48f99ac", 975 | "locked": true, 976 | "points": 3, 977 | "schema_version": 3, 978 | "solution": false, 979 | "task": false 980 | } 981 | }, 982 | "outputs": [], 983 | "source": [ 984 | "assert finally_function(-5)" 985 | ] 986 | } 987 | ], 988 | "metadata": { 989 | "kernelspec": { 990 | "display_name": "Python 3 (ipykernel)", 991 | "language": "python", 992 | "name": "python3" 993 | }, 994 | "language_info": { 995 | "codemirror_mode": { 996 | "name": "ipython", 997 | "version": 3 998 | }, 999 | "file_extension": ".py", 1000 | "mimetype": "text/x-python", 1001 | "name": "python", 1002 | "nbconvert_exporter": "python", 1003 | "pygments_lexer": "ipython3", 1004 | "version": "3.10.4" 1005 | }, 1006 | "toc": { 1007 | "base_numbering": 1, 1008 | "nav_menu": {}, 1009 | "number_sections": true, 1010 | "sideBar": true, 1011 | "skip_h1_title": false, 1012 | "title_cell": "Table of Contents", 1013 | "title_sidebar": "Contents", 1014 | "toc_cell": false, 1015 | "toc_position": {}, 1016 | "toc_section_display": true, 1017 | "toc_window_display": true 1018 | }, 1019 | "vscode": { 1020 | "interpreter": { 1021 | "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" 1022 | } 1023 | } 1024 | }, 1025 | "nbformat": 4, 1026 | "nbformat_minor": 2 1027 | } 1028 | -------------------------------------------------------------------------------- /assignments/4_List_comprehension_functional_lab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", 8 | "\n", 9 | "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "NAME = \"\"\n", 19 | "COLLABORATORS = \"\"" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "---" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "deletable": false, 33 | "editable": false, 34 | "nbgrader": { 35 | "cell_type": "markdown", 36 | "checksum": "07e55412ee568c47bfdb8b8da11e1902", 37 | "grade": false, 38 | "grade_id": "cell-ac62b5bf39c1a456", 39 | "locked": true, 40 | "schema_version": 3, 41 | "solution": false, 42 | "task": false 43 | } 44 | }, 45 | "source": [ 46 | "# Assignment 4: List comprechension, generators, functional python" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Imports" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 1, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [ 62 | "from contextlib import redirect_stdout\n", 63 | "from typing import Iterable, Any, List\n", 64 | "from types import GeneratorType\n", 65 | "from functools import reduce\n", 66 | "import io\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "markdown", 71 | "metadata": {}, 72 | "source": [ 73 | "## 1. Comprehensions\n", 74 | "\n", 75 | "Convert the following for loops into comprehensions:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 3, 81 | "metadata": { 82 | "deletable": false, 83 | "editable": false, 84 | "nbgrader": { 85 | "cell_type": "code", 86 | "checksum": "309b8419abad4bcba0c530b4ab7cf8b2", 87 | "grade": false, 88 | "grade_id": "cell-2cd05e0199137bf5", 89 | "locked": true, 90 | "schema_version": 3, 91 | "solution": false, 92 | "task": false 93 | } 94 | }, 95 | "outputs": [ 96 | { 97 | "data": { 98 | "text/plain": [ 99 | "[-7, -5, -3, -1, 1, 3, 5, 7]" 100 | ] 101 | }, 102 | "execution_count": 3, 103 | "metadata": {}, 104 | "output_type": "execute_result" 105 | } 106 | ], 107 | "source": [ 108 | "l = []\n", 109 | "for i in range(-5, 10, 2):\n", 110 | " l.append(i-2)\n", 111 | "l" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 4, 117 | "metadata": { 118 | "deletable": false, 119 | "nbgrader": { 120 | "cell_type": "code", 121 | "checksum": "2a29f5d13d3655cbfe7e000bb981bb7d", 122 | "grade": false, 123 | "grade_id": "cell-fe81859f4057f7ff", 124 | "locked": false, 125 | "schema_version": 3, 126 | "solution": true, 127 | "task": false 128 | } 129 | }, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "[-7, -5, -3, -1, 1, 3, 5, 7]" 135 | ] 136 | }, 137 | "execution_count": 4, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "l2 = [i-2 for i in range(-5, 10, 2)]\n", 144 | "l2" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 5, 150 | "metadata": { 151 | "deletable": false, 152 | "editable": false, 153 | "nbgrader": { 154 | "cell_type": "code", 155 | "checksum": "3d37a767a785979758a5765e328f36c1", 156 | "grade": false, 157 | "grade_id": "cell-a64f6d63d3a081ca", 158 | "locked": true, 159 | "schema_version": 3, 160 | "solution": false, 161 | "task": false 162 | } 163 | }, 164 | "outputs": [ 165 | { 166 | "data": { 167 | "text/plain": [ 168 | "[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]" 169 | ] 170 | }, 171 | "execution_count": 5, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "l = []\n", 178 | "for i in range(100):\n", 179 | " if i % 10 == 4:\n", 180 | " l.append(i)\n", 181 | "l" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 7, 187 | "metadata": { 188 | "deletable": false, 189 | "nbgrader": { 190 | "cell_type": "code", 191 | "checksum": "b262a886e5fd6fd78ae9d59a50908b87", 192 | "grade": false, 193 | "grade_id": "cell-adadaf2661704ec1", 194 | "locked": false, 195 | "schema_version": 3, 196 | "solution": true, 197 | "task": false 198 | } 199 | }, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "[4, 14, 24, 34, 44, 54, 64, 74, 84, 94]" 205 | ] 206 | }, 207 | "execution_count": 7, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "l2 = [i for i in range(100) if i % 10 == 4]\n", 214 | "l2" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "metadata": { 221 | "deletable": false, 222 | "editable": false, 223 | "nbgrader": { 224 | "cell_type": "code", 225 | "checksum": "e4e7ef1ec6796122ce25455b3612d607", 226 | "grade": false, 227 | "grade_id": "cell-e45a3fb2f858a92b", 228 | "locked": true, 229 | "schema_version": 3, 230 | "solution": false, 231 | "task": false 232 | } 233 | }, 234 | "outputs": [], 235 | "source": [ 236 | "l1 = [12, 1, 0, 13, -3, -4, 0, 2]\n", 237 | "l2 = []\n", 238 | "\n", 239 | "for e in l1:\n", 240 | " if e % 2 == 1:\n", 241 | " l2.append(e)\n", 242 | "l2" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": { 249 | "deletable": false, 250 | "nbgrader": { 251 | "cell_type": "code", 252 | "checksum": "423b1c5670aa98b341678ade21bf710a", 253 | "grade": false, 254 | "grade_id": "cell-01c992d67ca14968", 255 | "locked": false, 256 | "schema_version": 3, 257 | "solution": true, 258 | "task": false 259 | } 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "# YOUR CODE HERE\n", 264 | "raise NotImplementedError()" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": { 271 | "deletable": false, 272 | "editable": false, 273 | "nbgrader": { 274 | "cell_type": "code", 275 | "checksum": "dd3a1cc84f3fe6ca88f743b69e2866fd", 276 | "grade": false, 277 | "grade_id": "cell-e68e24222ccae6ac", 278 | "locked": true, 279 | "schema_version": 3, 280 | "solution": false, 281 | "task": false 282 | } 283 | }, 284 | "outputs": [], 285 | "source": [ 286 | "l1 = [12, 1, 0, 13, -3, -4, 0, 2]\n", 287 | "l2 = []\n", 288 | "\n", 289 | "for e in l1:\n", 290 | " if e % 2 == 1:\n", 291 | " l2.append(True)\n", 292 | " else:\n", 293 | " l2.append(False)\n", 294 | "l2" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": null, 300 | "metadata": { 301 | "deletable": false, 302 | "nbgrader": { 303 | "cell_type": "code", 304 | "checksum": "27fbbc1ad64310a665a101d6ae931234", 305 | "grade": false, 306 | "grade_id": "cell-106d47d0b13dd3b1", 307 | "locked": false, 308 | "schema_version": 3, 309 | "solution": true, 310 | "task": false 311 | } 312 | }, 313 | "outputs": [], 314 | "source": [ 315 | "# YOUR CODE HERE\n", 316 | "raise NotImplementedError()" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": 8, 322 | "metadata": { 323 | "deletable": false, 324 | "editable": false, 325 | "nbgrader": { 326 | "cell_type": "code", 327 | "checksum": "1ae9f95ba52626cf56fae7ca00b7dd0f", 328 | "grade": false, 329 | "grade_id": "cell-32329cc757eecb28", 330 | "locked": true, 331 | "schema_version": 3, 332 | "solution": false, 333 | "task": false 334 | } 335 | }, 336 | "outputs": [ 337 | { 338 | "data": { 339 | "text/plain": [ 340 | "[6, 12, 18, 10, 20, 30, 14, 28, 42, 22, 44, 66]" 341 | ] 342 | }, 343 | "execution_count": 8, 344 | "metadata": {}, 345 | "output_type": "execute_result" 346 | } 347 | ], 348 | "source": [ 349 | "l1 = [3, 5, 7, 11]\n", 350 | "l2 = [2, 4, 6]\n", 351 | "\n", 352 | "products = []\n", 353 | "\n", 354 | "for x in l1:\n", 355 | " for y in l2:\n", 356 | " products.append(x*y)\n", 357 | "products" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 9, 363 | "metadata": { 364 | "deletable": false, 365 | "nbgrader": { 366 | "cell_type": "code", 367 | "checksum": "ecf9e3c0d326b868a2348b82c0a21f61", 368 | "grade": false, 369 | "grade_id": "cell-4ead65faba295143", 370 | "locked": false, 371 | "schema_version": 3, 372 | "solution": true, 373 | "task": false 374 | } 375 | }, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/plain": [ 380 | "[6, 12, 18, 10, 20, 30, 14, 28, 42, 22, 44, 66]" 381 | ] 382 | }, 383 | "execution_count": 9, 384 | "metadata": {}, 385 | "output_type": "execute_result" 386 | } 387 | ], 388 | "source": [ 389 | "prod2 = [x * y for x in l1 for y in l2]\n", 390 | "prod2" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": null, 396 | "metadata": { 397 | "deletable": false, 398 | "editable": false, 399 | "nbgrader": { 400 | "cell_type": "code", 401 | "checksum": "485eb324b71a26e037a440fcdf34a7bf", 402 | "grade": false, 403 | "grade_id": "cell-3c0f2b750c329898", 404 | "locked": true, 405 | "schema_version": 3, 406 | "solution": false, 407 | "task": false 408 | } 409 | }, 410 | "outputs": [], 411 | "source": [ 412 | "l1 = [3, 5, 7, 11, 13, 17, 19]\n", 413 | "l2 = [2, 4, 6, 8, 10]\n", 414 | "\n", 415 | "products = []\n", 416 | "\n", 417 | "for x in l1:\n", 418 | " for y in l2:\n", 419 | " if (x + y) % 3 == 0:\n", 420 | " products.append(x*y)\n", 421 | "products" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": { 428 | "deletable": false, 429 | "nbgrader": { 430 | "cell_type": "code", 431 | "checksum": "8b874ca1957da0fe205c9520e6cd69fc", 432 | "grade": false, 433 | "grade_id": "cell-c8caea611b1907b3", 434 | "locked": false, 435 | "schema_version": 3, 436 | "solution": true, 437 | "task": false 438 | } 439 | }, 440 | "outputs": [], 441 | "source": [ 442 | "# YOUR CODE HERE\n", 443 | "raise NotImplementedError()" 444 | ] 445 | }, 446 | { 447 | "cell_type": "code", 448 | "execution_count": 18, 449 | "metadata": { 450 | "deletable": false, 451 | "editable": false, 452 | "nbgrader": { 453 | "cell_type": "code", 454 | "checksum": "6bca438ef3387389b07dfd435f4f46bb", 455 | "grade": false, 456 | "grade_id": "cell-afcf74e4c27c981b", 457 | "locked": true, 458 | "schema_version": 3, 459 | "solution": false, 460 | "task": false 461 | } 462 | }, 463 | "outputs": [ 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "[['a', 'pp', 'ppp', 'llll', 'eeeee'],\n", 468 | " ['p', 'll', 'uuu', 'mmmm'],\n", 469 | " ['p', 'ee', 'aaa', 'rrrr'],\n", 470 | " ['a', 'vv', 'ooo', 'cccc', 'aaaaa', 'dddddd', 'ooooooo']]" 471 | ] 472 | }, 473 | "execution_count": 18, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "fruits = [\"apple\", \"plum\", \"pear\", \"avocado\"]\n", 480 | "\n", 481 | "mtx = []\n", 482 | "for fruit in fruits:\n", 483 | " row = []\n", 484 | " for i, c in enumerate(fruit):\n", 485 | " row.append(c*(i+1))\n", 486 | " mtx.append(row)\n", 487 | " \n", 488 | "mtx" 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "execution_count": null, 494 | "metadata": { 495 | "deletable": false, 496 | "nbgrader": { 497 | "cell_type": "code", 498 | "checksum": "a9911105e51f5a421b70b25499f22a8a", 499 | "grade": false, 500 | "grade_id": "cell-b2e0df6824c6b028", 501 | "locked": false, 502 | "schema_version": 3, 503 | "solution": true, 504 | "task": false 505 | } 506 | }, 507 | "outputs": [], 508 | "source": [ 509 | "# YOUR CODE HERE\n", 510 | "raise NotImplementedError()" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 14, 516 | "metadata": { 517 | "deletable": false, 518 | "editable": false, 519 | "nbgrader": { 520 | "cell_type": "code", 521 | "checksum": "14ee78368f7cadebe0f0d30a09673e48", 522 | "grade": false, 523 | "grade_id": "cell-2e6122253a01c5b4", 524 | "locked": true, 525 | "schema_version": 3, 526 | "solution": false, 527 | "task": false 528 | } 529 | }, 530 | "outputs": [ 531 | { 532 | "data": { 533 | "text/plain": [ 534 | "{'a': 5, 'b': 3, 'c': 1, 'd': 2, 's': 1}" 535 | ] 536 | }, 537 | "execution_count": 14, 538 | "metadata": {}, 539 | "output_type": "execute_result" 540 | } 541 | ], 542 | "source": [ 543 | "text = \"ababaacdsadb\"\n", 544 | "\n", 545 | "char_freqs = {}\n", 546 | "\n", 547 | "for c in text:\n", 548 | " try:\n", 549 | " char_freqs[c] += 1\n", 550 | " except KeyError:\n", 551 | " char_freqs[c] = 1\n", 552 | " \n", 553 | "char_freqs" 554 | ] 555 | }, 556 | { 557 | "cell_type": "code", 558 | "execution_count": 16, 559 | "metadata": {}, 560 | "outputs": [ 561 | { 562 | "data": { 563 | "text/plain": [ 564 | "{'a': 5, 'b': 3, 'c': 1, 'd': 2, 's': 1}" 565 | ] 566 | }, 567 | "execution_count": 16, 568 | "metadata": {}, 569 | "output_type": "execute_result" 570 | } 571 | ], 572 | "source": [ 573 | "char_freqs2 = {}\n", 574 | "\n", 575 | "for c in text:\n", 576 | " if c not in char_freqs2:\n", 577 | " char_freqs2[c] = 1\n", 578 | " else:\n", 579 | " char_freqs2[c] += 1\n", 580 | "\n", 581 | "char_freqs2" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 17, 587 | "metadata": { 588 | "deletable": false, 589 | "nbgrader": { 590 | "cell_type": "code", 591 | "checksum": "b828e27f0e98763930863e46e9437c2a", 592 | "grade": false, 593 | "grade_id": "cell-9af4ab90dccea74a", 594 | "locked": false, 595 | "schema_version": 3, 596 | "solution": true, 597 | "task": false 598 | } 599 | }, 600 | "outputs": [ 601 | { 602 | "data": { 603 | "text/plain": [ 604 | "{'a': 5, 'b': 3, 'c': 1, 'd': 2, 's': 1}" 605 | ] 606 | }, 607 | "execution_count": 17, 608 | "metadata": {}, 609 | "output_type": "execute_result" 610 | } 611 | ], 612 | "source": [ 613 | "{c: text.count(c) for c in text}" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": null, 619 | "metadata": { 620 | "deletable": false, 621 | "editable": false, 622 | "nbgrader": { 623 | "cell_type": "code", 624 | "checksum": "07aa8284298c4b1247ab4730844a2aeb", 625 | "grade": false, 626 | "grade_id": "cell-2920bfccfa7bdb1b", 627 | "locked": true, 628 | "schema_version": 3, 629 | "solution": false, 630 | "task": false 631 | } 632 | }, 633 | "outputs": [], 634 | "source": [ 635 | "d1 = {\"a\": 1, \"b\": 3, \"c\": 2}\n", 636 | "d2 = {\"a\": 2, \"b\": 1}\n", 637 | "\n", 638 | "d3 = {}\n", 639 | "\n", 640 | "for key in set(d1.keys()) | set(d2.keys()):\n", 641 | " max_val = max(d1.get(key, 0), d2.get(key, 0))\n", 642 | " d3[key] = max_val\n", 643 | "\n", 644 | "d3" 645 | ] 646 | }, 647 | { 648 | "cell_type": "code", 649 | "execution_count": null, 650 | "metadata": { 651 | "deletable": false, 652 | "nbgrader": { 653 | "cell_type": "code", 654 | "checksum": "aa125e3d9bfd164b31473d3527835100", 655 | "grade": false, 656 | "grade_id": "cell-269ea11b9d01f4dd", 657 | "locked": false, 658 | "schema_version": 3, 659 | "solution": true, 660 | "task": false 661 | } 662 | }, 663 | "outputs": [], 664 | "source": [ 665 | "# YOUR CODE HERE\n", 666 | "raise NotImplementedError()" 667 | ] 668 | }, 669 | { 670 | "cell_type": "markdown", 671 | "metadata": { 672 | "deletable": false, 673 | "editable": false, 674 | "nbgrader": { 675 | "cell_type": "markdown", 676 | "checksum": "802263ccad314d994e4ab761f6614df0", 677 | "grade": false, 678 | "grade_id": "cell-6fa790352dd04a35", 679 | "locked": true, 680 | "schema_version": 3, 681 | "solution": false, 682 | "task": false 683 | } 684 | }, 685 | "source": [ 686 | "## 2. Generators" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "metadata": {}, 692 | "source": [ 693 | "### 2.1 Bigram generator\n", 694 | "Write a generator function that takes a string and generates character bigrams from it:" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": 10, 700 | "metadata": { 701 | "deletable": false, 702 | "nbgrader": { 703 | "cell_type": "code", 704 | "checksum": "95748303d96333fb258bf025e759aa70", 705 | "grade": false, 706 | "grade_id": "cell-e3f683ca2b1a38ff", 707 | "locked": false, 708 | "schema_version": 3, 709 | "solution": true, 710 | "task": false 711 | } 712 | }, 713 | "outputs": [], 714 | "source": [ 715 | "def get_bigrams(text: str) -> str:\n", 716 | " for i, _ in enumerate(text[:-1]):\n", 717 | " yield text[i:i+2]" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 13, 723 | "metadata": {}, 724 | "outputs": [ 725 | { 726 | "name": "stdout", 727 | "output_type": "stream", 728 | "text": [ 729 | "ab\n", 730 | "bc\n", 731 | "ca\n" 732 | ] 733 | } 734 | ], 735 | "source": [ 736 | "gen = get_bigrams(\"abca\")\n", 737 | "\n", 738 | "for g in gen:\n", 739 | " print(g)" 740 | ] 741 | }, 742 | { 743 | "cell_type": "code", 744 | "execution_count": 11, 745 | "metadata": { 746 | "deletable": false, 747 | "editable": false, 748 | "nbgrader": { 749 | "cell_type": "code", 750 | "checksum": "860304a95d7fb0af569fe608f9bec30a", 751 | "grade": true, 752 | "grade_id": "cell-a9c433466ba1aab5", 753 | "locked": true, 754 | "points": 0, 755 | "schema_version": 3, 756 | "solution": false, 757 | "task": false 758 | } 759 | }, 760 | "outputs": [], 761 | "source": [ 762 | "assert list(get_bigrams(\"abca\")) == [\"ab\", \"bc\", \"ca\"]\n", 763 | "assert list(get_bigrams(\"\")) == []\n", 764 | "assert isinstance(get_bigrams(\"a\"), GeneratorType)" 765 | ] 766 | }, 767 | { 768 | "cell_type": "markdown", 769 | "metadata": { 770 | "deletable": false, 771 | "editable": false, 772 | "nbgrader": { 773 | "cell_type": "markdown", 774 | "checksum": "d81e16fb97519bf44e0a909738c4b4b0", 775 | "grade": false, 776 | "grade_id": "cell-0cfd42097b3b2748", 777 | "locked": true, 778 | "schema_version": 3, 779 | "solution": false, 780 | "task": false 781 | } 782 | }, 783 | "source": [ 784 | "### 2.2 Generators with memory\n", 785 | "\n", 786 | "Write a generator function that remembers the previous element and yields it beside the current one." 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": null, 792 | "metadata": { 793 | "deletable": false, 794 | "nbgrader": { 795 | "cell_type": "code", 796 | "checksum": "09f224c4d6efc216da14ccb3cb29d4c7", 797 | "grade": false, 798 | "grade_id": "cell-993c1f55e59af7f8", 799 | "locked": false, 800 | "schema_version": 3, 801 | "solution": true, 802 | "task": false 803 | } 804 | }, 805 | "outputs": [], 806 | "source": [ 807 | "def iter_with_memory(orig_iter):\n", 808 | " # YOUR CODE HERE\n", 809 | " raise NotImplementedError()" 810 | ] 811 | }, 812 | { 813 | "cell_type": "markdown", 814 | "metadata": { 815 | "deletable": false, 816 | "editable": false, 817 | "nbgrader": { 818 | "cell_type": "markdown", 819 | "checksum": "e1930df151163f95dc52375959744f38", 820 | "grade": false, 821 | "grade_id": "cell-4b9176d605305c16", 822 | "locked": true, 823 | "schema_version": 3, 824 | "solution": false, 825 | "task": false 826 | } 827 | }, 828 | "source": [ 829 | "## ================ GRADED PART ================" 830 | ] 831 | }, 832 | { 833 | "cell_type": "markdown", 834 | "metadata": {}, 835 | "source": [ 836 | "## 3. Graded exercises" 837 | ] 838 | }, 839 | { 840 | "cell_type": "markdown", 841 | "metadata": { 842 | "deletable": false, 843 | "editable": false, 844 | "nbgrader": { 845 | "cell_type": "markdown", 846 | "checksum": "c426a0b0890794ae0523a417adfc915a", 847 | "grade": false, 848 | "grade_id": "cell-5be6d18cc269c7d9", 849 | "locked": true, 850 | "schema_version": 3, 851 | "solution": false, 852 | "task": false 853 | } 854 | }, 855 | "source": [ 856 | "### 3.1 Formatted printing with comprehensions [6 points]\n", 857 | "\n", 858 | "Given a list of lists of students, print them in the following format with one print invocation. You can find similar examples in Lecture 11." 859 | ] 860 | }, 861 | { 862 | "cell_type": "code", 863 | "execution_count": null, 864 | "metadata": { 865 | "deletable": false, 866 | "editable": false, 867 | "nbgrader": { 868 | "cell_type": "code", 869 | "checksum": "b79bac520831d76ca9a0de3e7c4dc76e", 870 | "grade": false, 871 | "grade_id": "cell-cad5948bd5934395", 872 | "locked": true, 873 | "schema_version": 3, 874 | "solution": false, 875 | "task": false 876 | } 877 | }, 878 | "outputs": [], 879 | "source": [ 880 | "all_students = [\n", 881 | " [\"Joe\", \"John\", \"Mary\"],\n", 882 | " [\"Tina\", \"Tony\", \"Jeff\", \"Bela\"],\n", 883 | " [\"Pete\", \"Dave\"],\n", 884 | "]" 885 | ] 886 | }, 887 | { 888 | "cell_type": "markdown", 889 | "metadata": { 890 | "deletable": false, 891 | "editable": false, 892 | "nbgrader": { 893 | "cell_type": "markdown", 894 | "checksum": "991d972f766d0f3c76cf56350c56df3d", 895 | "grade": false, 896 | "grade_id": "cell-17e5f552c9ba724e", 897 | "locked": true, 898 | "schema_version": 3, 899 | "solution": false, 900 | "task": false 901 | } 902 | }, 903 | "source": [ 904 | "One class-per-line and print the size of the class too\n", 905 | "\n", 906 | "~~~\n", 907 | "class 1, size: 3, students: Joe, John, Mary\n", 908 | "class 2, size: 4, students: Tina, Tony, Jeff, Bela\n", 909 | "class 3, size: 2, students: Pete, Dave\n", 910 | "~~~" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": null, 916 | "metadata": { 917 | "deletable": false, 918 | "nbgrader": { 919 | "cell_type": "code", 920 | "checksum": "0e66d848305817162e1e395b46f736c8", 921 | "grade": false, 922 | "grade_id": "cell-cc231a351b5a051f", 923 | "locked": false, 924 | "schema_version": 3, 925 | "solution": true, 926 | "task": false 927 | } 928 | }, 929 | "outputs": [], 930 | "source": [ 931 | "def print_students(all_students: List[List[str]]): \n", 932 | " # YOUR CODE HERE\n", 933 | " raise NotImplementedError()" 934 | ] 935 | }, 936 | { 937 | "cell_type": "code", 938 | "execution_count": null, 939 | "metadata": { 940 | "deletable": false, 941 | "editable": false, 942 | "nbgrader": { 943 | "cell_type": "code", 944 | "checksum": "fa5a1f88825f81e03fcfb2fad223c8b2", 945 | "grade": true, 946 | "grade_id": "cell-d540370d23ed6e0a", 947 | "locked": true, 948 | "points": 3, 949 | "schema_version": 3, 950 | "solution": false, 951 | "task": false 952 | } 953 | }, 954 | "outputs": [], 955 | "source": [ 956 | "f = io.StringIO()\n", 957 | "with redirect_stdout(f):\n", 958 | " print_students(all_students)\n", 959 | "s = f.getvalue()\n", 960 | "\n", 961 | "assert s.strip() == \"class 1, size: 3, students: Joe, John, Mary\\nclass 2, size: 4, students: Tina, Tony, Jeff, Bela\\nclass 3, size: 2, students: Pete, Dave\"" 962 | ] 963 | }, 964 | { 965 | "cell_type": "markdown", 966 | "metadata": { 967 | "deletable": false, 968 | "editable": false, 969 | "nbgrader": { 970 | "cell_type": "markdown", 971 | "checksum": "66bdfd74d06e28572d458e9d15fd1ac8", 972 | "grade": false, 973 | "grade_id": "cell-98c259374e84a07a", 974 | "locked": true, 975 | "schema_version": 3, 976 | "solution": false, 977 | "task": false 978 | } 979 | }, 980 | "source": [ 981 | "Sort the classes by size in increasing order\n", 982 | "\n", 983 | "~~~\n", 984 | "class 3, size: 2, students: Pete, Dave\n", 985 | "class 1, size: 3, students: Joe, John, Mary\n", 986 | "class 2, size: 4, students: Tina, Tony, Jeff, Bela\n", 987 | "~~~" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": null, 993 | "metadata": { 994 | "deletable": false, 995 | "nbgrader": { 996 | "cell_type": "code", 997 | "checksum": "884c2510e220dcff2dec26634049d84e", 998 | "grade": false, 999 | "grade_id": "cell-7b6657c1ef2e4231", 1000 | "locked": false, 1001 | "schema_version": 3, 1002 | "solution": true, 1003 | "task": false 1004 | } 1005 | }, 1006 | "outputs": [], 1007 | "source": [ 1008 | "def print_ordered_students(all_students: List[List[str]]):\n", 1009 | " # YOUR CODE HERE\n", 1010 | " raise NotImplementedError()" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": null, 1016 | "metadata": { 1017 | "deletable": false, 1018 | "editable": false, 1019 | "nbgrader": { 1020 | "cell_type": "code", 1021 | "checksum": "401d05eb7b2e0f69a727369f161cdce5", 1022 | "grade": true, 1023 | "grade_id": "cell-cf7b5a337c36691a", 1024 | "locked": true, 1025 | "points": 3, 1026 | "schema_version": 3, 1027 | "solution": false, 1028 | "task": false 1029 | } 1030 | }, 1031 | "outputs": [], 1032 | "source": [ 1033 | "f = io.StringIO()\n", 1034 | "with redirect_stdout(f):\n", 1035 | " print_ordered_students(all_students)\n", 1036 | "s = f.getvalue()\n", 1037 | "\n", 1038 | "assert s.strip() == \"class 3, size: 2, students: Pete, Dave\\nclass 1, size: 3, students: Joe, John, Mary\\nclass 2, size: 4, students: Tina, Tony, Jeff, Bela\"" 1039 | ] 1040 | }, 1041 | { 1042 | "cell_type": "markdown", 1043 | "metadata": {}, 1044 | "source": [ 1045 | "### 3.2 Generators" 1046 | ] 1047 | }, 1048 | { 1049 | "cell_type": "markdown", 1050 | "metadata": { 1051 | "deletable": false, 1052 | "editable": false, 1053 | "nbgrader": { 1054 | "cell_type": "markdown", 1055 | "checksum": "7b18a727a0720a060ca5339e6dd3c326", 1056 | "grade": false, 1057 | "grade_id": "cell-6ba3b1a95ef03d8d", 1058 | "locked": true, 1059 | "schema_version": 3, 1060 | "solution": false, 1061 | "task": false 1062 | } 1063 | }, 1064 | "source": [ 1065 | "#### 3.2.1 N-gram generator [3 points]\n", 1066 | "\n", 1067 | "Change the function in the 2nd exercise so that it can generate n-grams. N should be a parameter of the function." 1068 | ] 1069 | }, 1070 | { 1071 | "cell_type": "code", 1072 | "execution_count": null, 1073 | "metadata": { 1074 | "deletable": false, 1075 | "nbgrader": { 1076 | "cell_type": "code", 1077 | "checksum": "e28555d0db753a090c7bb57199dc796d", 1078 | "grade": false, 1079 | "grade_id": "cell-07f2eb9be62b41bf", 1080 | "locked": false, 1081 | "schema_version": 3, 1082 | "solution": true, 1083 | "task": false 1084 | } 1085 | }, 1086 | "outputs": [], 1087 | "source": [ 1088 | "def get_ngrams(text: str, n: int) -> str\n", 1089 | " # YOUR CODE HERE\n", 1090 | " raise NotImplementedError()" 1091 | ] 1092 | }, 1093 | { 1094 | "cell_type": "code", 1095 | "execution_count": null, 1096 | "metadata": { 1097 | "deletable": false, 1098 | "editable": false, 1099 | "nbgrader": { 1100 | "cell_type": "code", 1101 | "checksum": "f1ec41fb327bc8d0ebfebee7215a6a13", 1102 | "grade": true, 1103 | "grade_id": "cell-4117b15c31f462b3", 1104 | "locked": true, 1105 | "points": 3, 1106 | "schema_version": 3, 1107 | "solution": false, 1108 | "task": false 1109 | } 1110 | }, 1111 | "outputs": [], 1112 | "source": [ 1113 | "assert list(get_ngrams(\"abca\", 2)) == [\"ab\", \"bc\", \"ca\"]\n", 1114 | "assert list(get_ngrams(\"abca\", 3)) == [\"abc\", \"bca\"]\n", 1115 | "assert list(get_ngrams(\"abca\", 3)) == [\"abc\", \"bca\"]\n", 1116 | "assert isinstance(get_ngrams(\"a\", 1), GeneratorType)" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "markdown", 1121 | "metadata": {}, 1122 | "source": [ 1123 | "#### 3.2.2 Generator with memory [6 points]\n", 1124 | "Write a generatior that takes an iterator and a memory size `k` and yields a tuple of k elements from the iterator." 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "execution_count": null, 1130 | "metadata": { 1131 | "deletable": false, 1132 | "nbgrader": { 1133 | "cell_type": "code", 1134 | "checksum": "fb700e6d1b0081fe2fbf497654be7df4", 1135 | "grade": false, 1136 | "grade_id": "cell-213760247532dbf9", 1137 | "locked": false, 1138 | "schema_version": 3, 1139 | "solution": true, 1140 | "task": false 1141 | } 1142 | }, 1143 | "outputs": [], 1144 | "source": [ 1145 | "def iter_with_k_memory(orig_iter: Iterable, k: int) -> Tuple[Any]:\n", 1146 | " # YOUR CODE HERE\n", 1147 | " raise NotImplementedError()" 1148 | ] 1149 | }, 1150 | { 1151 | "cell_type": "code", 1152 | "execution_count": null, 1153 | "metadata": { 1154 | "deletable": false, 1155 | "editable": false, 1156 | "nbgrader": { 1157 | "cell_type": "code", 1158 | "checksum": "86899bc6c0b387581e20f99e87076227", 1159 | "grade": true, 1160 | "grade_id": "cell-10d456c5035c4b76", 1161 | "locked": true, 1162 | "points": 6, 1163 | "schema_version": 3, 1164 | "solution": false, 1165 | "task": false 1166 | } 1167 | }, 1168 | "outputs": [], 1169 | "source": [ 1170 | "l = list(range(3))\n", 1171 | "assert list(iter_with_k_memory(l, 3)) == [(0, None, None), (1, 0, None), (2, 1, 0)]\n", 1172 | "assert list(iter_with_k_memory(l, 1)) == [(0,), (1,), (2,)]\n", 1173 | "assert isinstance(iter_with_k_memory([], 1), GeneratorType)" 1174 | ] 1175 | }, 1176 | { 1177 | "cell_type": "markdown", 1178 | "metadata": {}, 1179 | "source": [ 1180 | "### 3.3 Functional python" 1181 | ] 1182 | }, 1183 | { 1184 | "cell_type": "markdown", 1185 | "metadata": {}, 1186 | "source": [ 1187 | "#### 3.3.1 Map [3 points]\n", 1188 | "\n", 1189 | "Write a function that returns the uppercase version of a letter, if it was lower case and lower case if it was upper case. Use the map function to transform a string this way." 1190 | ] 1191 | }, 1192 | { 1193 | "cell_type": "code", 1194 | "execution_count": null, 1195 | "metadata": { 1196 | "deletable": false, 1197 | "nbgrader": { 1198 | "cell_type": "code", 1199 | "checksum": "1b5f48a5e4c598191971fc0a409c16e5", 1200 | "grade": false, 1201 | "grade_id": "cell-aef25ccd30b50c67", 1202 | "locked": false, 1203 | "schema_version": 3, 1204 | "solution": true, 1205 | "task": false 1206 | } 1207 | }, 1208 | "outputs": [], 1209 | "source": [ 1210 | "def upper_lower(character: str) -> str:\n", 1211 | " # YOUR CODE HERE\n", 1212 | " raise NotImplementedError()" 1213 | ] 1214 | }, 1215 | { 1216 | "cell_type": "code", 1217 | "execution_count": null, 1218 | "metadata": { 1219 | "deletable": false, 1220 | "editable": false, 1221 | "nbgrader": { 1222 | "cell_type": "code", 1223 | "checksum": "42217effe47f23451361caecb5b901b2", 1224 | "grade": true, 1225 | "grade_id": "cell-d67a26f3aaf855bf", 1226 | "locked": true, 1227 | "points": 3, 1228 | "schema_version": 3, 1229 | "solution": false, 1230 | "task": false 1231 | } 1232 | }, 1233 | "outputs": [], 1234 | "source": [ 1235 | "assert \"\".join(list(map(upper_lower, \"This iS A nOrmAl SentEnce\"))) == \"tHIS Is a NoRMaL sENTeNCE\"\n", 1236 | "assert \"\".join(list(map(upper_lower, \"AbSoLuTe MADness\"))) == \"aBsOlUtE madNESS\"" 1237 | ] 1238 | }, 1239 | { 1240 | "cell_type": "markdown", 1241 | "metadata": { 1242 | "deletable": false, 1243 | "editable": false, 1244 | "nbgrader": { 1245 | "cell_type": "markdown", 1246 | "checksum": "a96c514b6d798a28a962ae9447fae7d2", 1247 | "grade": false, 1248 | "grade_id": "cell-f56952c2a3e67e00", 1249 | "locked": true, 1250 | "schema_version": 3, 1251 | "solution": false, 1252 | "task": false 1253 | } 1254 | }, 1255 | "source": [ 1256 | "#### 3.3.2 Filter [3 points]\n", 1257 | "\n", 1258 | "Write a function that can be used with filter to filter out numeric characters in a string." 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "code", 1263 | "execution_count": null, 1264 | "metadata": { 1265 | "deletable": false, 1266 | "nbgrader": { 1267 | "cell_type": "code", 1268 | "checksum": "935355a0d9ccf36c258cdc1ff3f01271", 1269 | "grade": false, 1270 | "grade_id": "cell-ec81b4f71ad41351", 1271 | "locked": false, 1272 | "schema_version": 3, 1273 | "solution": true, 1274 | "task": false 1275 | } 1276 | }, 1277 | "outputs": [], 1278 | "source": [ 1279 | "def just_alphabet(character: str) -> bool:\n", 1280 | " # YOUR CODE HERE\n", 1281 | " raise NotImplementedError()" 1282 | ] 1283 | }, 1284 | { 1285 | "cell_type": "code", 1286 | "execution_count": null, 1287 | "metadata": { 1288 | "deletable": false, 1289 | "editable": false, 1290 | "nbgrader": { 1291 | "cell_type": "code", 1292 | "checksum": "44eaf774b2e2a50153240bf5d702df6e", 1293 | "grade": true, 1294 | "grade_id": "cell-bec2c98a12c3ba20", 1295 | "locked": true, 1296 | "points": 3, 1297 | "schema_version": 3, 1298 | "solution": false, 1299 | "task": false 1300 | } 1301 | }, 1302 | "outputs": [], 1303 | "source": [ 1304 | "assert \"\".join(list(filter(just_alphabet, \"LE3T'S G0O\"))) == \"LET'S GO\"\n", 1305 | "assert \"\".join(list(filter(just_alphabet, \"4321\"))) == \"\"" 1306 | ] 1307 | }, 1308 | { 1309 | "cell_type": "markdown", 1310 | "metadata": { 1311 | "deletable": false, 1312 | "editable": false, 1313 | "nbgrader": { 1314 | "cell_type": "markdown", 1315 | "checksum": "740ebbc00f1d10ee5e49b29ec31a3988", 1316 | "grade": false, 1317 | "grade_id": "cell-a764e5883c60f784", 1318 | "locked": true, 1319 | "schema_version": 3, 1320 | "solution": false, 1321 | "task": false 1322 | } 1323 | }, 1324 | "source": [ 1325 | "#### 3.3.3 Reduce [4 points]\n", 1326 | "\n", 1327 | "Write a function that can concatenate any elements together as strings. Use the reduce function to demonstrate." 1328 | ] 1329 | }, 1330 | { 1331 | "cell_type": "code", 1332 | "execution_count": null, 1333 | "metadata": { 1334 | "deletable": false, 1335 | "nbgrader": { 1336 | "cell_type": "code", 1337 | "checksum": "cb81c076ab1b96701670622b24b4ff31", 1338 | "grade": false, 1339 | "grade_id": "cell-40d5eacebbe06cf7", 1340 | "locked": false, 1341 | "schema_version": 3, 1342 | "solution": true, 1343 | "task": false 1344 | } 1345 | }, 1346 | "outputs": [], 1347 | "source": [ 1348 | "def concat(string1: str, string2: Any) -> str:\n", 1349 | " # YOUR CODE HERE\n", 1350 | " raise NotImplementedError()" 1351 | ] 1352 | }, 1353 | { 1354 | "cell_type": "code", 1355 | "execution_count": null, 1356 | "metadata": { 1357 | "deletable": false, 1358 | "editable": false, 1359 | "nbgrader": { 1360 | "cell_type": "code", 1361 | "checksum": "a32954efcf644119530dc4e2227e0aa7", 1362 | "grade": true, 1363 | "grade_id": "cell-d0e7ea0f483ba9d8", 1364 | "locked": true, 1365 | "points": 4, 1366 | "schema_version": 3, 1367 | "solution": false, 1368 | "task": false 1369 | } 1370 | }, 1371 | "outputs": [], 1372 | "source": [ 1373 | "assert reduce(concat, [\"I\", \"\\'\", \"ve\", \" learned\", \" P\", \"ytho\", \"n\", \" <\", 3], \"\") == \"I've learned Python <3\"\n", 1374 | "assert reduce(concat, [], \"\") == \"\"\n", 1375 | "assert reduce(concat, [\"2\", 2, 3.2, (3, 2), {2: 3}], \"\") == \"223.2(3, 2){2: 3}\"" 1376 | ] 1377 | } 1378 | ], 1379 | "metadata": { 1380 | "kernelspec": { 1381 | "display_name": "Python 3 (ipykernel)", 1382 | "language": "python", 1383 | "name": "python3" 1384 | }, 1385 | "language_info": { 1386 | "codemirror_mode": { 1387 | "name": "ipython", 1388 | "version": 3 1389 | }, 1390 | "file_extension": ".py", 1391 | "mimetype": "text/x-python", 1392 | "name": "python", 1393 | "nbconvert_exporter": "python", 1394 | "pygments_lexer": "ipython3", 1395 | "version": "3.10.6" 1396 | }, 1397 | "toc": { 1398 | "base_numbering": 1, 1399 | "nav_menu": {}, 1400 | "number_sections": true, 1401 | "sideBar": true, 1402 | "skip_h1_title": false, 1403 | "title_cell": "Table of Contents", 1404 | "title_sidebar": "Contents", 1405 | "toc_cell": false, 1406 | "toc_position": {}, 1407 | "toc_section_display": true, 1408 | "toc_window_display": true 1409 | }, 1410 | "vscode": { 1411 | "interpreter": { 1412 | "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" 1413 | } 1414 | } 1415 | }, 1416 | "nbformat": 4, 1417 | "nbformat_minor": 2 1418 | } 1419 | -------------------------------------------------------------------------------- /assignments/2_Builtin_types_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "deletable": false, 7 | "editable": false, 8 | "nbgrader": { 9 | "cell_type": "markdown", 10 | "checksum": "880abeff0ad43082ade59a7d00703ef1", 11 | "grade": false, 12 | "grade_id": "cell-b32cc7a0861f7655", 13 | "locked": true, 14 | "schema_version": 3, 15 | "solution": false, 16 | "task": false 17 | } 18 | }, 19 | "source": [ 20 | "# Programming in Python\n", 21 | "\n", 22 | "__Laboratory 02, Type system and built-in types__\n", 23 | "\n", 24 | "__Sept 27, 2022__\n", 25 | "\n", 26 | "__Submission deadline: Sept 29 23:59__\n", 27 | "\n", 28 | "__Submissions must be made through Github Classroom__\n", 29 | "\n", 30 | "Some exercises are graded.\n", 31 | "The maximum score you can get is 25 points.\n", 32 | "The score value of graded exercises is listed in the instructions.\n", 33 | "\n", 34 | "You are free to use any material (lecture, Stackoverflow etc.) except full solutions but you should first try to figure them out on your own.\n", 35 | "\n", 36 | "We use an [nbgrader](https://nbgrader.readthedocs.io/en/stable/index.html) for autograding the assignments. Many cells are read-only. Please **DO NOT** delete or copy cells. You can add new cells but your solutions should go into the designated cells.\n", 37 | "\n", 38 | "We will use the wikipedia module for some exercises. It can be installed by running pip from Jupyter:\n", 39 | "\n", 40 | "The Python Standard Library has solutions for many of these tasks but you should solve them without relying on the standard library." 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "We will use the `wikipedia` module in notebook." 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "metadata": {}, 54 | "outputs": [], 55 | "source": [ 56 | "! pip install wikipedia" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": { 62 | "deletable": false, 63 | "editable": false, 64 | "nbgrader": { 65 | "cell_type": "markdown", 66 | "checksum": "278581dc95c79f1f6dae5c7fd4e46355", 67 | "grade": false, 68 | "grade_id": "cell-76aee6a76b5a3976", 69 | "locked": true, 70 | "schema_version": 3, 71 | "solution": false, 72 | "task": false 73 | } 74 | }, 75 | "source": [ 76 | "# 1. Define a function that takes a sequence as its input and returns whether the sequence is symmetric. A sequence is symmetric if it is equal to its reverse." 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": null, 82 | "metadata": { 83 | "deletable": false, 84 | "nbgrader": { 85 | "cell_type": "code", 86 | "checksum": "fa5cf582d5918fcc3f83632867234881", 87 | "grade": false, 88 | "grade_id": "cell-9063e2fba9c7157d", 89 | "locked": false, 90 | "schema_version": 3, 91 | "solution": true, 92 | "task": false 93 | } 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "# YOUR CODE HERE\n", 98 | "raise NotImplementedError()" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "metadata": { 105 | "deletable": false, 106 | "editable": false, 107 | "nbgrader": { 108 | "cell_type": "code", 109 | "checksum": "53030b3050410ac3dd0c4b6548bce575", 110 | "grade": true, 111 | "grade_id": "cell-58b3ad7e361196e7", 112 | "locked": true, 113 | "points": 0, 114 | "schema_version": 3, 115 | "solution": false, 116 | "task": false 117 | } 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "assert is_symmetric([1]) is True\n", 122 | "assert is_symmetric([None, None]) is True\n", 123 | "assert is_symmetric([]) is True\n", 124 | "assert is_symmetric([1, 2, 3, 1]) is False\n", 125 | "assert is_symmetric([1, \"foo\", \"bar\", \"foo\", 1]) is True\n", 126 | "assert is_symmetric(\"abcba\") is True\n", 127 | "assert is_symmetric(list(range(100)) + list(range(99, -1, -1))) is True\n", 128 | "assert is_symmetric(list(range(100)) + list(range(100, 0, -1))) is False\n", 129 | "assert is_symmetric([1, \"1\"]) is False" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "# String manipulation" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": { 142 | "deletable": false, 143 | "editable": false, 144 | "nbgrader": { 145 | "cell_type": "markdown", 146 | "checksum": "d7145e1374fb5649087517a5329eab1f", 147 | "grade": false, 148 | "grade_id": "cell-31ee0faefb4af111", 149 | "locked": true, 150 | "schema_version": 3, 151 | "solution": false, 152 | "task": false 153 | } 154 | }, 155 | "source": [ 156 | "## 2.1 Define a function that takes a string as its input and return a dictionary with the character frequencies.\n", 157 | "\n", 158 | "Do not use the `collections` module." 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": { 165 | "deletable": false, 166 | "nbgrader": { 167 | "cell_type": "code", 168 | "checksum": "8506743122a0b94c80856ef506a52943", 169 | "grade": false, 170 | "grade_id": "cell-7fbaf8610be225a9", 171 | "locked": false, 172 | "schema_version": 3, 173 | "solution": true, 174 | "task": false 175 | } 176 | }, 177 | "outputs": [], 178 | "source": [ 179 | "# YOUR CODE HERE\n", 180 | "raise NotImplementedError()" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": { 187 | "deletable": false, 188 | "editable": false, 189 | "nbgrader": { 190 | "cell_type": "code", 191 | "checksum": "acdcafaae2c18bb26e0756eaa5f46784", 192 | "grade": true, 193 | "grade_id": "cell-ecedb93a6c391539", 194 | "locked": true, 195 | "points": 1, 196 | "schema_version": 3, 197 | "solution": false, 198 | "task": false 199 | } 200 | }, 201 | "outputs": [], 202 | "source": [ 203 | "assert char_freq(\"aba\") == {\"a\": 2, \"b\": 1}\n", 204 | "assert char_freq(\"\") == {}\n", 205 | "assert char_freq(\"ab\" * 100 + \"cd\") == {\"a\": 100, \"b\": 100, \"c\": 1, \"d\": 1}" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": { 211 | "deletable": false, 212 | "editable": false, 213 | "nbgrader": { 214 | "cell_type": "markdown", 215 | "checksum": "75b1d50a7420617534fcb14f0655ef26", 216 | "grade": false, 217 | "grade_id": "cell-ec941b4366dbd59f", 218 | "locked": true, 219 | "schema_version": 3, 220 | "solution": false, 221 | "task": false 222 | } 223 | }, 224 | "source": [ 225 | "## 2.2 (1p) Define a function that computes word frequencies in a text.\n", 226 | "\n", 227 | "You can split the text with `text.split()`." 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": { 234 | "deletable": false, 235 | "nbgrader": { 236 | "cell_type": "code", 237 | "checksum": "bf8a0b5a81497db7772c518226de81b4", 238 | "grade": false, 239 | "grade_id": "cell-4d4d473dd807f962", 240 | "locked": false, 241 | "schema_version": 3, 242 | "solution": true, 243 | "task": false 244 | } 245 | }, 246 | "outputs": [], 247 | "source": [ 248 | "# YOUR CODE HERE\n", 249 | "raise NotImplementedError()" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": null, 255 | "metadata": { 256 | "deletable": false, 257 | "editable": false, 258 | "nbgrader": { 259 | "cell_type": "code", 260 | "checksum": "8f6ef3c72803eddabaf6cae96540fd4d", 261 | "grade": true, 262 | "grade_id": "cell-1c299c2c935129e7", 263 | "locked": true, 264 | "points": 0, 265 | "schema_version": 3, 266 | "solution": false, 267 | "task": false 268 | } 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "s = \"the green tea and the black tea\"\n", 273 | "assert word_freq(s) == {\"the\": 2, \"tea\": 2, \"green\": 1, \"black\": 1, \"and\": 1}\n", 274 | "assert word_freq(\"\") == {}" 275 | ] 276 | }, 277 | { 278 | "cell_type": "markdown", 279 | "metadata": { 280 | "deletable": false, 281 | "editable": false, 282 | "nbgrader": { 283 | "cell_type": "markdown", 284 | "checksum": "b0f06ec50f0507bb0937ff5ced9330a3", 285 | "grade": false, 286 | "grade_id": "cell-82d37f261c619b9e", 287 | "locked": true, 288 | "schema_version": 3, 289 | "solution": false, 290 | "task": false 291 | } 292 | }, 293 | "source": [ 294 | "## 2.3 (2p) Define a function that takes two strings and decides whether they are anagrams. A string is an anagram of another string if its letters can be rearranged so that it equals the other string.¶\n", 295 | "\n", 296 | "For example:\n", 297 | "\n", 298 | "```\n", 299 | "abc -- bac\n", 300 | "aabb -- abab\n", 301 | "```\n", 302 | "\n", 303 | "Counter examples:\n", 304 | "\n", 305 | "```\n", 306 | "abc -- aabc\n", 307 | "abab -- aaab\n", 308 | "```" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": { 315 | "deletable": false, 316 | "nbgrader": { 317 | "cell_type": "code", 318 | "checksum": "239926542f0f02b5b5ff53eb054593d4", 319 | "grade": false, 320 | "grade_id": "cell-7ea849b6ffc4421a", 321 | "locked": false, 322 | "schema_version": 3, 323 | "solution": true, 324 | "task": false 325 | } 326 | }, 327 | "outputs": [], 328 | "source": [ 329 | "# YOUR CODE HERE\n", 330 | "raise NotImplementedError()" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": { 337 | "deletable": false, 338 | "editable": false, 339 | "nbgrader": { 340 | "cell_type": "code", 341 | "checksum": "6bf76b10878810a731e42bb6d3b7feb3", 342 | "grade": true, 343 | "grade_id": "cell-231f78d971a5d48b", 344 | "locked": true, 345 | "points": 2, 346 | "schema_version": 3, 347 | "solution": false, 348 | "task": false 349 | } 350 | }, 351 | "outputs": [], 352 | "source": [ 353 | "assert are_anagrams(\"\", \"\") is True\n", 354 | "assert are_anagrams(\"abc\", \"bac\") is True\n", 355 | "assert are_anagrams(\"aabb\", \"abab\") is True\n", 356 | "assert are_anagrams(\"abab\", \"aaab\") is False" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": { 362 | "deletable": false, 363 | "editable": false, 364 | "nbgrader": { 365 | "cell_type": "markdown", 366 | "checksum": "f2a8d18e481ff61915c28c7343c1dab3", 367 | "grade": false, 368 | "grade_id": "cell-c7ab3adcb5a39bac", 369 | "locked": true, 370 | "schema_version": 3, 371 | "solution": false, 372 | "task": false 373 | } 374 | }, 375 | "source": [ 376 | "## 2.4 (2p) Define a function that takes a string and returns its bigram frequencies as a dictionary.\n", 377 | "\n", 378 | "Character bigrams are pairs of subsequent characters. For example word apple contains the following bigrams: ap, pp, pl, le.\n", 379 | "\n", 380 | "Do not use the `collections` module." 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": { 387 | "deletable": false, 388 | "nbgrader": { 389 | "cell_type": "code", 390 | "checksum": "cb4f46142309d3a4026db197d82c46db", 391 | "grade": false, 392 | "grade_id": "cell-2669edc3d29b28b6", 393 | "locked": false, 394 | "schema_version": 3, 395 | "solution": true, 396 | "task": false 397 | } 398 | }, 399 | "outputs": [], 400 | "source": [ 401 | "# YOUR CODE HERE\n", 402 | "raise NotImplementedError()" 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "execution_count": null, 408 | "metadata": { 409 | "deletable": false, 410 | "editable": false, 411 | "nbgrader": { 412 | "cell_type": "code", 413 | "checksum": "7e547fa81e2763807d969ae959242333", 414 | "grade": true, 415 | "grade_id": "cell-5a3764a1d0909393", 416 | "locked": true, 417 | "points": 2, 418 | "schema_version": 3, 419 | "solution": false, 420 | "task": false 421 | } 422 | }, 423 | "outputs": [], 424 | "source": [ 425 | "assert get_char_bigrams(\"apple\") == {\"ap\": 1, \"pp\": 1, \"pl\": 1, \"le\": 1}\n", 426 | "assert get_char_bigrams(\"apple apple\") == {\n", 427 | " \"ap\": 2,\n", 428 | " \"pp\": 2,\n", 429 | " \"pl\": 2,\n", 430 | " \"le\": 2,\n", 431 | " \"e \": 1,\n", 432 | " \" a\": 1,\n", 433 | "}" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": { 439 | "deletable": false, 440 | "editable": false, 441 | "nbgrader": { 442 | "cell_type": "markdown", 443 | "checksum": "b7a0504ee16c0ac64f74aeae3018869b", 444 | "grade": false, 445 | "grade_id": "cell-843a6eedcc531992", 446 | "locked": true, 447 | "schema_version": 3, 448 | "solution": false, 449 | "task": false 450 | } 451 | }, 452 | "source": [ 453 | "# Wikipedia module\n", 454 | "\n", 455 | "The following exercises use the `wikipedia` package. The basic usage is illustrated below.\n", 456 | "\n", 457 | "The documentation is available [here](https://pypi.python.org/pypi/wikipedia/).\n", 458 | "\n", 459 | "Searching for pages:" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": null, 465 | "metadata": {}, 466 | "outputs": [], 467 | "source": [ 468 | "import wikipedia\n", 469 | "\n", 470 | "results = wikipedia.search(\"Vienna\")\n", 471 | "results" 472 | ] 473 | }, 474 | { 475 | "cell_type": "markdown", 476 | "metadata": { 477 | "deletable": false, 478 | "editable": false, 479 | "nbgrader": { 480 | "cell_type": "markdown", 481 | "checksum": "42e7bc1a469601ae635fe2d7d8f428ac", 482 | "grade": false, 483 | "grade_id": "cell-78e29f2660bc6fbf", 484 | "locked": true, 485 | "schema_version": 3, 486 | "solution": false, 487 | "task": false 488 | } 489 | }, 490 | "source": [ 491 | "Downloading an article:" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": null, 497 | "metadata": {}, 498 | "outputs": [], 499 | "source": [ 500 | "article = wikipedia.page(\"Budapest\")\n", 501 | "\n", 502 | "article.summary[:200]" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "metadata": { 508 | "deletable": false, 509 | "editable": false, 510 | "nbgrader": { 511 | "cell_type": "markdown", 512 | "checksum": "3b7cd156e6f361932e5cd2eafd0f3888", 513 | "grade": false, 514 | "grade_id": "cell-dc32405bbfb31d99", 515 | "locked": true, 516 | "schema_version": 3, 517 | "solution": false, 518 | "task": false 519 | } 520 | }, 521 | "source": [ 522 | "The content attribute contains the full text:" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": null, 528 | "metadata": {}, 529 | "outputs": [], 530 | "source": [ 531 | "type(article.content), len(article.content)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "markdown", 536 | "metadata": { 537 | "deletable": false, 538 | "editable": false, 539 | "nbgrader": { 540 | "cell_type": "markdown", 541 | "checksum": "0c48587c5a71b46669047f43917b37f0", 542 | "grade": false, 543 | "grade_id": "cell-ebc4708eb9ec8edb", 544 | "locked": true, 545 | "schema_version": 3, 546 | "solution": false, 547 | "task": false 548 | } 549 | }, 550 | "source": [ 551 | "By default the module downloads the English Wikipedia. The language can be changed the following way:" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": null, 557 | "metadata": {}, 558 | "outputs": [], 559 | "source": [ 560 | "wikipedia.set_lang(\"de\")" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": null, 566 | "metadata": {}, 567 | "outputs": [], 568 | "source": [ 569 | "wikipedia.search(\"Budapest\")" 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "execution_count": null, 575 | "metadata": {}, 576 | "outputs": [], 577 | "source": [ 578 | "de_article = wikipedia.page(\"Budapest\")\n", 579 | "de_article.summary[:100]" 580 | ] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "execution_count": null, 585 | "metadata": {}, 586 | "outputs": [], 587 | "source": [ 588 | "wikipedia.set_lang(\"en\")" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": { 594 | "deletable": false, 595 | "editable": false, 596 | "nbgrader": { 597 | "cell_type": "markdown", 598 | "checksum": "fa92e56d609af0b5c7559b090612bbc8", 599 | "grade": false, 600 | "grade_id": "cell-95be16d5cc290d23", 601 | "locked": true, 602 | "schema_version": 3, 603 | "solution": false, 604 | "task": false 605 | } 606 | }, 607 | "source": [ 608 | "## 3.1 (5p) Build a small Wikipedia corpus by downloading pages until their character count exceeds 100000. The corpus should be a list of strings.\n", 609 | "\n", 610 | "You can download arbitrary pages but some pages redirect to disambiguation pages. You should not pick these. Make sure the same page is not included multiple times." 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": null, 616 | "metadata": { 617 | "deletable": false, 618 | "nbgrader": { 619 | "cell_type": "code", 620 | "checksum": "04c921ddca419c1b935dfad982d37f2b", 621 | "grade": false, 622 | "grade_id": "cell-2e7be91b99158dca", 623 | "locked": false, 624 | "schema_version": 3, 625 | "solution": true, 626 | "task": false 627 | } 628 | }, 629 | "outputs": [], 630 | "source": [ 631 | "# YOUR CODE HERE\n", 632 | "raise NotImplementedError()" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": null, 638 | "metadata": { 639 | "deletable": false, 640 | "editable": false, 641 | "nbgrader": { 642 | "cell_type": "code", 643 | "checksum": "735a8d5d5b34d9882142362f49a58b48", 644 | "grade": true, 645 | "grade_id": "cell-f26aa917928c62c8", 646 | "locked": true, 647 | "points": 5, 648 | "schema_version": 3, 649 | "solution": false, 650 | "task": false 651 | } 652 | }, 653 | "outputs": [], 654 | "source": [ 655 | "corpus = build_wikipedia_corpus()\n", 656 | "# check types\n", 657 | "assert isinstance(corpus, list)\n", 658 | "assert isinstance(corpus[0], str)\n", 659 | "\n", 660 | "# check character count\n", 661 | "sum_length = 0\n", 662 | "for document in corpus:\n", 663 | " sum_length += len(document)\n", 664 | "assert sum_length >= 100000\n", 665 | "\n", 666 | "# no duplicate pages\n", 667 | "assert len(set(corpus)) == len(corpus)" 668 | ] 669 | }, 670 | { 671 | "cell_type": "markdown", 672 | "metadata": { 673 | "deletable": false, 674 | "editable": false, 675 | "nbgrader": { 676 | "cell_type": "markdown", 677 | "checksum": "1dbe701f50c44dbb49e4f54848b15c39", 678 | "grade": false, 679 | "grade_id": "cell-c560b4af51f932f1", 680 | "locked": true, 681 | "schema_version": 3, 682 | "solution": false, 683 | "task": false 684 | } 685 | }, 686 | "source": [ 687 | "## 3.2 (2p) Add a `min_char` parameter to `build_wikipedia_corpus` that defaults to 10000. You should stop building the corpus when this number is reached." 688 | ] 689 | }, 690 | { 691 | "cell_type": "code", 692 | "execution_count": null, 693 | "metadata": { 694 | "deletable": false, 695 | "nbgrader": { 696 | "cell_type": "code", 697 | "checksum": "84ae0263b8784b0d38ac61698af0f108", 698 | "grade": false, 699 | "grade_id": "cell-bf7ffeba0644e753", 700 | "locked": false, 701 | "schema_version": 3, 702 | "solution": true, 703 | "task": false 704 | } 705 | }, 706 | "outputs": [], 707 | "source": [ 708 | "# YOUR CODE HERE\n", 709 | "raise NotImplementedError()" 710 | ] 711 | }, 712 | { 713 | "cell_type": "code", 714 | "execution_count": null, 715 | "metadata": { 716 | "deletable": false, 717 | "editable": false, 718 | "nbgrader": { 719 | "cell_type": "code", 720 | "checksum": "319fc9a7638a850cbbb721ce34b3f613", 721 | "grade": true, 722 | "grade_id": "cell-68c3de00731699f8", 723 | "locked": true, 724 | "points": 2, 725 | "schema_version": 3, 726 | "solution": false, 727 | "task": false 728 | } 729 | }, 730 | "outputs": [], 731 | "source": [ 732 | "corpus = build_wikipedia_corpus(1000)\n", 733 | "# check types\n", 734 | "assert isinstance(corpus, list)\n", 735 | "assert isinstance(corpus[0], str)\n", 736 | "\n", 737 | "# check character count\n", 738 | "sum_length = 0\n", 739 | "for document in corpus:\n", 740 | " sum_length += len(document)\n", 741 | "assert sum_length >= 1000\n", 742 | "# shouldn't be too long\n", 743 | "assert sum_length < 100000" 744 | ] 745 | }, 746 | { 747 | "cell_type": "markdown", 748 | "metadata": { 749 | "deletable": false, 750 | "editable": false, 751 | "nbgrader": { 752 | "cell_type": "markdown", 753 | "checksum": "590518197131ed37b21e61b40309fd6a", 754 | "grade": false, 755 | "grade_id": "cell-a132e8fee5967805", 756 | "locked": true, 757 | "schema_version": 3, 758 | "solution": false, 759 | "task": false 760 | } 761 | }, 762 | "source": [ 763 | "## 3.3 (3p) Compute the word frequencies of your Wikipedia corpus. Return the $k$ most frequent words with their frequencies as a list of tuples. $k$ should default to 10. The list should be ordered by frequency starting from the most frequent.\n", 764 | "\n", 765 | "You can use str.split() for word tokenization." 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": null, 771 | "metadata": { 772 | "deletable": false, 773 | "nbgrader": { 774 | "cell_type": "code", 775 | "checksum": "54fdc3c7b8b56fc1f4878e468ed37050", 776 | "grade": false, 777 | "grade_id": "cell-8c167ceb2a4c65f1", 778 | "locked": false, 779 | "schema_version": 3, 780 | "solution": true, 781 | "task": false 782 | } 783 | }, 784 | "outputs": [], 785 | "source": [ 786 | "# YOUR CODE HERE\n", 787 | "raise NotImplementedError()" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": null, 793 | "metadata": { 794 | "deletable": false, 795 | "editable": false, 796 | "nbgrader": { 797 | "cell_type": "code", 798 | "checksum": "2428800ef5d9c86e3508622b3e067c00", 799 | "grade": true, 800 | "grade_id": "cell-d17a9e499c13bd35", 801 | "locked": true, 802 | "points": 3, 803 | "schema_version": 3, 804 | "solution": false, 805 | "task": false 806 | } 807 | }, 808 | "outputs": [], 809 | "source": [ 810 | "top_words_and_freqs = get_corpus_word_frequencies(corpus)\n", 811 | "assert isinstance(top_words_and_freqs, list)\n", 812 | "assert len(top_words_and_freqs) == 10\n", 813 | "assert isinstance(top_words_and_freqs[0], tuple)\n", 814 | "\n", 815 | "# Frequency of the most frequent word\n", 816 | "fr = top_words_and_freqs[0][1]\n", 817 | "\n", 818 | "for word, freq in top_words_and_freqs:\n", 819 | " assert isinstance(word, str)\n", 820 | " assert isinstance(freq, int)\n", 821 | " # Check decreasing order\n", 822 | " assert freq <= fr\n", 823 | " fr = freq\n", 824 | "\n", 825 | "top_words_and_freqs5 = get_corpus_word_frequencies(corpus, k=5)\n", 826 | "assert len(top_words_and_freqs5) == 5" 827 | ] 828 | }, 829 | { 830 | "cell_type": "markdown", 831 | "metadata": { 832 | "deletable": false, 833 | "editable": false, 834 | "nbgrader": { 835 | "cell_type": "markdown", 836 | "checksum": "5ce6aaedaa2eacbc1d78a1f49e303b12", 837 | "grade": false, 838 | "grade_id": "cell-2734f82ba43bf34d", 839 | "locked": true, 840 | "schema_version": 3, 841 | "solution": false, 842 | "task": false 843 | } 844 | }, 845 | "source": [ 846 | "## 3.4.2 Print the ten most frequent words and their frequency in the following format:\n", 847 | "\n", 848 | "```\n", 849 | "a12\n", 850 | "an11\n", 851 | "```" 852 | ] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "execution_count": null, 857 | "metadata": { 858 | "deletable": false, 859 | "nbgrader": { 860 | "cell_type": "code", 861 | "checksum": "3a514dbba3ed6e12a39fcfcbe51f0c7a", 862 | "grade": false, 863 | "grade_id": "cell-bc2d984be320ff7c", 864 | "locked": false, 865 | "schema_version": 3, 866 | "solution": true, 867 | "task": false 868 | } 869 | }, 870 | "outputs": [], 871 | "source": [ 872 | "# YOUR CODE HERE\n", 873 | "raise NotImplementedError()" 874 | ] 875 | }, 876 | { 877 | "cell_type": "markdown", 878 | "metadata": {}, 879 | "source": [ 880 | "# Linear algebra" 881 | ] 882 | }, 883 | { 884 | "cell_type": "markdown", 885 | "metadata": { 886 | "deletable": false, 887 | "editable": false, 888 | "nbgrader": { 889 | "cell_type": "markdown", 890 | "checksum": "8ecd0d97c2fae3693da35696561b2da4", 891 | "grade": false, 892 | "grade_id": "cell-3ff21ab0f0a271e3", 893 | "locked": true, 894 | "schema_version": 3, 895 | "solution": false, 896 | "task": false 897 | } 898 | }, 899 | "source": [ 900 | "## 4.1 (2p) Define a function that takes a list of lists and decided if it is a matrix. A list of lists is a matrix if each nested list is the same length." 901 | ] 902 | }, 903 | { 904 | "cell_type": "code", 905 | "execution_count": null, 906 | "metadata": { 907 | "deletable": false, 908 | "nbgrader": { 909 | "cell_type": "code", 910 | "checksum": "1e8206f402442b88e543379b500409d5", 911 | "grade": false, 912 | "grade_id": "cell-90b341f4b426d33d", 913 | "locked": false, 914 | "schema_version": 3, 915 | "solution": true, 916 | "task": false 917 | } 918 | }, 919 | "outputs": [], 920 | "source": [ 921 | "# YOUR CODE HERE\n", 922 | "raise NotImplementedError()" 923 | ] 924 | }, 925 | { 926 | "cell_type": "code", 927 | "execution_count": null, 928 | "metadata": { 929 | "deletable": false, 930 | "editable": false, 931 | "nbgrader": { 932 | "cell_type": "code", 933 | "checksum": "a9b81810999e0b3b9b703bfe5beb90db", 934 | "grade": true, 935 | "grade_id": "cell-218d090c410748b5", 936 | "locked": true, 937 | "points": 2, 938 | "schema_version": 3, 939 | "solution": false, 940 | "task": false 941 | } 942 | }, 943 | "outputs": [], 944 | "source": [ 945 | "assert is_matrix([]) is True\n", 946 | "assert is_matrix([[], []]) is True\n", 947 | "assert is_matrix([[1, 2], [2, 1]]) is True\n", 948 | "assert is_matrix([[1], [2, 1]]) is False\n", 949 | "assert is_matrix([[1, 2], [2, 1], [3, 4]]) is True\n", 950 | "assert is_matrix([[1, 2], [2, 1], [3, 4, 5]]) is False" 951 | ] 952 | }, 953 | { 954 | "cell_type": "markdown", 955 | "metadata": { 956 | "deletable": false, 957 | "editable": false, 958 | "nbgrader": { 959 | "cell_type": "markdown", 960 | "checksum": "b1208c1a5d0055907cfa32d02091d101", 961 | "grade": false, 962 | "grade_id": "cell-2ff6396d150fb578", 963 | "locked": true, 964 | "schema_version": 3, 965 | "solution": false, 966 | "task": false 967 | } 968 | }, 969 | "source": [ 970 | "## 4.2 (2p) Define a function that takes a matrix as an input represented as a list of lists. Return its transpose without changing the original matrix.\n", 971 | "\n", 972 | "Raise a `ValueError` if the input is not a valid matrix. Don't forget to reuse your code." 973 | ] 974 | }, 975 | { 976 | "cell_type": "code", 977 | "execution_count": null, 978 | "metadata": { 979 | "deletable": false, 980 | "nbgrader": { 981 | "cell_type": "code", 982 | "checksum": "6d72fe6b282df6792f21ba7bb1609def", 983 | "grade": false, 984 | "grade_id": "cell-adee24575dcbac8e", 985 | "locked": false, 986 | "schema_version": 3, 987 | "solution": true, 988 | "task": false 989 | } 990 | }, 991 | "outputs": [], 992 | "source": [ 993 | "# YOUR CODE HERE\n", 994 | "raise NotImplementedError()" 995 | ] 996 | }, 997 | { 998 | "cell_type": "code", 999 | "execution_count": null, 1000 | "metadata": { 1001 | "deletable": false, 1002 | "editable": false, 1003 | "nbgrader": { 1004 | "cell_type": "code", 1005 | "checksum": "d981175f24d7b7a291441a7845795dc8", 1006 | "grade": true, 1007 | "grade_id": "cell-9b412cb3d0c0506a", 1008 | "locked": true, 1009 | "points": 2, 1010 | "schema_version": 3, 1011 | "solution": false, 1012 | "task": false 1013 | } 1014 | }, 1015 | "outputs": [], 1016 | "source": [ 1017 | "m1 = [[1, 2, 3], [4, 5, 6]]\n", 1018 | "m2 = [[1, 4], [2, 5], [3, 6]]\n", 1019 | "\n", 1020 | "assert transpose([[1]]) == [[1]]\n", 1021 | "assert transpose(m1) == m2\n", 1022 | "# m1 is unchanged\n", 1023 | "assert m1 == [[1, 2, 3], [4, 5, 6]]\n", 1024 | "\n", 1025 | "# transpose is an involution (self-inverse)\n", 1026 | "assert transpose(transpose(m1)) == m1" 1027 | ] 1028 | }, 1029 | { 1030 | "cell_type": "markdown", 1031 | "metadata": { 1032 | "deletable": false, 1033 | "editable": false, 1034 | "nbgrader": { 1035 | "cell_type": "markdown", 1036 | "checksum": "109bce0837c5a6da2a1c2f2fdc51540f", 1037 | "grade": false, 1038 | "grade_id": "cell-6447dbadcb503d60", 1039 | "locked": true, 1040 | "schema_version": 3, 1041 | "solution": false, 1042 | "task": false 1043 | } 1044 | }, 1045 | "source": [ 1046 | "## 4.3 (1p) Define a function that takes a matrix as its input and returns the row-wise maximum elements as a list.\n", 1047 | "\n", 1048 | "Raise a `ValueError` if the input is not a valid matrix. Don't forget to reuse your code." 1049 | ] 1050 | }, 1051 | { 1052 | "cell_type": "code", 1053 | "execution_count": null, 1054 | "metadata": { 1055 | "deletable": false, 1056 | "nbgrader": { 1057 | "cell_type": "code", 1058 | "checksum": "2652c8da70bda918810c923555ce3aab", 1059 | "grade": false, 1060 | "grade_id": "cell-21f91aada57c0332", 1061 | "locked": false, 1062 | "schema_version": 3, 1063 | "solution": true, 1064 | "task": false 1065 | } 1066 | }, 1067 | "outputs": [], 1068 | "source": [ 1069 | "# YOUR CODE HERE\n", 1070 | "raise NotImplementedError()" 1071 | ] 1072 | }, 1073 | { 1074 | "cell_type": "code", 1075 | "execution_count": null, 1076 | "metadata": { 1077 | "deletable": false, 1078 | "editable": false, 1079 | "nbgrader": { 1080 | "cell_type": "code", 1081 | "checksum": "493a7791aec07cff034cc3031467f10e", 1082 | "grade": true, 1083 | "grade_id": "cell-eb440f91ea977032", 1084 | "locked": true, 1085 | "points": 1, 1086 | "schema_version": 3, 1087 | "solution": false, 1088 | "task": false 1089 | } 1090 | }, 1091 | "outputs": [], 1092 | "source": [ 1093 | "assert rowwise_max(\n", 1094 | " [\n", 1095 | " [1, 2, 3],\n", 1096 | " [1, 4, 3],\n", 1097 | " [1, 5, 3],\n", 1098 | " ]\n", 1099 | ") == [3, 4, 5]\n", 1100 | "\n", 1101 | "assert rowwise_max([list(range(1000))]) == [999]\n", 1102 | "assert rowwise_max(transpose([list(range(1000))])) == list(range(1000))" 1103 | ] 1104 | }, 1105 | { 1106 | "cell_type": "markdown", 1107 | "metadata": { 1108 | "deletable": false, 1109 | "editable": false, 1110 | "nbgrader": { 1111 | "cell_type": "markdown", 1112 | "checksum": "2f4ee5b9e67cc2fe625efe25d640a7d9", 1113 | "grade": false, 1114 | "grade_id": "cell-d497ed3bf67b2435", 1115 | "locked": true, 1116 | "schema_version": 3, 1117 | "solution": false, 1118 | "task": false 1119 | } 1120 | }, 1121 | "source": [ 1122 | "## 4.4 (2p) Define a function that takes a matrix as its input and returns the column-wise maximum elements as a list.\n", 1123 | "\n", 1124 | "Try to reuse your previous functions." 1125 | ] 1126 | }, 1127 | { 1128 | "cell_type": "code", 1129 | "execution_count": null, 1130 | "metadata": { 1131 | "deletable": false, 1132 | "nbgrader": { 1133 | "cell_type": "code", 1134 | "checksum": "b9ea1d5db6192330853b033b5b00ab43", 1135 | "grade": false, 1136 | "grade_id": "cell-bde0cb9cf8d75c89", 1137 | "locked": false, 1138 | "schema_version": 3, 1139 | "solution": true, 1140 | "task": false 1141 | } 1142 | }, 1143 | "outputs": [], 1144 | "source": [ 1145 | "# YOUR CODE HERE\n", 1146 | "raise NotImplementedError()" 1147 | ] 1148 | }, 1149 | { 1150 | "cell_type": "code", 1151 | "execution_count": null, 1152 | "metadata": { 1153 | "deletable": false, 1154 | "editable": false, 1155 | "nbgrader": { 1156 | "cell_type": "code", 1157 | "checksum": "91e72b16b0232ce1ff967c81f7bbd86a", 1158 | "grade": true, 1159 | "grade_id": "cell-142e6034ed15005b", 1160 | "locked": true, 1161 | "points": 2, 1162 | "schema_version": 3, 1163 | "solution": false, 1164 | "task": false 1165 | } 1166 | }, 1167 | "outputs": [], 1168 | "source": [ 1169 | "assert columnwise_max(\n", 1170 | " [\n", 1171 | " [1, 2, 3],\n", 1172 | " [1, 4, 3],\n", 1173 | " [1, 5, 3],\n", 1174 | " ]\n", 1175 | ") == [1, 5, 3]\n", 1176 | "\n", 1177 | "assert columnwise_max([list(range(1000))]) == list(range(1000))\n", 1178 | "assert columnwise_max(transpose([list(range(1000))])) == [999]" 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "markdown", 1183 | "metadata": { 1184 | "deletable": false, 1185 | "editable": false, 1186 | "nbgrader": { 1187 | "cell_type": "markdown", 1188 | "checksum": "745bf8c744aea75ba83a4b61e83d9042", 1189 | "grade": false, 1190 | "grade_id": "cell-6121a01202b8b3f4", 1191 | "locked": true, 1192 | "schema_version": 3, 1193 | "solution": false, 1194 | "task": false 1195 | } 1196 | }, 1197 | "source": [ 1198 | "## 4.5 (3p) Define a function that takes two vectors represented as lists and returns their L2 distance.\n", 1199 | "\n", 1200 | "Also called Euclidean distance." 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": null, 1206 | "metadata": { 1207 | "deletable": false, 1208 | "nbgrader": { 1209 | "cell_type": "code", 1210 | "checksum": "7c89e75bbe528f9924c297e03cb3d898", 1211 | "grade": false, 1212 | "grade_id": "cell-849b6ea4c65f058e", 1213 | "locked": false, 1214 | "schema_version": 3, 1215 | "solution": true, 1216 | "task": false 1217 | } 1218 | }, 1219 | "outputs": [], 1220 | "source": [ 1221 | "# YOUR CODE HERE\n", 1222 | "raise NotImplementedError()" 1223 | ] 1224 | }, 1225 | { 1226 | "cell_type": "code", 1227 | "execution_count": null, 1228 | "metadata": { 1229 | "deletable": false, 1230 | "editable": false, 1231 | "nbgrader": { 1232 | "cell_type": "code", 1233 | "checksum": "2da045094b58ca5420f36e4de76a042c", 1234 | "grade": true, 1235 | "grade_id": "cell-4c9f06a8534cbefc", 1236 | "locked": true, 1237 | "points": 3, 1238 | "schema_version": 3, 1239 | "solution": false, 1240 | "task": false 1241 | } 1242 | }, 1243 | "outputs": [], 1244 | "source": [ 1245 | "assert abs(l2_distance([1, 0], [1, 0]) - 0) < 1e-5\n", 1246 | "assert abs(l2_distance([1, 0], [0, 1]) - 2**0.5) < 1e-5" 1247 | ] 1248 | }, 1249 | { 1250 | "cell_type": "markdown", 1251 | "metadata": { 1252 | "deletable": false, 1253 | "editable": false, 1254 | "nbgrader": { 1255 | "cell_type": "markdown", 1256 | "checksum": "4f3d2fa22bf571d8c99b44d27de9730c", 1257 | "grade": false, 1258 | "grade_id": "cell-8d38b914ec98f2be", 1259 | "locked": true, 1260 | "schema_version": 3, 1261 | "solution": false, 1262 | "task": false 1263 | } 1264 | }, 1265 | "source": [ 1266 | "## 4.6 Create a function that takes a matrix of size NxM and returns the pairwise L2 distance of each row pair.\n", 1267 | "\n", 1268 | "For input $A^{N \\times M}$, the output should be $D^{N \\times N}$ where: $D_{i,j} = L_2 (A_i, A_j)$" 1269 | ] 1270 | }, 1271 | { 1272 | "cell_type": "code", 1273 | "execution_count": null, 1274 | "metadata": { 1275 | "deletable": false, 1276 | "nbgrader": { 1277 | "cell_type": "code", 1278 | "checksum": "80bcb4c2f3f8cead8816494483b6ec74", 1279 | "grade": false, 1280 | "grade_id": "cell-644d3391fc995ee9", 1281 | "locked": false, 1282 | "schema_version": 3, 1283 | "solution": true, 1284 | "task": false 1285 | } 1286 | }, 1287 | "outputs": [], 1288 | "source": [ 1289 | "# YOUR CODE HERE\n", 1290 | "raise NotImplementedError()" 1291 | ] 1292 | }, 1293 | { 1294 | "cell_type": "code", 1295 | "execution_count": null, 1296 | "metadata": { 1297 | "deletable": false, 1298 | "editable": false, 1299 | "nbgrader": { 1300 | "cell_type": "code", 1301 | "checksum": "b1ad00fa9849166dcb1604d4fa161036", 1302 | "grade": true, 1303 | "grade_id": "cell-8bc3cf03b303c7c3", 1304 | "locked": true, 1305 | "points": 0, 1306 | "schema_version": 3, 1307 | "solution": false, 1308 | "task": false 1309 | } 1310 | }, 1311 | "outputs": [], 1312 | "source": [ 1313 | "D = pairwise_row_distance([[1, 0, 2], [0, 2, 1]])\n", 1314 | "# should be a matrix\n", 1315 | "assert is_matrix(D) is True\n", 1316 | "assert len(D) == 2\n", 1317 | "assert len(D[0]) == 2\n", 1318 | "assert D[0][0] == 0\n", 1319 | "assert abs(D[0][1] - 2.449489742783178) < 1e-3\n", 1320 | "assert abs(D[1][0] - 2.449489742783178) < 1e-3\n", 1321 | "assert D[1][1] == 0\n", 1322 | "\n", 1323 | "# should be symmetric\n", 1324 | "assert transpose(D) == D" 1325 | ] 1326 | } 1327 | ], 1328 | "metadata": { 1329 | "kernelspec": { 1330 | "display_name": "Python 3 (ipykernel)", 1331 | "language": "python", 1332 | "name": "python3" 1333 | }, 1334 | "language_info": { 1335 | "codemirror_mode": { 1336 | "name": "ipython", 1337 | "version": 3 1338 | }, 1339 | "file_extension": ".py", 1340 | "mimetype": "text/x-python", 1341 | "name": "python", 1342 | "nbconvert_exporter": "python", 1343 | "pygments_lexer": "ipython3", 1344 | "version": "3.8.5" 1345 | }, 1346 | "toc": { 1347 | "base_numbering": 1, 1348 | "nav_menu": {}, 1349 | "number_sections": false, 1350 | "sideBar": true, 1351 | "skip_h1_title": false, 1352 | "title_cell": "Table of Contents", 1353 | "title_sidebar": "Contents", 1354 | "toc_cell": false, 1355 | "toc_position": { 1356 | "height": "calc(100% - 180px)", 1357 | "left": "10px", 1358 | "top": "150px", 1359 | "width": "662px" 1360 | }, 1361 | "toc_section_display": true, 1362 | "toc_window_display": true 1363 | } 1364 | }, 1365 | "nbformat": 4, 1366 | "nbformat_minor": 2 1367 | } 1368 | -------------------------------------------------------------------------------- /assignments/1_Python_introduction_exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "deletable": false, 7 | "editable": false, 8 | "nbgrader": { 9 | "cell_type": "markdown", 10 | "checksum": "a4ae6c6d9dad9d64ca626c52248df874", 11 | "grade": false, 12 | "grade_id": "cell-b7412adc4e975740", 13 | "locked": true, 14 | "schema_version": 3, 15 | "solution": false, 16 | "task": false 17 | } 18 | }, 19 | "source": [ 20 | "# Programming in Python\n", 21 | "\n", 22 | "__Laboratory 1, Introduction__\n", 23 | "\n", 24 | "__Sept 26, 2022__\n", 25 | "\n", 26 | "__Submission deadline: Sept 29 23:59__\n", 27 | "\n", 28 | "__Submissions must be made through Github Classroom__\n", 29 | "\n", 30 | "Some exercises are graded.\n", 31 | "The maximum score you can get is 25 points.\n", 32 | "The score value of graded exercises is listed in the instructions.\n", 33 | "\n", 34 | "You are free to use any material (lecture, Stackoverflow etc.) except full solutions but you should first try to figure them out on your own.\n", 35 | "\n", 36 | "We use an [nbgrader](https://nbgrader.readthedocs.io/en/stable/index.html) for autograding the assignments. Many cells are read-only. Please **DO NOT** delete or copy cells. You can add new cells but your solutions should go into the designated cells." 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "deletable": false, 43 | "editable": false, 44 | "nbgrader": { 45 | "cell_type": "markdown", 46 | "checksum": "5d72fe892091ca92e9af47e1b4041a61", 47 | "grade": false, 48 | "grade_id": "cell-c1b4e57b70e15aa8", 49 | "locked": true, 50 | "schema_version": 3, 51 | "solution": false, 52 | "task": false 53 | } 54 | }, 55 | "source": [ 56 | "# `range()` practice\n", 57 | "\n", 58 | "## Print the numbers between 60 and 69 inclusive." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "deletable": false, 66 | "nbgrader": { 67 | "cell_type": "code", 68 | "checksum": "f8cea911828bf564dbe36285c3523029", 69 | "grade": false, 70 | "grade_id": "cell-306d37c936005bde", 71 | "locked": false, 72 | "schema_version": 3, 73 | "solution": true, 74 | "task": false 75 | } 76 | }, 77 | "outputs": [], 78 | "source": [ 79 | "# YOUR CODE HERE\n", 80 | "raise NotImplementedError()" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": { 86 | "deletable": false, 87 | "editable": false, 88 | "nbgrader": { 89 | "cell_type": "markdown", 90 | "checksum": "56e247bad450ab7567e0d914c66fa3f7", 91 | "grade": false, 92 | "grade_id": "cell-ee6316d7719a50ca", 93 | "locked": true, 94 | "schema_version": 3, 95 | "solution": false, 96 | "task": false 97 | } 98 | }, 99 | "source": [ 100 | "## Print the first 10 square numbers starting from 1." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": null, 106 | "metadata": { 107 | "deletable": false, 108 | "nbgrader": { 109 | "cell_type": "code", 110 | "checksum": "a922095e9cb741abf5b5b872a7c516e2", 111 | "grade": false, 112 | "grade_id": "cell-b5c9f9ae3534a9b4", 113 | "locked": false, 114 | "schema_version": 3, 115 | "solution": true, 116 | "task": false 117 | } 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "# YOUR CODE HERE\n", 122 | "raise NotImplementedError()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": { 128 | "deletable": false, 129 | "editable": false, 130 | "nbgrader": { 131 | "cell_type": "markdown", 132 | "checksum": "92d07cf852dc37dee06d049cf4437d72", 133 | "grade": false, 134 | "grade_id": "cell-cb7a56c8f3dc0601", 135 | "locked": true, 136 | "schema_version": 3, 137 | "solution": false, 138 | "task": false 139 | } 140 | }, 141 | "source": [ 142 | "## Print the numbers from 29 to 20 (backwards)." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": { 149 | "deletable": false, 150 | "nbgrader": { 151 | "cell_type": "code", 152 | "checksum": "17ed320fa33ace86a652f32817054587", 153 | "grade": false, 154 | "grade_id": "cell-3b0aefde40906e95", 155 | "locked": false, 156 | "schema_version": 3, 157 | "solution": true, 158 | "task": false 159 | } 160 | }, 161 | "outputs": [], 162 | "source": [ 163 | "# YOUR CODE HERE\n", 164 | "raise NotImplementedError()" 165 | ] 166 | }, 167 | { 168 | "cell_type": "markdown", 169 | "metadata": { 170 | "deletable": false, 171 | "editable": false, 172 | "nbgrader": { 173 | "cell_type": "markdown", 174 | "checksum": "855de7bf29ea896bc920f353ed5a2c07", 175 | "grade": false, 176 | "grade_id": "cell-f36aba34c82823a0", 177 | "locked": true, 178 | "schema_version": 3, 179 | "solution": false, 180 | "task": false 181 | } 182 | }, 183 | "source": [ 184 | "# Reading user input\n", 185 | "\n", 186 | "User input can be read with the `input()` function. It always returns a string which can be converted to integer using the `int()` function." 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": { 192 | "deletable": false, 193 | "editable": false, 194 | "nbgrader": { 195 | "cell_type": "markdown", 196 | "checksum": "ad3f58d5b0fff06df46cdaaaf072431f", 197 | "grade": false, 198 | "grade_id": "cell-569e884114d1be95", 199 | "locked": true, 200 | "schema_version": 3, 201 | "solution": false, 202 | "task": false 203 | } 204 | }, 205 | "source": [ 206 | "## Read a number N and print the numbers from 1 to N." 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": { 213 | "deletable": false, 214 | "nbgrader": { 215 | "cell_type": "code", 216 | "checksum": "db44b0c79306f9661db321bfcd363f8a", 217 | "grade": false, 218 | "grade_id": "cell-0a36206459fb1dd0", 219 | "locked": false, 220 | "schema_version": 3, 221 | "solution": true, 222 | "task": false 223 | } 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "# YOUR CODE HERE\n", 228 | "raise NotImplementedError()" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": { 234 | "deletable": false, 235 | "editable": false, 236 | "nbgrader": { 237 | "cell_type": "markdown", 238 | "checksum": "95f8a49976cfc6cd5b98845e929d04d4", 239 | "grade": false, 240 | "grade_id": "cell-9c605f8b56c8be50", 241 | "locked": true, 242 | "schema_version": 3, 243 | "solution": false, 244 | "task": false 245 | } 246 | }, 247 | "source": [ 248 | "## Read a number N and print the first N square numbers starting from 1." 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": null, 254 | "metadata": { 255 | "deletable": false, 256 | "nbgrader": { 257 | "cell_type": "code", 258 | "checksum": "dca979d65cb50d9ef23875d0ae812a6c", 259 | "grade": false, 260 | "grade_id": "cell-834b3f1952f3a615", 261 | "locked": false, 262 | "schema_version": 3, 263 | "solution": true, 264 | "task": false 265 | } 266 | }, 267 | "outputs": [], 268 | "source": [ 269 | "# YOUR CODE HERE\n", 270 | "raise NotImplementedError()" 271 | ] 272 | }, 273 | { 274 | "cell_type": "markdown", 275 | "metadata": { 276 | "deletable": false, 277 | "editable": false, 278 | "nbgrader": { 279 | "cell_type": "markdown", 280 | "checksum": "086cce9c80c0bb9d091da9fa4da29d35", 281 | "grade": false, 282 | "grade_id": "cell-f4dd49042e616070", 283 | "locked": true, 284 | "schema_version": 3, 285 | "solution": false, 286 | "task": false 287 | } 288 | }, 289 | "source": [ 290 | "## Read numbers until the input is 0 and print their sum." 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": null, 296 | "metadata": { 297 | "deletable": false, 298 | "nbgrader": { 299 | "cell_type": "code", 300 | "checksum": "2ea2b82f65277b5c893f36f521fdd8cb", 301 | "grade": false, 302 | "grade_id": "cell-445648d32d55a5e5", 303 | "locked": false, 304 | "schema_version": 3, 305 | "solution": true, 306 | "task": false 307 | } 308 | }, 309 | "outputs": [], 310 | "source": [ 311 | "# YOUR CODE HERE\n", 312 | "raise NotImplementedError()" 313 | ] 314 | }, 315 | { 316 | "cell_type": "markdown", 317 | "metadata": { 318 | "deletable": false, 319 | "editable": false, 320 | "nbgrader": { 321 | "cell_type": "markdown", 322 | "checksum": "30adc40bb88dc1b5c4e18fa512aeaef7", 323 | "grade": false, 324 | "grade_id": "cell-34aa8d0e2d9961f6", 325 | "locked": true, 326 | "schema_version": 3, 327 | "solution": false, 328 | "task": false 329 | } 330 | }, 331 | "source": [ 332 | "## Read numbers until 0 and print their mean without using a list.\n", 333 | "\n", 334 | "Do not include 0 in the statistics." 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": { 341 | "deletable": false, 342 | "nbgrader": { 343 | "cell_type": "code", 344 | "checksum": "11c76f24cf9a2de1d2f6ba8b589a3b0f", 345 | "grade": false, 346 | "grade_id": "cell-25bc5f05e52e11c5", 347 | "locked": false, 348 | "schema_version": 3, 349 | "solution": true, 350 | "task": false 351 | } 352 | }, 353 | "outputs": [], 354 | "source": [ 355 | "# YOUR CODE HERE\n", 356 | "raise NotImplementedError()" 357 | ] 358 | }, 359 | { 360 | "cell_type": "markdown", 361 | "metadata": { 362 | "deletable": false, 363 | "editable": false, 364 | "nbgrader": { 365 | "cell_type": "markdown", 366 | "checksum": "067179073794fa108f8cb4a148d207b0", 367 | "grade": false, 368 | "grade_id": "cell-22a94bf2db5d2414", 369 | "locked": true, 370 | "schema_version": 3, 371 | "solution": false, 372 | "task": false 373 | } 374 | }, 375 | "source": [ 376 | "## Read numbers until 0 and print their standard deviation without using a list.\n", 377 | "\n", 378 | "Do not include 0 in the statistics.\n", 379 | "\n", 380 | "Standard deviation can be computed without a list. You can find the formula [here](https://en.wikipedia.org/wiki/Standard_deviation#Identities_and_mathematical_properties)." 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": { 387 | "deletable": false, 388 | "nbgrader": { 389 | "cell_type": "code", 390 | "checksum": "9c1b0181b24c0b61df39341c05d4ebec", 391 | "grade": false, 392 | "grade_id": "cell-6d3a243a6151a29d", 393 | "locked": false, 394 | "schema_version": 3, 395 | "solution": true, 396 | "task": false 397 | } 398 | }, 399 | "outputs": [], 400 | "source": [ 401 | "# YOUR CODE HERE\n", 402 | "raise NotImplementedError()" 403 | ] 404 | }, 405 | { 406 | "cell_type": "markdown", 407 | "metadata": { 408 | "collapsed": true, 409 | "deletable": false, 410 | "editable": false, 411 | "nbgrader": { 412 | "cell_type": "markdown", 413 | "checksum": "2be809f6efbee204fcd14f8aaa3f60b7", 414 | "grade": false, 415 | "grade_id": "cell-ef2652a4e08329a7", 416 | "locked": true, 417 | "schema_version": 3, 418 | "solution": false, 419 | "task": false 420 | } 421 | }, 422 | "source": [ 423 | "# Lists\n", 424 | "\n", 425 | "## Print the odd numbers from this list." 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": null, 431 | "metadata": { 432 | "deletable": false, 433 | "editable": false, 434 | "nbgrader": { 435 | "cell_type": "code", 436 | "checksum": "a0020c271939a3a963f63b5a30a3e0ed", 437 | "grade": false, 438 | "grade_id": "cell-f0e76a9ae83db0ba", 439 | "locked": true, 440 | "schema_version": 3, 441 | "solution": false, 442 | "task": false 443 | } 444 | }, 445 | "outputs": [], 446 | "source": [ 447 | "l = [2, 3, -2, -7, 0, 2, 3]" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": null, 453 | "metadata": { 454 | "deletable": false, 455 | "nbgrader": { 456 | "cell_type": "code", 457 | "checksum": "cdc75bc62ef7025674b39f9f1fbbacbe", 458 | "grade": false, 459 | "grade_id": "cell-2d63634cd4835914", 460 | "locked": false, 461 | "schema_version": 3, 462 | "solution": true, 463 | "task": false 464 | } 465 | }, 466 | "outputs": [], 467 | "source": [ 468 | "# YOUR CODE HERE\n", 469 | "raise NotImplementedError()" 470 | ] 471 | }, 472 | { 473 | "cell_type": "markdown", 474 | "metadata": { 475 | "deletable": false, 476 | "editable": false, 477 | "nbgrader": { 478 | "cell_type": "markdown", 479 | "checksum": "09dfa125eee47fad42abdb24ca34d1b0", 480 | "grade": false, 481 | "grade_id": "cell-28a320cf91cd42bc", 482 | "locked": true, 483 | "schema_version": 3, 484 | "solution": false, 485 | "task": false 486 | } 487 | }, 488 | "source": [ 489 | "## Read numbers until 0 and collect them in a list. Print the elements backwards (zero not included).\n", 490 | "\n", 491 | "For example:\n", 492 | "\n", 493 | "```\n", 494 | "input: 3 4 2 12 3 0\n", 495 | "output: 3 12 3 4 3 ```" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": null, 501 | "metadata": { 502 | "deletable": false, 503 | "nbgrader": { 504 | "cell_type": "code", 505 | "checksum": "168ffe6551253b7fc75044b6f0e55be1", 506 | "grade": false, 507 | "grade_id": "cell-89c5428a352b0b90", 508 | "locked": false, 509 | "schema_version": 3, 510 | "solution": true, 511 | "task": false 512 | } 513 | }, 514 | "outputs": [], 515 | "source": [ 516 | "# YOUR CODE HERE\n", 517 | "raise NotImplementedError()" 518 | ] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "metadata": { 523 | "deletable": false, 524 | "editable": false, 525 | "nbgrader": { 526 | "cell_type": "markdown", 527 | "checksum": "bd3bae45e62f2cc48333945f995dfb92", 528 | "grade": false, 529 | "grade_id": "cell-c9afafa54ed38754", 530 | "locked": true, 531 | "schema_version": 3, 532 | "solution": false, 533 | "task": false 534 | } 535 | }, 536 | "source": [ 537 | "## Read a number N and then read exactly N numbers. Print them backwards.\n", 538 | "\n", 539 | "For example:\n", 540 | "\n", 541 | "```\n", 542 | "input: 4 -1 0 2 12\n", 543 | "output: 12 2 0 -1\n", 544 | "```" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "metadata": { 551 | "deletable": false, 552 | "nbgrader": { 553 | "cell_type": "code", 554 | "checksum": "a129ec12adfb7c2c7b84d80337a20029", 555 | "grade": false, 556 | "grade_id": "cell-28fe536393b9cbff", 557 | "locked": false, 558 | "schema_version": 3, 559 | "solution": true, 560 | "task": false 561 | } 562 | }, 563 | "outputs": [], 564 | "source": [ 565 | "# YOUR CODE HERE\n", 566 | "raise NotImplementedError()" 567 | ] 568 | }, 569 | { 570 | "cell_type": "markdown", 571 | "metadata": { 572 | "deletable": false, 573 | "editable": false, 574 | "nbgrader": { 575 | "cell_type": "markdown", 576 | "checksum": "234b32715928aa212ebe928a7641d170", 577 | "grade": false, 578 | "grade_id": "cell-4ebe0c980a01f717", 579 | "locked": true, 580 | "schema_version": 3, 581 | "solution": false, 582 | "task": false 583 | } 584 | }, 585 | "source": [ 586 | "# Functions\n", 587 | "\n", 588 | "The following exercises contain function skeletons. Finish these functions.\n", 589 | "If you work correctly, the assert statements should not fail, however, passing all tests does not guarantee that your solution is correct.\n", 590 | "You are free to add more tests in a new cell. The test cell cannot be modified.\n", 591 | "\n", 592 | "## (2p) Write a function that takes an integer parameter (N) and returns a list of the first N prime numbers starting from 2." 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": null, 598 | "metadata": { 599 | "deletable": false, 600 | "nbgrader": { 601 | "cell_type": "code", 602 | "checksum": "e8045f38a2bb15a18383e871c428fd31", 603 | "grade": false, 604 | "grade_id": "cell-32bfa66ad55808d0", 605 | "locked": false, 606 | "schema_version": 3, 607 | "solution": true, 608 | "task": false 609 | } 610 | }, 611 | "outputs": [], 612 | "source": [ 613 | "# YOUR CODE HERE\n", 614 | "raise NotImplementedError()" 615 | ] 616 | }, 617 | { 618 | "cell_type": "code", 619 | "execution_count": null, 620 | "metadata": { 621 | "deletable": false, 622 | "editable": false, 623 | "nbgrader": { 624 | "cell_type": "code", 625 | "checksum": "27711dbf4111121b1f3f316a4226ced1", 626 | "grade": true, 627 | "grade_id": "cell-1338b4225b785617", 628 | "locked": true, 629 | "points": 2, 630 | "schema_version": 3, 631 | "solution": false, 632 | "task": false 633 | } 634 | }, 635 | "outputs": [], 636 | "source": [ 637 | "assert get_n_primes(3) == [2, 3, 5]\n", 638 | "assert get_n_primes(0) == []" 639 | ] 640 | }, 641 | { 642 | "cell_type": "markdown", 643 | "metadata": { 644 | "deletable": false, 645 | "editable": false, 646 | "nbgrader": { 647 | "cell_type": "markdown", 648 | "checksum": "a8f9d5230016803474527b9b4eda1fac", 649 | "grade": false, 650 | "grade_id": "cell-a169e0c53bfd025c", 651 | "locked": true, 652 | "schema_version": 3, 653 | "solution": false, 654 | "task": false 655 | } 656 | }, 657 | "source": [ 658 | "## (3p) Write a function that takes an integer parameter (N) and returns the Nth Fibonacci number.\n", 659 | "\n", 660 | "The Fibonacci series is defined as:\n", 661 | "\n", 662 | "$$\n", 663 | "F_n = F_{n-1} + F_{n-2} \\text{ for } n>1\\\\\n", 664 | "F_0 = 0\\\\\n", 665 | "F_1 = 1\n", 666 | "$$\n", 667 | "\n", 668 | "The first few elements of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13 etc.\n", 669 | "\n", 670 | "Raise a `ValueError` if $N<0$." 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": null, 676 | "metadata": { 677 | "deletable": false, 678 | "nbgrader": { 679 | "cell_type": "code", 680 | "checksum": "d785ca861663cfa364c0f01dd86c95aa", 681 | "grade": false, 682 | "grade_id": "cell-bed163595b1306c1", 683 | "locked": false, 684 | "schema_version": 3, 685 | "solution": true, 686 | "task": false 687 | } 688 | }, 689 | "outputs": [], 690 | "source": [ 691 | "# YOUR CODE HERE\n", 692 | "raise NotImplementedError()" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": null, 698 | "metadata": { 699 | "deletable": false, 700 | "editable": false, 701 | "nbgrader": { 702 | "cell_type": "code", 703 | "checksum": "fc25a1c72795157c51a97efb9c54f7da", 704 | "grade": true, 705 | "grade_id": "cell-c956085d6aa9b32d", 706 | "locked": true, 707 | "points": 3, 708 | "schema_version": 3, 709 | "solution": false, 710 | "task": false 711 | } 712 | }, 713 | "outputs": [], 714 | "source": [ 715 | "assert get_nth_fibonacci(0) == 0\n", 716 | "assert get_nth_fibonacci(1) == 1\n", 717 | "assert get_nth_fibonacci(2) == 1\n", 718 | "assert get_nth_fibonacci(5) == 5\n", 719 | "assert get_nth_fibonacci(7) == 13\n", 720 | "\n", 721 | "try:\n", 722 | " get_nth_fibonacci(-2)\n", 723 | "except ValueError:\n", 724 | " pass\n", 725 | "else:\n", 726 | " AssertionError(\"TEST FAILS. Negative index should raise a ValueError.\")" 727 | ] 728 | }, 729 | { 730 | "cell_type": "markdown", 731 | "metadata": { 732 | "deletable": false, 733 | "editable": false, 734 | "nbgrader": { 735 | "cell_type": "markdown", 736 | "checksum": "09959e5d70e80a465a539bf9f0ee8a44", 737 | "grade": false, 738 | "grade_id": "cell-9be4a9c459f36ea2", 739 | "locked": true, 740 | "schema_version": 3, 741 | "solution": false, 742 | "task": false 743 | } 744 | }, 745 | "source": [ 746 | "## (2p) Write a function that takes an integer parameter (N) and returns a list of the first N Fibonacci numbers starting from $F_1$ to $F_N$ inclusive.\n", 747 | "\n", 748 | "You are encouraged to reuse previous functions." 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": null, 754 | "metadata": { 755 | "deletable": false, 756 | "nbgrader": { 757 | "cell_type": "code", 758 | "checksum": "1fc8b856c3f6bce598e00e3d42a9b23f", 759 | "grade": false, 760 | "grade_id": "cell-a483efba1cf84902", 761 | "locked": false, 762 | "schema_version": 3, 763 | "solution": true, 764 | "task": false 765 | } 766 | }, 767 | "outputs": [], 768 | "source": [ 769 | "# YOUR CODE HERE\n", 770 | "raise NotImplementedError()" 771 | ] 772 | }, 773 | { 774 | "cell_type": "code", 775 | "execution_count": null, 776 | "metadata": { 777 | "deletable": false, 778 | "editable": false, 779 | "nbgrader": { 780 | "cell_type": "code", 781 | "checksum": "1a500fb0b2f6705ab8200a417d643ac6", 782 | "grade": true, 783 | "grade_id": "cell-70487d81d741784a", 784 | "locked": true, 785 | "points": 2, 786 | "schema_version": 3, 787 | "solution": false, 788 | "task": false 789 | } 790 | }, 791 | "outputs": [], 792 | "source": [ 793 | "assert get_n_fibonacci(4) == [1, 1, 2, 3]\n", 794 | "assert get_n_fibonacci(8) == [1, 1, 2, 3, 5, 8, 13, 21]\n", 795 | "assert get_n_fibonacci(0) == []" 796 | ] 797 | }, 798 | { 799 | "cell_type": "markdown", 800 | "metadata": { 801 | "deletable": false, 802 | "editable": false, 803 | "nbgrader": { 804 | "cell_type": "markdown", 805 | "checksum": "0c15d448d6ed1b0d3e1a28f0f4c0a855", 806 | "grade": false, 807 | "grade_id": "cell-836bd9962fe40746", 808 | "locked": true, 809 | "schema_version": 3, 810 | "solution": false, 811 | "task": false 812 | } 813 | }, 814 | "source": [ 815 | "## (3p) Write a function that takes two integers, A and B and returns B-A Fibonacci numbers starting from the Ath Fibonacci number to the (B-1)th number. See the example below." 816 | ] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "execution_count": null, 821 | "metadata": { 822 | "deletable": false, 823 | "nbgrader": { 824 | "cell_type": "code", 825 | "checksum": "fc1a00d57c3b387f87972ad789b954b5", 826 | "grade": false, 827 | "grade_id": "cell-bdda0121e9b4384e", 828 | "locked": false, 829 | "schema_version": 3, 830 | "solution": true, 831 | "task": false 832 | } 833 | }, 834 | "outputs": [], 835 | "source": [ 836 | "# YOUR CODE HERE\n", 837 | "raise NotImplementedError()" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": null, 843 | "metadata": { 844 | "deletable": false, 845 | "editable": false, 846 | "nbgrader": { 847 | "cell_type": "code", 848 | "checksum": "13edfe0e0829fca627bba208e864810c", 849 | "grade": true, 850 | "grade_id": "cell-009e90c9d285f80e", 851 | "locked": true, 852 | "points": 3, 853 | "schema_version": 3, 854 | "solution": false, 855 | "task": false 856 | } 857 | }, 858 | "outputs": [], 859 | "source": [ 860 | "assert get_range_fibonacci(2, 5) == [1, 2, 3]\n", 861 | "assert get_range_fibonacci(5, 5) == []\n", 862 | "assert get_range_fibonacci(1, 5) == [1, 1, 2, 3]" 863 | ] 864 | }, 865 | { 866 | "cell_type": "markdown", 867 | "metadata": { 868 | "deletable": false, 869 | "editable": false, 870 | "nbgrader": { 871 | "cell_type": "markdown", 872 | "checksum": "f1d63d5cf475fd3ef41614886d969250", 873 | "grade": false, 874 | "grade_id": "cell-1bc996027017d3cc", 875 | "locked": true, 876 | "schema_version": 3, 877 | "solution": false, 878 | "task": false 879 | } 880 | }, 881 | "source": [ 882 | "# Default and keyword arguments\n", 883 | "\n", 884 | "## (2p) Write a function that computes the sum of a list with an optional starting value. If no starting value is provided, it should sum from 0." 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": null, 890 | "metadata": { 891 | "deletable": false, 892 | "nbgrader": { 893 | "cell_type": "code", 894 | "checksum": "af738ac40c5b84ef59b73671690c02d8", 895 | "grade": false, 896 | "grade_id": "cell-33aba3b44cbe7d79", 897 | "locked": false, 898 | "schema_version": 3, 899 | "solution": true, 900 | "task": false 901 | } 902 | }, 903 | "outputs": [], 904 | "source": [ 905 | "# YOUR CODE HERE\n", 906 | "raise NotImplementedError()" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": null, 912 | "metadata": { 913 | "deletable": false, 914 | "editable": false, 915 | "nbgrader": { 916 | "cell_type": "code", 917 | "checksum": "45974c56060cd4551e71b5fd54327f49", 918 | "grade": true, 919 | "grade_id": "cell-11357ac08ed33343", 920 | "locked": true, 921 | "points": 2, 922 | "schema_version": 3, 923 | "solution": false, 924 | "task": false 925 | } 926 | }, 927 | "outputs": [], 928 | "source": [ 929 | "assert sum_list([1, 2, 3]) == 6\n", 930 | "assert sum_list([1, 2, 3], 5) == 11\n", 931 | "assert sum_list([1, 2, 3, 5, 11, 3213], 5) == 3240" 932 | ] 933 | }, 934 | { 935 | "cell_type": "markdown", 936 | "metadata": { 937 | "deletable": false, 938 | "editable": false, 939 | "nbgrader": { 940 | "cell_type": "markdown", 941 | "checksum": "ed66a9fcc505265ac570181d350081e7", 942 | "grade": false, 943 | "grade_id": "cell-793265e8620872dc", 944 | "locked": true, 945 | "schema_version": 3, 946 | "solution": false, 947 | "task": false 948 | } 949 | }, 950 | "source": [ 951 | "## (3p) Write a function that takes two numbers and an arithmetic operator as a string (\"+\", \"-\", \"\\*\" or \"/\") and returns the result of the arithmetic operation on the two numbers. The default operation should be addition." 952 | ] 953 | }, 954 | { 955 | "cell_type": "code", 956 | "execution_count": null, 957 | "metadata": { 958 | "deletable": false, 959 | "nbgrader": { 960 | "cell_type": "code", 961 | "checksum": "5bc9884c50601be8afbda80595e0e8f5", 962 | "grade": false, 963 | "grade_id": "cell-f2dad9c42958066e", 964 | "locked": false, 965 | "schema_version": 3, 966 | "solution": true, 967 | "task": false 968 | } 969 | }, 970 | "outputs": [], 971 | "source": [ 972 | "# YOUR CODE HERE\n", 973 | "raise NotImplementedError()" 974 | ] 975 | }, 976 | { 977 | "cell_type": "code", 978 | "execution_count": null, 979 | "metadata": { 980 | "deletable": false, 981 | "editable": false, 982 | "nbgrader": { 983 | "cell_type": "code", 984 | "checksum": "7c50161f861099aa52aad2ef8f393b53", 985 | "grade": true, 986 | "grade_id": "cell-7ed54437386acf59", 987 | "locked": true, 988 | "points": 3, 989 | "schema_version": 3, 990 | "solution": false, 991 | "task": false 992 | } 993 | }, 994 | "outputs": [], 995 | "source": [ 996 | "assert arithmetic(2, 3) == 5\n", 997 | "assert arithmetic(2, 3, \"+\") == 5\n", 998 | "assert arithmetic(2, 3, \"-\") == -1\n", 999 | "assert arithmetic(2, 3, \"*\") == 6\n", 1000 | "assert arithmetic(2, 3, \"/\") == 2 / 3" 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "markdown", 1005 | "metadata": { 1006 | "deletable": false, 1007 | "editable": false, 1008 | "nbgrader": { 1009 | "cell_type": "markdown", 1010 | "checksum": "eb620a17fa47cfe02fcf02b90fbb0fb1", 1011 | "grade": false, 1012 | "grade_id": "cell-2ed6e73322e6ed6a", 1013 | "locked": true, 1014 | "schema_version": 3, 1015 | "solution": false, 1016 | "task": false 1017 | } 1018 | }, 1019 | "source": [ 1020 | "## (3p) Write a function that takes three parameters. The third is a callable (function) that takes two parameters. The function should call its third parameter with the first two as parameters. If the third parameter is not specified the function should add the two arguments.\n", 1021 | "\n", 1022 | "This is very similar to the previous function but the third parameter is a function instead of a string." 1023 | ] 1024 | }, 1025 | { 1026 | "cell_type": "code", 1027 | "execution_count": null, 1028 | "metadata": { 1029 | "deletable": false, 1030 | "nbgrader": { 1031 | "cell_type": "code", 1032 | "checksum": "e01cd3b14000b861507808416f95b19f", 1033 | "grade": false, 1034 | "grade_id": "cell-3d76a413c7125655", 1035 | "locked": false, 1036 | "schema_version": 3, 1037 | "solution": true, 1038 | "task": false 1039 | } 1040 | }, 1041 | "outputs": [], 1042 | "source": [ 1043 | "# YOUR CODE HERE\n", 1044 | "raise NotImplementedError()" 1045 | ] 1046 | }, 1047 | { 1048 | "cell_type": "code", 1049 | "execution_count": null, 1050 | "metadata": { 1051 | "deletable": false, 1052 | "editable": false, 1053 | "nbgrader": { 1054 | "cell_type": "code", 1055 | "checksum": "a7aedce2dcf4b1ca4a0d48702f82fe83", 1056 | "grade": true, 1057 | "grade_id": "cell-c77dfa723cea60a1", 1058 | "locked": true, 1059 | "points": 3, 1060 | "schema_version": 3, 1061 | "solution": false, 1062 | "task": false 1063 | } 1064 | }, 1065 | "outputs": [], 1066 | "source": [ 1067 | "assert call_func(3, 4, product) == 12\n", 1068 | "assert call_func(\"foo\", \"bar\") == \"foobar\"" 1069 | ] 1070 | }, 1071 | { 1072 | "cell_type": "markdown", 1073 | "metadata": { 1074 | "deletable": false, 1075 | "editable": false, 1076 | "nbgrader": { 1077 | "cell_type": "markdown", 1078 | "checksum": "136d91bd3a2853d7fc5c9f81a0a1608b", 1079 | "grade": false, 1080 | "grade_id": "cell-afaa10d520788225", 1081 | "locked": true, 1082 | "schema_version": 3, 1083 | "solution": false, 1084 | "task": false 1085 | } 1086 | }, 1087 | "source": [ 1088 | "# Advanced exercises" 1089 | ] 1090 | }, 1091 | { 1092 | "cell_type": "markdown", 1093 | "metadata": { 1094 | "deletable": false, 1095 | "editable": false, 1096 | "nbgrader": { 1097 | "cell_type": "markdown", 1098 | "checksum": "067889b2376da79ce982636287c1e08a", 1099 | "grade": false, 1100 | "grade_id": "cell-99a1e44d923dd0ea", 1101 | "locked": true, 1102 | "schema_version": 3, 1103 | "solution": false, 1104 | "task": false 1105 | } 1106 | }, 1107 | "source": [ 1108 | "## (3p) Create a function that take a list and a predicate (function with boolean return value) as parameters and returns a new list of those elements which the predicate return True.\n", 1109 | "\n", 1110 | "A predicate is a function that takes one element and return `True` or `False`, for example `is_even`, `is_prime`.\n", 1111 | "You need to implement the predicate functions in the tests and you are encouraged to add additional predicates and tests.\n", 1112 | "\n", 1113 | "If you implemented The following tests should run." 1114 | ] 1115 | }, 1116 | { 1117 | "cell_type": "code", 1118 | "execution_count": null, 1119 | "metadata": { 1120 | "deletable": false, 1121 | "nbgrader": { 1122 | "cell_type": "code", 1123 | "checksum": "81c8d876d1386aa7da2819c5f538463f", 1124 | "grade": false, 1125 | "grade_id": "cell-244a0447cef16213", 1126 | "locked": false, 1127 | "schema_version": 3, 1128 | "solution": true, 1129 | "task": false 1130 | } 1131 | }, 1132 | "outputs": [], 1133 | "source": [ 1134 | "# YOUR CODE HERE\n", 1135 | "raise NotImplementedError()" 1136 | ] 1137 | }, 1138 | { 1139 | "cell_type": "code", 1140 | "execution_count": null, 1141 | "metadata": { 1142 | "deletable": false, 1143 | "editable": false, 1144 | "nbgrader": { 1145 | "cell_type": "code", 1146 | "checksum": "ecd5f0f1353cd1155d70d4997076f511", 1147 | "grade": true, 1148 | "grade_id": "cell-07ddbdcd2afcdf4c", 1149 | "locked": true, 1150 | "points": 3, 1151 | "schema_version": 3, 1152 | "solution": false, 1153 | "task": false 1154 | } 1155 | }, 1156 | "outputs": [], 1157 | "source": [ 1158 | "l1 = [1, 2, 3, 4, 19, 35, 11]\n", 1159 | "l2 = [1, 4, 9, 16, 25]\n", 1160 | "assert filter_list(l1, is_odd) == [1, 3, 19, 35, 11]\n", 1161 | "assert filter_list(l1, is_prime) == [2, 3, 19, 11]\n", 1162 | "assert filter_list(l2, is_prime) == []" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "## (3p) Reduce is a function that applies a two argument function against an accumulator and each element in the sequence (from left to right) to reduce it to a single value. If no initial value is provided, the accumulator is initialized with the return value of the function run on the first two elements of the sequence.\n", 1170 | "\n", 1171 | "```\n", 1172 | "reduce([1, 2, 3], product) ---> 6\n", 1173 | "reduce([1, 2, 3], product, accumulator=10) ---> 60\n", 1174 | "reduce([\"foo\", \"bar\"], string_addition) ---> \"foobar\"\n", 1175 | "reduce([\"foo\", \"bar\"], string_addition, accumulator=\"hello\") ---> \"hellofoobar\"\n", 1176 | "```" 1177 | ] 1178 | }, 1179 | { 1180 | "cell_type": "code", 1181 | "execution_count": null, 1182 | "metadata": { 1183 | "deletable": false, 1184 | "nbgrader": { 1185 | "cell_type": "code", 1186 | "checksum": "b86928a49f19ee38292ec5bef6c59933", 1187 | "grade": false, 1188 | "grade_id": "cell-6fb5723159b26749", 1189 | "locked": false, 1190 | "schema_version": 3, 1191 | "solution": true, 1192 | "task": false 1193 | } 1194 | }, 1195 | "outputs": [], 1196 | "source": [ 1197 | "# YOUR CODE HERE\n", 1198 | "raise NotImplementedError()" 1199 | ] 1200 | }, 1201 | { 1202 | "cell_type": "code", 1203 | "execution_count": null, 1204 | "metadata": { 1205 | "deletable": false, 1206 | "editable": false, 1207 | "nbgrader": { 1208 | "cell_type": "code", 1209 | "checksum": "7c5d09e435b16f5279fa4bad5fbf84f7", 1210 | "grade": true, 1211 | "grade_id": "cell-c43e405f72ada059", 1212 | "locked": true, 1213 | "points": 3, 1214 | "schema_version": 3, 1215 | "solution": false, 1216 | "task": false 1217 | } 1218 | }, 1219 | "outputs": [], 1220 | "source": [ 1221 | "l1 = [1, 2, -1, 5]\n", 1222 | "l2 = [\"foo\", \"bar\", \"hello\"]\n", 1223 | "\n", 1224 | "assert reduce(l1, add) == 7\n", 1225 | "assert reduce(l1, add, 10) == 17\n", 1226 | "assert reduce(l2, string_len_add, 0) == 11" 1227 | ] 1228 | }, 1229 | { 1230 | "cell_type": "markdown", 1231 | "metadata": { 1232 | "deletable": false, 1233 | "editable": false, 1234 | "nbgrader": { 1235 | "cell_type": "markdown", 1236 | "checksum": "2a62772ef005280e1e6e1dbef24b50de", 1237 | "grade": false, 1238 | "grade_id": "cell-4162c6de3504dae6", 1239 | "locked": true, 1240 | "schema_version": 3, 1241 | "solution": false, 1242 | "task": false 1243 | } 1244 | }, 1245 | "source": [ 1246 | "## (1p) Use your reduce function for the following operations:\n", 1247 | "\n", 1248 | "1. count the number of odd elements in a list of integers,\n", 1249 | "2. find the maximum of a list of integers,\n", 1250 | "3. find the longest string in list of strings." 1251 | ] 1252 | }, 1253 | { 1254 | "cell_type": "code", 1255 | "execution_count": null, 1256 | "metadata": { 1257 | "deletable": false, 1258 | "nbgrader": { 1259 | "cell_type": "code", 1260 | "checksum": "fa4cb9e0cccfb01cb3eca64f7129929b", 1261 | "grade": true, 1262 | "grade_id": "cell-1554306956c531d6", 1263 | "locked": false, 1264 | "points": 0, 1265 | "schema_version": 3, 1266 | "solution": true, 1267 | "task": false 1268 | } 1269 | }, 1270 | "outputs": [], 1271 | "source": [ 1272 | "# YOUR CODE HERE\n", 1273 | "raise NotImplementedError()" 1274 | ] 1275 | }, 1276 | { 1277 | "cell_type": "code", 1278 | "execution_count": null, 1279 | "metadata": { 1280 | "deletable": false, 1281 | "editable": false, 1282 | "nbgrader": { 1283 | "cell_type": "code", 1284 | "checksum": "3588d504aa4f8b92b07b6f462bc95386", 1285 | "grade": true, 1286 | "grade_id": "cell-959dbab0fb50d84d", 1287 | "locked": true, 1288 | "points": 0, 1289 | "schema_version": 3, 1290 | "solution": false, 1291 | "task": false 1292 | } 1293 | }, 1294 | "outputs": [], 1295 | "source": [ 1296 | "test_list = [1, 2, 3, 4, 5, 7, 9, 11, 16]\n", 1297 | "test_string_list = [\n", 1298 | " \"abc\",\n", 1299 | " \"casdsa\",\n", 1300 | " \"adasdefasf\",\n", 1301 | " \"adhaetjareh\",\n", 1302 | " \"This is a very long string, so hopefully my function will return this.\",\n", 1303 | "]\n", 1304 | "\n", 1305 | "# 1\n", 1306 | "assert reduce(test_list, num_odd_elements) == 6\n", 1307 | "# 2\n", 1308 | "assert reduce(test_list, max) == 16\n", 1309 | "# 3\n", 1310 | "assert (\n", 1311 | " reduce(test_string_list, get_longest)\n", 1312 | " == \"This is a very long string, so hopefully my function will return this.\"\n", 1313 | ")" 1314 | ] 1315 | }, 1316 | { 1317 | "cell_type": "markdown", 1318 | "metadata": { 1319 | "deletable": false, 1320 | "editable": false, 1321 | "nbgrader": { 1322 | "cell_type": "markdown", 1323 | "checksum": "b1521c312f0e87507efecb5d2e5b068e", 1324 | "grade": false, 1325 | "grade_id": "cell-8e8d5e94389c08d7", 1326 | "locked": true, 1327 | "schema_version": 3, 1328 | "solution": false, 1329 | "task": false 1330 | } 1331 | }, 1332 | "source": [ 1333 | "## Implement qsort. Qsort sorts a list in place using the quicksort algorithm." 1334 | ] 1335 | }, 1336 | { 1337 | "cell_type": "code", 1338 | "execution_count": null, 1339 | "metadata": { 1340 | "deletable": false, 1341 | "nbgrader": { 1342 | "cell_type": "code", 1343 | "checksum": "e25eaa6121c22396f9d97500441735ee", 1344 | "grade": false, 1345 | "grade_id": "cell-a9a76c9ca712b9ee", 1346 | "locked": false, 1347 | "schema_version": 3, 1348 | "solution": true, 1349 | "task": false 1350 | } 1351 | }, 1352 | "outputs": [], 1353 | "source": [ 1354 | "# YOUR CODE HERE\n", 1355 | "raise NotImplementedError()" 1356 | ] 1357 | }, 1358 | { 1359 | "cell_type": "code", 1360 | "execution_count": null, 1361 | "metadata": { 1362 | "deletable": false, 1363 | "editable": false, 1364 | "nbgrader": { 1365 | "cell_type": "code", 1366 | "checksum": "82fc284291a1a964367d6cd9c796f0bf", 1367 | "grade": true, 1368 | "grade_id": "cell-034cb73519ff059b", 1369 | "locked": true, 1370 | "points": 0, 1371 | "schema_version": 3, 1372 | "solution": false, 1373 | "task": false 1374 | } 1375 | }, 1376 | "outputs": [], 1377 | "source": [ 1378 | "l1 = [10, -1, 2, 11, 0]\n", 1379 | "l2 = [5, 4, 3, 1, 2]\n", 1380 | "qsort(l1)\n", 1381 | "qsort(l2)\n", 1382 | "assert l1 == [-1, 0, 2, 10, 11]\n", 1383 | "assert l2 == [1, 2, 3, 4, 5]" 1384 | ] 1385 | }, 1386 | { 1387 | "cell_type": "markdown", 1388 | "metadata": { 1389 | "deletable": false, 1390 | "editable": false, 1391 | "nbgrader": { 1392 | "cell_type": "markdown", 1393 | "checksum": "4e8e3b4dc7a0928a6201501c00a497fe", 1394 | "grade": false, 1395 | "grade_id": "cell-7df176aff71be026", 1396 | "locked": true, 1397 | "schema_version": 3, 1398 | "solution": false, 1399 | "task": false 1400 | } 1401 | }, 1402 | "source": [ 1403 | "## Implement in-place bubble sort.\n", 1404 | "\n", 1405 | "In-place means that the function changes the list it gets as an argument instead of creating a new list." 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": null, 1411 | "metadata": { 1412 | "deletable": false, 1413 | "nbgrader": { 1414 | "cell_type": "code", 1415 | "checksum": "052163fe9b9d6e39dca81ba1bae3b6b3", 1416 | "grade": false, 1417 | "grade_id": "cell-40c165b7b3725cc9", 1418 | "locked": false, 1419 | "schema_version": 3, 1420 | "solution": true, 1421 | "task": false 1422 | } 1423 | }, 1424 | "outputs": [], 1425 | "source": [ 1426 | "# YOUR CODE HERE\n", 1427 | "raise NotImplementedError()" 1428 | ] 1429 | }, 1430 | { 1431 | "cell_type": "code", 1432 | "execution_count": null, 1433 | "metadata": { 1434 | "deletable": false, 1435 | "editable": false, 1436 | "nbgrader": { 1437 | "cell_type": "code", 1438 | "checksum": "63e6dcdc56ecc9599d59604906294279", 1439 | "grade": true, 1440 | "grade_id": "cell-1de3197545574d72", 1441 | "locked": true, 1442 | "points": 0, 1443 | "schema_version": 3, 1444 | "solution": false, 1445 | "task": false 1446 | } 1447 | }, 1448 | "outputs": [], 1449 | "source": [ 1450 | "l1 = [10, -1, 2, 11, 0]\n", 1451 | "l2 = [5, 4, 3, 1, 2]\n", 1452 | "bubblesort(l1)\n", 1453 | "bubblesort(l2)\n", 1454 | "assert l1 == [-1, 0, 2, 10, 11]\n", 1455 | "assert l2 == [1, 2, 3, 4, 5]" 1456 | ] 1457 | } 1458 | ], 1459 | "metadata": { 1460 | "kernelspec": { 1461 | "display_name": "Python 3 (ipykernel)", 1462 | "language": "python", 1463 | "name": "python3" 1464 | }, 1465 | "language_info": { 1466 | "codemirror_mode": { 1467 | "name": "ipython", 1468 | "version": 3 1469 | }, 1470 | "file_extension": ".py", 1471 | "mimetype": "text/x-python", 1472 | "name": "python", 1473 | "nbconvert_exporter": "python", 1474 | "pygments_lexer": "ipython3", 1475 | "version": "3.8.5" 1476 | }, 1477 | "toc": { 1478 | "base_numbering": 1, 1479 | "nav_menu": { 1480 | "height": "12px", 1481 | "width": "252px" 1482 | }, 1483 | "number_sections": true, 1484 | "sideBar": true, 1485 | "skip_h1_title": false, 1486 | "title_cell": "Table of Contents", 1487 | "title_sidebar": "Contents", 1488 | "toc_cell": false, 1489 | "toc_position": { 1490 | "height": "calc(100% - 180px)", 1491 | "left": "10px", 1492 | "top": "150px", 1493 | "width": "463px" 1494 | }, 1495 | "toc_section_display": "block", 1496 | "toc_window_display": true 1497 | } 1498 | }, 1499 | "nbformat": 4, 1500 | "nbformat_minor": 2 1501 | } 1502 | --------------------------------------------------------------------------------