{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Exercise solutions\n", "\n", "## Exericse 1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# define Triangle class\n", "class Triangle:\n", " \n", " def __init__(self, height, width):\n", " self.height = height\n", " self.width = width\n", "\n", " @property\n", " def area(self):\n", " return 0.5 * self.height * self.width" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# initialise instances\n", "tri1 = Triangle(5, 5)\n", "tri2 = Triangle(5, 8)\n", "# compare areas\n", "tri1.area < tri2.area" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exericse 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.02262031409510108\n", "1.9613063809417997\n", "3.5747228774418245\n", "5.328844861280842\n", "5.535032025900612\n", "6.543706546566132\n", "10.914336423463437\n", "11.597341952659397\n", "12.142288392240784\n", "18.616891800445224\n" ] } ], "source": [ "from random import random\n", "# initialise list of random triangles\n", "triangles = [Triangle(10*random(), 10*random()) for i in range(10)]\n", "# sorting inplace\n", "triangles.sort(key = lambda x: x.area)\n", "# print the sorted triangles and corresponding areas\n", "for tri in triangles:\n", " print( tri.area )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3-1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# define Counter class\n", "class Counter:\n", " def __init__(self, start=0):\n", " self.__value = start\n", "\n", " def count_up(self):\n", " self.__value += 1\n", "\n", " def count_down(self):\n", " self.__value -= 1\n", "\n", " def get_value(self):\n", " return self.__value" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# initialise counter instance and take it for a spin\n", "counter = Counter()\n", "counter.count_up()\n", "counter.count_up()\n", "counter.count_up()\n", "counter.count_down()\n", "counter.get_value()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3-2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# define Counter class\n", "class Counter:\n", " def __init__(self, start=0):\n", " self.__value = start\n", "\n", " def count_up(self):\n", " self.__value += 1\n", "\n", " def count_down(self):\n", " self.__value -= 1\n", "\n", " def get_value(self):\n", " return self.__value\n", " \n", " def count_up_by(self, step):\n", " self.__value += step" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# initialise counter instance and take it for a spin\n", "counter = Counter()\n", "counter.count_up()\n", "counter.count_up_by(5)\n", "counter.count_down()\n", "counter.get_value()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# End of exercises\n", "\n", "*The cell below is for setting the style of this document. It's not part of the exercises.*" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Apply css theme to notebook\n", "from IPython.display import HTML\n", "HTML(''.format(open('../css/cowi.css').read()))" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Table of Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "165px" }, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }