Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a4d7b4f
Add files via upload
jselvarathinam Feb 7, 2020
71ce37d
Add files via upload
jselvarathinam Feb 14, 2020
ad7bac9
Merge pull request #1 from afarbin/master
jselvarathinam Feb 14, 2020
720b63f
Add files via upload
jselvarathinam Feb 21, 2020
573f7f8
Merge pull request #2 from afarbin/master
jselvarathinam Feb 21, 2020
754e83e
Add files via upload
jselvarathinam Feb 28, 2020
07ac281
Add files via upload
jselvarathinam Feb 28, 2020
f19e7e0
Add files via upload
jselvarathinam Feb 28, 2020
04081b6
Merge pull request #3 from afarbin/master
jselvarathinam Mar 4, 2020
3a8bbce
Merge pull request #4 from afarbin/master
jselvarathinam Mar 23, 2020
12b97fe
Merge pull request #5 from afarbin/master
jselvarathinam Mar 25, 2020
f9f20d6
Merge pull request #6 from afarbin/master
jselvarathinam Apr 1, 2020
96d26fb
Merge pull request #7 from afarbin/master
jselvarathinam Apr 1, 2020
4a079af
Add files via upload
jselvarathinam Apr 3, 2020
f79aed0
Merge pull request #8 from afarbin/master
jselvarathinam Apr 6, 2020
0326739
Merge pull request #9 from afarbin/master
jselvarathinam Apr 15, 2020
7803da9
Merge pull request #10 from afarbin/master
jselvarathinam Apr 17, 2020
e5a287c
Merge pull request #11 from afarbin/master
jselvarathinam Apr 20, 2020
d784881
Merge pull request #12 from afarbin/master
jselvarathinam Apr 27, 2020
726979d
Merge pull request #13 from afarbin/master
jselvarathinam May 3, 2020
7a73731
Merge pull request #14 from afarbin/master
jselvarathinam May 4, 2020
30ac21b
Merge pull request #15 from afarbin/master
jselvarathinam May 11, 2020
285ede1
Add files via upload
jselvarathinam May 11, 2020
9da3702
Add files via upload
jselvarathinam May 14, 2020
422ad5a
Add files via upload
jselvarathinam May 17, 2020
f97858c
Add files via upload
jselvarathinam May 18, 2020
5ff9564
Add files via upload
jselvarathinam May 18, 2020
bd81889
Add files via upload
jselvarathinam May 18, 2020
5bb740e
Add files via upload
jselvarathinam May 19, 2020
037ea77
Add files via upload
jselvarathinam May 19, 2020
e13fb26
Second draft
jselvarathinam Nov 15, 2022
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
1,198 changes: 1,198 additions & 0 deletions CSE5334_Assignment_2.ipynb

Large diffs are not rendered by default.

365 changes: 365 additions & 0 deletions Exams/Final/Final_Solutions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "qm0nk_1hN7Zl"
},
"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": {
"colab_type": "text",
"id": "rvH8KWunN7Zm"
},
"source": [
"Recall the drawing system from lecture 18:"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "yzlcOrnZN7Zn"
},
"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": 65,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "WqdcjvcYN7Zq"
},
"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": 66,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "Vpp5-Un9N7Zs"
},
"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": {
"colab_type": "text",
"id": "xkDeheLTN7Zu"
},
"source": [
"1. Add `Point` and `Triangle` classes and test them."
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "9C7qeFbQN7Zv"
},
"outputs": [],
"source": [
"class Point(Shape):\n",
" def __init__(self, x, y, **kwargs):\n",
" Shape.__init__(self, **kwargs)\n",
" self.x = x\n",
" self.y = y\n",
" \n",
" def paint(self, canvas):\n",
" canvas.set_pixel(self, self.x, self.y, char='*')\n",
" \n",
" \n",
"class Triangle(Shape): pass"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "set_pixel() got multiple values for argument 'char'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-71-e3ababb41bd9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m50\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mPoint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpaint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisplay\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-67-7334fd61e9fb>\u001b[0m in \u001b[0;36mpaint\u001b[1;34m(self, canvas)\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mpaint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mcanvas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_pixel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mchar\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'*'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[0mcanvas\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisplay\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: set_pixel() got multiple values for argument 'char'"
]
}
],
"source": [
"# Testing the class point\n",
"c = canvas(50,50)\n",
"p = Point(1,2)\n",
"p.paint(c)\n",
"c.display()"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"# Testing the class triangle\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "_MZ4UWHVN7Zx"
},
"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": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "0GafNXM7N7Zx"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "uzRUUYbEN7Zz"
},
"source": [
"3. Use your classes to create a `RasterDrawing` that draws a happy face."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "H029ajDYN7Z0"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "dA2chW3IN7Z2"
},
"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": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "uxKISf_DN7Z2"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "QNi6xqN8N7Z5"
},
"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": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "uE6CAyzoN7Z6"
},
"outputs": [],
"source": [
"eval(\"print('Hello')\")"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "bjBPl6tiN7Z8"
},
"outputs": [],
"source": [
"x = eval('1+2')\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "UcZHstflN7Z9"
},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"name": "Final.ipynb",
"provenance": []
},
"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"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading