{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5. Exercise solutions\n",
"\n",
"## Exericse 1"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# The original 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": [],
"source": [
"# The new class\n",
"class Triangle(Triangle):\n",
" \n",
" def __mul__(self, value):\n",
" return Triangle(self.height*value, self.width*value)\n",
" \n",
" def __lt__(self, other):\n",
" return self.area < other.area\n",
" \n",
" def __repr__(self):\n",
" return f'{self.__class__.__name__}(height={self.height}, width={self.width})' "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Triangle(height=5, width=5)\n",
"Triangle(height=10, width=10)\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# initialise instances\n",
"tri1 = Triangle(5, 5)\n",
"tri2 = Triangle(5, 8)\n",
"print(tri1)\n",
"# scale triangle by multiplication\n",
"tri1 *= 2 # same as tri1 = tri1*2\n",
"print(tri1)\n",
"# compare triangles\n",
"tri1 < tri2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exericse 2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"class Point:\n",
"\n",
" def __init__(self, x, y):\n",
" self._x = x\n",
" self._y = y\n",
" \n",
" def distance_to(self, other):\n",
" '''Return the distance to another point instance.'''\n",
" x0, y0 = self._x, self._y\n",
" x1, y1 = other._x, other._y\n",
" return self.distance(x0, y0, x1, y1)\n",
" \n",
" @staticmethod\n",
" def distance(x0, y0, x1, y1):\n",
" return math.sqrt( (x1-x0)**2 + (y1-y0)**2 )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"class Polyline:\n",
"\n",
" def __init__(self, points):\n",
" self.points = points\n",
" \n",
" def __len__(self):\n",
" return len(self.points)-1\n",
" \n",
" def get_total_length(self):\n",
" length = 0\n",
" for point0, point1 in zip(self.points, self.points[1:]):\n",
" length += point0.distance_to(point1)\n",
" return length"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"pt1 = Point(0,0)\n",
"pt2 = Point(0,5)\n",
"pt3 = Point(5,5)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number of segments: 2\n",
"total length: 10.0\n"
]
}
],
"source": [
"poly = Polyline([pt1, pt2, pt3])\n",
"print('number of segments:', len(poly))\n",
"print('total length:', poly.get_total_length())"
]
},
{
"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.4"
},
"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
}