Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions Exams/Final/Final.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Final Exam\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/blob/master/Exams/Final/Final.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall the drawing system from lecture 18:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Canvas:\n",
" def __init__(self, width, height):\n",
" self.width = width\n",
" self.height = height\n",
" self.data = [[' '] * width for i in range(height)]\n",
"\n",
" def set_pixel(self, row, col, char='*'):\n",
" self.data[row][col] = char\n",
"\n",
" def get_pixel(self, row, col):\n",
" return self.data[row][col]\n",
" \n",
" def h_line(self, x, y, w, **kargs):\n",
" for i in range(x,x+w):\n",
" self.set_pixel(i,y, **kargs)\n",
"\n",
" def v_line(self, x, y, h, **kargs):\n",
" for i in range(y,y+h):\n",
" self.set_pixel(x,i, **kargs)\n",
" \n",
" def line(self, x1, y1, x2, y2, **kargs):\n",
" slope = (y2-y1) / (x2-x1)\n",
" for y in range(y1,y2):\n",
" x= int(slope * y)\n",
" self.set_pixel(x,y, **kargs)\n",
" \n",
" def display(self):\n",
" print(\"\\n\".join([\"\".join(row) for row in self.data]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Shape:\n",
" def __init__(self, name=\"\", **kwargs):\n",
" self.name=name\n",
" self.kwargs=kwargs\n",
" \n",
" def paint(self, canvas): pass\n",
"\n",
"class Rectangle(Shape):\n",
" def __init__(self, x, y, w, h, **kwargs):\n",
" Shape.__init__(self, **kwargs)\n",
" self.x = x\n",
" self.y = y\n",
" self.w = w\n",
" self.h = h\n",
"\n",
" def paint(self, canvas):\n",
" canvas.h_line(self.x, self.y, self.w, **self.kwargs)\n",
" canvas.h_line(self.x, self.y + self.h, self.w, **self.kwargs)\n",
" canvas.v_line(self.x, self.y, self.h, **self.kwargs)\n",
" canvas.v_line(self.x + self.w, self.y, self.h, **self.kwargs)\n",
"\n",
"class Square(Rectangle):\n",
" def __init__(self, x, y, size, **kwargs):\n",
" Rectangle.__init__(self, x, y, size, size, **kwargs)\n",
"\n",
"class Line(Shape):\n",
" def __init__(self, x1, y1, x2, y2, **kwargs):\n",
" Shape.__init__(self, **kwargs)\n",
" self.x1=x1\n",
" self.y1=y1\n",
" self.x2=x2\n",
" self.y2=y2\n",
" \n",
" def paint(self, canvas):\n",
" canvas.line(self.x1,self.y1,self.x2,self.y2)\n",
" \n",
"class CompoundShape(Shape):\n",
" def __init__(self, shapes):\n",
" self.shapes = shapes\n",
"\n",
" def paint(self, canvas):\n",
" for s in self.shapes:\n",
" s.paint(canvas)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class RasterDrawing:\n",
" def __init__(self):\n",
" self.shapes=dict()\n",
" self.shape_names=list()\n",
" \n",
" def add_shape(self,shape):\n",
" if shape.name == \"\":\n",
" shape.name = self.assign_name()\n",
" \n",
" self.shapes[shape.name]=shape\n",
" self.shape_names.append(shape.name)\n",
" \n",
" def paint(self,canvas):\n",
" for shape_name in self.shape_names:\n",
" self.shapes[shape_name].paint(canvas)\n",
" \n",
" def assign_name(self):\n",
" name_base=\"shape\"\n",
" name = name_base+\"_0\"\n",
" \n",
" i=1\n",
" while name in self.shapes:\n",
" name = name_base+\"_\"+str(i)\n",
" \n",
" return name\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Add `Point` and `Triangle` classes and test them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Add an `Arc` class that is instantiated with a center location, two axis lengths, and starting and ending angles. If start and end are not specified or are the same angle, the `Arc` instance should draw an oval. If in addition the two axes are the same, the `Arc` instance should draw a circle. Create `Oval` and `Circle` classes that inherit from `Arc`. Test everything."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Use your classes to create a `RasterDrawing` that draws a happy face."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Add to the `Shape` base class a `__str__()` method. Overwrite the method in each shape to generate a string of the python code necessary to reinstantiate the object. For example, for a rectangle originally instantiated using `Square(5,5,20,char=\"^\")`, `__str__()` should return the string `'Square(5,5,20,char=\"^\")'`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Add to `RasterDrawing` two functions, `save(filename)` and `load(filename)`. The save function writes the `__str__()` of all of the shapes in the drawing to a file (one shape per line). The load function, reads the file, and instantiates each object using the python `eval(expression)` function, and adds each shape to the drawing, thereby recreating a \"saved\" raster drawing. Use this functionality to save and load your happy face.\n",
"\n",
" `eval` takes a string that contains a fragment of a python code and executes it. Consider the following examples: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eval(\"print('Hello')\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = eval('1+2')\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading