diff --git a/Exams/Final/Final_answers.ipynb b/Exams/Final/Final_answers.ipynb new file mode 100644 index 0000000..b24511a --- /dev/null +++ b/Exams/Final/Final_answers.ipynb @@ -0,0 +1,518 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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" + }, + "colab": { + "name": "Final_answers.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "OLHvh6r3k-pV", + "colab_type": "text" + }, + "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": { + "id": "fMVtgvxak-pW", + "colab_type": "text" + }, + "source": [ + "Recall the drawing system from lecture 18:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eWWn5VRCk-pY", + "colab_type": "code", + "colab": {} + }, + "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]))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ccq2Pbgwk-pb", + "colab_type": "code", + "colab": {} + }, + "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", + " def __str__(self):\n", + " print(\"Rectangle(\"+str(self.x)+','+str(self.y)+','+str(self.w)+','+str(self.h)+')')\n", + "class Square(Rectangle):\n", + " def __init__(self, x, y, size, **kwargs):\n", + " Rectangle.__init__(self, x, y, size, size, **kwargs)\n", + " \n", + " def __str__(self):\n", + " print(\"Square(\"+str(self.x)+','+str(self.y)+','+str(self.size)+','+str(self.size)+')')\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)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "qNo3ypgXk-pe", + "colab_type": "code", + "colab": {} + }, + "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" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fb-4H0jck-pk", + "colab_type": "text" + }, + "source": [ + "1. Add `Point` and `Triangle` classes and test them." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YwBoE94jk-pk", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class Triangle(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", + " def paint(self,canvas):\n", + " canvas.h_line(self.x, self.y, self.w, **self.kwargs)\n", + " canvas.v_line(self.x + self.w, self.y, self.h, **self.kwargs)\n", + " \n", + " def __str__(self):\n", + " print(\"Triangle(\"+str(self.x)+','+str(self.y)+','+str(self.w)+','+str(self.h)+')')\n", + "\n", + "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.x,self.y,**self.kwargs)\n", + " \n", + " def __str__(self):\n", + " print(\"Point(\"+str(self.x)+','+str(self.y)+')')" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "X0xOXS40KzSN", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 697 + }, + "outputId": "16fa8e57-4b0d-400d-e61b-ebf2de634882" + }, + "source": [ + "c1=Canvas(50,40)\n", + "s1=Triangle(5,5,10,20,char=\"^\")\n", + "s1.paint(c1)\n", + "l1=Line(1,8,4,11)\n", + "l1.paint(c1)\n", + "\n", + "p1=Point(1,1)\n", + "p1.paint(c1)\n", + "c1.display()" + ], + "execution_count": 228, + "outputs": [ + { + "output_type": "stream", + "text": [ + " \n", + " * \n", + " \n", + " \n", + " \n", + " ^ \n", + " ^ \n", + " ^ \n", + " ^ * \n", + " ^ * \n", + " ^ * \n", + " ^ \n", + " ^ \n", + " ^ \n", + " ^ \n", + " ^^^^^^^^^^^^^^^^^^^^ \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4c2Afbs1k-po", + "colab_type": "text" + }, + "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", + "metadata": { + "id": "ebqAa1xQk-pp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class Arc():\n", + " def __init__(self, x0, y0, ax1, ax2, start, end, **kwargs):\n", + " Shape.__init__(self, **kwargs)\n", + " self.x0 = x0\n", + " self.y0 = y0 \n", + " self.a1 = a1\n", + " self.a2 = a2\n", + " self.start = start\n", + " self.end = end \n", + " \n", + " def paint(self,canvas):\n", + " \n", + "class Circle(Arc):\n", + " def __init__(self, x0, y0, radius, **kwargs):\n", + " Shape.__init__(self, **kwargs)\n", + " self.x0 = x0\n", + " self.y0 = y0 \n", + " self.radius = radius\n", + "\n", + " def paint(self,canvas): \n", + " self.set_pixel(x0, y0 + radius, **kwargs)\n", + " self.set_pixel(x0, y0 - radius, **kwargs)\n", + " self.set_pixel(x0 + radius, y0, **kwargs)\n", + " self.set_pixel(x0 - radius, y0, **kwargs)\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tQ34Dp18fnGT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "c1=Canvas(50,40)\n", + "C1=Circle(5,5,5)\n", + "C1.paint(c1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "l5w8AVjsk-ps", + "colab_type": "text" + }, + "source": [ + "3. Use your classes to create a `RasterDrawing` that draws a happy face." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Acg8Guiyk-ps", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 357 + }, + "outputId": "1b6da2d7-db3e-4969-e36c-ea80a6906bbd" + }, + "source": [ + "c1=Canvas(20,20)\n", + "rd=RasterDrawing()\n", + "rd.add_shape(Point(10,10))#eyes\n", + "rd.add_shape(Point(10,15))\n", + "\n", + "rd.paint(c1)\n", + "c1.display()" + ], + "execution_count": 240, + "outputs": [ + { + "output_type": "stream", + "text": [ + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " * * \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ScMg7pBNk-pw", + "colab_type": "text" + }, + "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", + "metadata": { + "id": "A8mH9tVxk-px", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "dde6f57a-4a50-4108-84d5-ba7f89543870" + }, + "source": [ + "c1=Canvas(20,20)\n", + "s1=Triangle(5,5,10,20,char=\"^\")\n", + "s1.__str__()" + ], + "execution_count": 248, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Triangle(5,5,10,20)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "53an4Vpok-p0", + "colab_type": "text" + }, + "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", + "metadata": { + "id": "CM_A74gOk-p1", + "colab_type": "code", + "colab": {} + }, + "source": [ + "eval(\"print('Hello')\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "3PGzlyJ-k-p3", + "colab_type": "code", + "colab": {} + }, + "source": [ + "x = eval('1+2')\n", + "print(x)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "zgLMCeQHk-p6", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Exams/Mid-term/Exam_Answers.ipynb b/Exams/Mid-term/Exam_Answers.ipynb new file mode 100644 index 0000000..a6d35fb --- /dev/null +++ b/Exams/Mid-term/Exam_Answers.ipynb @@ -0,0 +1,596 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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.1" + }, + "colab": { + "name": "Exam_Answers.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "K6O7zMIyxiXv", + "colab_type": "text" + }, + "source": [ + "# Mid-term 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/Mid-term/Exam.ipynb)\n", + "\n", + "Add cells to this notebook as you need for your solutions and your test of your solutions." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kAqE82JuxiX6", + "colab_type": "text" + }, + "source": [ + "1. Write a function `first_alphabetically(lst)` that takes a list `lst` of strings and returns the string that is alphabetically first. For example, calling your function with the list of states:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "931YKu2SxiYD", + "colab_type": "code", + "colab": {} + }, + "source": [ + "states=['Mississippi', 'Maryland', 'Delaware', 'Connecticut', 'Virginia', 'Utah', 'Kansas',\n", + " 'Wyoming', 'Indiana', 'Louisiana', 'Missouri', 'Illinois', 'Minnesota', 'Vermont', \n", + " 'New Mexico', 'North Dakota', 'Wisconsin', 'Tennessee', 'New York', 'Oklahoma', \n", + " 'Colorado', 'Pennsylvania', 'West Virginia', 'Alabama', 'Montana', 'Texas', \n", + " 'Washington', 'Michigan', 'New Hampshire', 'Arkansas', 'Hawaii', 'Iowa', \n", + " 'Idaho', 'Kentucky', 'Ohio', 'Nebraska', 'Alaska', 'Oregon', 'South Dakota', \n", + " 'New Jersey', 'Florida', 'Georgia', 'Rhode Island', 'Arizona', 'Maine', \n", + " 'South Carolina', 'California', 'Nevada', 'Massachusetts', 'North Carolina']" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wwpsLlVDxiYJ", + "colab_type": "text" + }, + "source": [ + "should return the string `\"Alabama\"`. Note that you can compare strings:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "DSiE0xjSMWiO", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def first_alphabetically(lst): #insertion sort\n", + " old_list=[i for i in lst]\n", + " for i in range(1, len(old_list)): \n", + " key = old_list[i] \n", + " j = i-1\n", + " while j >= 0 and key < old_list[j]: \n", + " old_list[j + 1] = old_list[j] \n", + " j -= 1\n", + " old_list[j + 1] = key \n", + " return(old_list[0])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "GHspUKPzxiYK", + "colab_type": "code", + "outputId": "fb44bd47-9754-4d50-e691-272ebda4ccd9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + } + }, + "source": [ + "print(first_alphabetically(states))\n", + "print(\"A\">\"B\")\n", + "print(\"B\">\"A\")\n", + "print(\"A\">\"a\")\n", + "print(\"bca\">\"bbc\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Alabama\n", + "False\n", + "True\n", + "False\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k-QBX0DAxiYP", + "colab_type": "text" + }, + "source": [ + "Make sure your implementation isn't case sensitive. Do not use python's built-in `min`, `max`, `sort` or any other sort function you find." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1fYHeo6KxiYQ", + "colab_type": "text" + }, + "source": [ + "2. Write a function `arg_first_alphabetically(lst)`, which does the same thing as in exercise 1 but returns the index of the first string alphabetically." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "J178RlSaSfCb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def arg_first_alphabetically(lst):\n", + " old_list=[i for i in lst]\n", + " for i in range(1, len(old_list)): \n", + " key = old_list[i] \n", + " j = i-1\n", + " while j >= 0 and key < old_list[j]: \n", + " old_list[j + 1] = old_list[j] \n", + " j -= 1\n", + " old_list[j + 1] = key \n", + " return lst.index(old_list[0])\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "R-DxJwYfL52W", + "colab_type": "code", + "outputId": "13694e1e-0569-4c67-d207-8f4aac925ea6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "arg_first_alphabetically(states)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "23" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 73 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fj5N0uCIxiYR", + "colab_type": "text" + }, + "source": [ + "3. Use your result in question 2 to implement a function `arg_sort_alphabetically(lst)` that returns a list that is alphabetically sorted. Sorting can be accomplished by successively applying the function in question 1 and removing the first element alphabetically. You can remove an element from a list using `pop()`. Make sure your implementation isn't case sensitive. Do not use python's built-in `min`, `max`, `sort` or any other sort function you find." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0rfByBhPUoQb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def arg_sort_alphabetically(lst):\n", + " for i in range(1, len(lst)): \n", + " key = lst[i] \n", + " j = i-1\n", + " while j >= 0 and key < lst[j] : \n", + " lst[j + 1] = lst[j] \n", + " j -= 1\n", + " lst[j + 1] = key \n", + " return(lst)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "hyvZ04mNU3mh", + "colab_type": "code", + "outputId": "ce773031-d167-43e7-d1a7-87cd959a9c7b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 867 + } + }, + "source": [ + "arg_sort_alphabetically(states)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "['Alabama',\n", + " 'Alaska',\n", + " 'Arizona',\n", + " 'Arkansas',\n", + " 'California',\n", + " 'Colorado',\n", + " 'Connecticut',\n", + " 'Delaware',\n", + " 'Florida',\n", + " 'Georgia',\n", + " 'Hawaii',\n", + " 'Idaho',\n", + " 'Illinois',\n", + " 'Indiana',\n", + " 'Iowa',\n", + " 'Kansas',\n", + " 'Kentucky',\n", + " 'Louisiana',\n", + " 'Maine',\n", + " 'Maryland',\n", + " 'Massachusetts',\n", + " 'Michigan',\n", + " 'Minnesota',\n", + " 'Mississippi',\n", + " 'Missouri',\n", + " 'Montana',\n", + " 'Nebraska',\n", + " 'Nevada',\n", + " 'New Hampshire',\n", + " 'New Jersey',\n", + " 'New Mexico',\n", + " 'New York',\n", + " 'North Carolina',\n", + " 'North Dakota',\n", + " 'Ohio',\n", + " 'Oklahoma',\n", + " 'Oregon',\n", + " 'Pennsylvania',\n", + " 'Rhode Island',\n", + " 'South Carolina',\n", + " 'South Dakota',\n", + " 'Tennessee',\n", + " 'Texas',\n", + " 'Utah',\n", + " 'Vermont',\n", + " 'Virginia',\n", + " 'Washington',\n", + " 'West Virginia',\n", + " 'Wisconsin',\n", + " 'Wyoming']" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 4 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zeeshkljxiYS", + "colab_type": "text" + }, + "source": [ + "4. Implement a function `outer_product` that takes two one-dimensional lists of numbers and returns the two-dimensional outer product matrix defined as:\n", + "\n", + "\\begin{equation*}\n", + "\\begin{pmatrix} x_1\\\\x_2\\\\ \\vdots \\\\x_m \\end{pmatrix} \\begin{pmatrix} y_1&y_2& \\dots &y_n\\end{pmatrix} =\n", + "\\begin{pmatrix}\n", + "x_1y_1 & x_1y_2 & \\dots & x_1y_n\\\\\n", + "x_2y_1 & x_2y_2 & \\dots & x_2y_n\\\\\n", + "\\vdots & \\vdots & \\ddots & \\vdots \\\\\n", + "x_my_1 & x_my_2 & \\dots & x_my_n\n", + "\\end{pmatrix}\n", + "\\end{equation*}\n", + "\n", + "In other words the elements of matrix C which is the outer product of A and B are $c_{ij} = a_i b_j$." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-93X0PEXVbJw", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def outer_product(a,b):\n", + " output=[]\n", + " x=len(a)\n", + " y=len(b)\n", + " for i in range(0,x):\n", + " new=[]\n", + " for j in range(0,y):\n", + " new.append(a[i]*b[j])\n", + " output.append(new)\n", + " return(output)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "lKCx3YyXQ90O", + "colab_type": "code", + "outputId": "7bcb3a9b-ea7a-4ea0-a8c6-d413cbf38f06", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "a=[1,2,3,4]\n", + "b=[2,4,6]\n", + "\n", + "outer_product(a,b)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[[2, 4, 6], [4, 8, 12], [6, 12, 18], [8, 16, 24]]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 156 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-pHPELpDxiYS", + "colab_type": "text" + }, + "source": [ + "5. Implement a function `cumulative_sum(lst)` that takes a list of numbers and returns a list of same size where the element `i` is the sum of the elements `0` to `i` of the input list. For example given `[1,2,3]`, you should return [1,3,6]." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "H6tyKqcwVVMj", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def cumulative_sum(lst): \n", + " total=[]\n", + " sum=0\n", + " for x in lst:\n", + " sum += x\n", + " total.append(sum)\n", + " return(total)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ki_-eRKOY5Vy", + "colab_type": "code", + "outputId": "2d8256be-c2d9-4490-c397-1411b8bc8ec7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "lst=[1,2,3,10,20,30]\n", + "cumulative_sum(lst)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 3, 6, 16, 36, 66]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 58 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6y_hjDCSxiYT", + "colab_type": "text" + }, + "source": [ + "6. Imagine you have a normal distributed random variable `x`. For example `x` can be grades on this exam. Using the normal distribution generator and histogram functions from lecture (provided below) and `cumulative_sum` from previous question to compute what is the value of $x_{90}$ in $\\sigma$ such that 90% of the values $x$ are below $x_{90}$. In other words:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UBOvOtjdxiYU", + "colab_type": "text" + }, + "source": [ + "$$\n", + "\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n", + "$$" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PI219lY9xiYV", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import math,random\n", + "\n", + "def arange(x_min,x_max,steps=10):\n", + " step_size=(x_max-x_min)/steps\n", + " x=x_min\n", + " out = list()\n", + " for i in range(steps):\n", + " out.append(x)\n", + " x+=step_size\n", + " return out\n", + "\n", + "def generate_normal(N,m=0,s=1):\n", + " out = list() \n", + " \n", + " while len(out)=bin_edges[i] and d\" symbol directs stdout into a file. Try \"cat > favorite-colors-list.txt\" and then type in your 3 favorite colors, each on it's own line. Use \"^D\" to end your input." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H5vxtcXnqvz6", + "colab_type": "text" + }, + "source": [ + "Use \"cat\", \"more\", or \"less\" to confirm that you file is as you expect it. \">>\" allows you to append to the file. \n", + "\n", + "*Exercise 5b:* Append 2 more colors to your file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "twRKNaGy3XGw", + "colab_type": "code", + "outputId": "a5b4f379-d688-43fd-db53-6d29ae3af350", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 391 + } + }, + "source": [ + "!/bin/bash --noediting" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (118): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@82772d01144f: /content/sample_data\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd ..\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# cd Drive\n", + "bash: cd: Drive: No such file or directory\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# ls\n", + "\u001b[0m\u001b[01;34mdrive\u001b[0m \u001b[01;34msample_data\u001b[0m\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# cd drive/'My Drive'/Data-1441/Lab-1-Solutions\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# cat >favorite-colors-list.txt\n", + "blue\n", + "pink \n", + "red\n", + "\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# more favorite-colors-list.txt\n", + "blue\n", + "pink \n", + "red\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# cat >>favorite-colors-list.txt\n", + "orange\n", + "green\n", + "\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# ^C\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DZODNKiAqvz8", + "colab_type": "text" + }, + "source": [ + "The \"sort\" command sorts what it sees on stdin. Instead of taking input from the terminal, you can direct the shell to take stdin from a file using \"<\". Try \"sort < favorite-color-list.txt\" and \"sort < favorite-color-list.txt > sorted-favorite-color-list.txt\".\n", + "\n", + "Finally, instead of piping input / output into files, you can directly chain one program into another using \"|\". So for example, you can do \"cat /etc/passwd | grep -i \\$USER | wc -l\". \n", + "\n", + "*Exercise 5c:* Use indirection to count the number of users on TACC with your first name. Copy the command you used into box below." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oP9XlZl_3iZD", + "colab_type": "code", + "outputId": "b13d3e6d-a2ba-4ca9-8e88-d4ee89748ecb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 680 + } + }, + "source": [ + "!/bin/bash --noediting\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "bash: cannot set terminal process group (118): Inappropriate ioctl for device\n", + "bash: no job control in this shell\n", + "\u001b]0;root@82772d01144f: /content/sample_data\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/sample_data\u001b[00m# cd ..\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# drive/'My drive'/Data-1441/Lab-1-Solutions\n", + "bash: drive/My drive/Data-1441/Lab-1-Solutions: No such file or directory\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# cd drive/'My drive'/Data-1441/Lab-1-Solutions\n", + "bash: cd: drive/My drive/Data-1441/Lab-1-Solutions: No such file or directory\n", + "\u001b]0;root@82772d01144f: /content\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content\u001b[00m# cd drive\n", + "\u001b]0;root@82772d01144f: /content/drive\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive\u001b[00m# cd 'My drive'/Data-1441\n", + "bash: cd: My drive/Data-1441: No such file or directory\n", + "\u001b]0;root@82772d01144f: /content/drive\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive\u001b[00m# ls\n", + "\u001b[0m\u001b[01;34m'My Drive'\u001b[0m\n", + "\u001b]0;root@82772d01144f: /content/drive\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive\u001b[00m# cd 'My Drive'/Data-1441/Lab-1-Solutions\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# sort sorted-favorite-colors-list.txt\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# cat /etc/passwd | grep -i $USER | wc -l\n", + "Usage: grep [OPTION]... PATTERN [FILE]...\n", + "Try 'grep --help' for more information.\n", + "0\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# grep -c $USER | wc -l\n", + "Usage: grep [OPTION]... PATTERN [FILE]...\n", + "Try 'grep --help' for more information.\n", + "0\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# cat /etc/passwd | grep -c $USER | wc -l\n", + "Usage: grep [OPTION]... PATTERN [FILE]...\n", + "Try 'grep --help' for more information.\n", + "0\n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# \n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# \n", + "\u001b]0;root@82772d01144f: /content/drive/My Drive/Data-1441/Lab-1-Solutions\u0007\u001b[01;32mroot@82772d01144f\u001b[00m:\u001b[01;34m/content/drive/My Drive/Data-1441/Lab-1-Solutions\u001b[00m# ^C\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v5IaZXNyqvz_", + "colab_type": "text" + }, + "source": [ + "## Git\n", + "\n", + "`git` is a Version Control System (VCS), typically used to organize the source code of software project but also good source of documents or web-pages. An instance of `git` server stores repositories, each typically containing the code relevant to a specific project. Users create local `clones` of repositories, change and develop the local copies of the code, `commit` the changes to their local repository, `push` to the server as a contribution, \n", + "`pull` updates from the server, and `merge` changes between local and remote versions. \n", + "\n", + "Besides cloning, repositories can be branched or forked. A repository generally starts with a `master` branch that evolves as push requests are merged in. Creating a new branch from an existing branch creates a snapshot of the which can evolve independently or be merged in later. Branches are easy to make and delete, and can serve various purposes. They can represent a stable version of software package. Or a parallel development for different operating system. A fork of a repository is a standalone instance of the repository which can be stored and managed independently from the original, where you can work independently without constraints or interference. \n", + "\n", + "[GitHub](github.com) provides a massive publically accessible instance of a `git` system besides sharing code, projects can be developed by the open source community. It provides tools for managing your repository and a wiki for documentation. Contributions to public software on GitHub generally require making a merge request which would be judged by the managers of the repository. That's why most software packages enourage you to create a new fork, so you can work independently.\n", + "\n", + "Lets take a look at some repositories:\n", + "\n", + "* [This class](https://github.com/afarbin/DATA1401-Spring-2020)\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J_R64sQDqv0A", + "colab_type": "text" + }, + "source": [ + "## Plan\n", + "\n", + "You made a clone of the class repository at start of this lab. We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, lets create a fork of the [class repository](https://github.com/afarbin/DATA1401-Spring-2019). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "edTvE6rOqv0C", + "colab_type": "text" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab.\n", + "\n", + "Note: Jupyter allows you to run shell directly in a notebook. We will use `!` and `%` to call shell commands directly in this notebook. Follow along yourself. Either create a new notebook or open a terminal. \n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e5tXg0f8qv0D", + "colab_type": "code", + "outputId": "21602300-b695-4745-f012-438c55e4caa3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "%cd /content/drive/My\\ Drive\n" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[Errno 2] No such file or directory: '/content/drive/My Drive'\n", + "/content\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WYsyYcg1qv0J", + "colab_type": "text" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z7noY1hMqv0L", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fwsBdTnYqv0Q", + "colab_type": "text" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8w42MH6Jqv0S", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# What you past here should look like:\n", + "#!git clone https://github.com//DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cOAuqTVUqv0V", + "colab_type": "text" + }, + "source": [ + "Go into the directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "b1Ew4tEZqv0X", + "colab_type": "code", + "colab": {} + }, + "source": [ + "%cd DATA1401-Spring-2020\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IrhWToc-qv0a", + "colab_type": "text" + }, + "source": [ + "We will now connect your fork to the original so you can pull changes from there. \n", + "\n", + "Check remote status:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JxtMYR-9qv0c", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9ud3X0fBqv0f", + "colab_type": "text" + }, + "source": [ + "Now use the original class URL to set your upstream:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pgJlKxBqqv0h", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote add upstream https://github.com/afarbin/DATA1401-Spring-2020.git" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "id2yUEt9qv0k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git remote -v" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sAkgeJ6Iqv0n", + "colab_type": "text" + }, + "source": [ + "From now on, you can get the newest version of class material by using:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "AGDsfTFLqv0o", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!git pull" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u9RAhs5b4vXY", + "colab_type": "text" + }, + "source": [ + "We will submit your Lab 1 using git at the next Lab." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PPfGmFQI40HR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Labs/Lab-2/Lab_2_Solutions.ipynb b/Labs/Lab-2/Lab_2_Solutions.ipynb new file mode 100644 index 0000000..2ca499c --- /dev/null +++ b/Labs/Lab-2/Lab_2_Solutions.ipynb @@ -0,0 +1,1101 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "colab": { + "name": "Copy of Lab-2.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "uk7yc0nadBGa", + "colab_type": "text" + }, + "source": [ + "# Lab 2\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/Labs/Lab-2/Lab-2.ipynb)\n", + "\n", + "## Submitting lab solutions\n", + "\n", + "At the end of the previous lab, you should have set up a \"Solutions\" directory in your Google Drive, with a fork of the class git repository that pull from Dr. Farbin's verison and pushes to your own fork. \n", + "\n", + "Unfortunately due to a typo in the previous lab, you probably forked the 2019 version of the gitlab repository for this course. Unless you noticed and corrected the error, you'll have to fork again.\n", + "\n", + "In addition, due to some problems with the setup in Google Colab, we will be submitting our solutions to your fork using the web interface. Instructions on how to use the command-line are in this notebook, but we suggest you do not follow them unless you are working in a jupyter notebook and not Google Colab." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OMNaOnRksNK3", + "colab_type": "text" + }, + "source": [ + "You may also choose to delete the fork from your GitHub account. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "J_R64sQDqv0A" + }, + "source": [ + "## Repeating last steps of Lab 1\n", + "\n", + "### Create your own fork\n", + "We will create a new fork where you can keep track and submit your work, following [these instructions](https://help.github.com/articles/fork-a-repo/).\n", + "\n", + "Goto to github.com and log in.\n", + "\n", + "Next, create a fork of the [2020 class repository](https://github.com/afarbin/DATA1401-Spring-2020). Click the link and press the \"Fork\" button on the top right. Select your repository as where you want to place the fork.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "edTvE6rOqv0C" + }, + "source": [ + "### Make a local clone (Advanced)\n", + "\n", + "Before we get started, please mount your Google Drive using by clicking the file icon on the left, then clicking \"Mount Drive\", and following the instructions as you did in the previous lab.\n", + "\n", + "If you did complete Lab 1 and therefore created a 2019 fork and a local clone in you Google Drive, delete the local clone:\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2u6B-rfNr1wN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "!rm -rf drive/My\\ Drive/Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BDVI5nu8-2RH", + "colab_type": "text" + }, + "source": [ + "Now we will check out your fork in your Google Drive / Colab. If you will be doing everything on your own computer instead of Google Colab/Drive, you are welcome to install Git on your computer and perform the following steps (appropriately modified) on your computer instead.\n", + "\n", + "Start by listing the contents of your current directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "e5tXg0f8qv0D", + "colab": {} + }, + "source": [ + "%cd /content/drive/My\\ Drive\n", + "!ls" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "WYsyYcg1qv0J" + }, + "source": [ + "Make a new directory:" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Z7noY1hMqv0L", + "colab": {} + }, + "source": [ + "!mkdir Data-1401-Repo\n", + "%cd Data-1401-Repo" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "fwsBdTnYqv0Q" + }, + "source": [ + "From the github page for your fork, press the green \"Clone or download\" button and copy the URL.\n", + "\n", + "Goto to your notebook and use the following command to clone the repository, pasting the URL you just copied:\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "8w42MH6Jqv0S", + "colab": {} + }, + "source": [ + "# What you past here should look like:\n", + "#!git clone https://github.com/ 730\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 731\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 465\u001b[0m \"\"\"\n\u001b[0;32m--> 466\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 467\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n","\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n","\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: ","\nDuring handling of the above exception, another exception occurred:\n","\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)","\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mboard\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmake_game_board\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mfull_game\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;32m\u001b[0m in \u001b[0;36mfull_game\u001b[0;34m(b, p)\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mgame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0mp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mwin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mboard\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m\u001b[0m in \u001b[0;36mgame\u001b[0;34m(b, p)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m!=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Make a Move: '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtake_move\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mdraw_board\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "]}]},{"cell_type":"markdown","metadata":{"deletable":true,"editable":true,"id":"ktgZfEHEPfLM","colab_type":"text"},"source":["*Exercise 11: (Extra Credit)* Develop a version of the game where one player is the computer. Note that you don't need to do an extensive seach for the best move. You can have the computer simply protect against loosing and otherwise try to win with straight or diagonal patterns."]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"pNnMK2hBPfLN","colab_type":"code","colab":{}},"source":["# Write you solution here"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"deletable":true,"editable":true,"id":"B1gzu9EJPfLT","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]}]} \ No newline at end of file diff --git a/Labs/Lab-4/Lab-4-Solutions.ipynb b/Labs/Lab-4/Lab-4-Solutions.ipynb new file mode 100644 index 0000000..4ba3196 --- /dev/null +++ b/Labs/Lab-4/Lab-4-Solutions.ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"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.1"},"colab":{"name":"Lab-4-Solutions.ipynb","provenance":[{"file_id":"https://github.com/afarbin/DATA1401-Spring-2020/blob/master/Labs/Lab-4/Lab-4.ipynb","timestamp":1583511217256}],"collapsed_sections":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"tF8VIBcwn1iH","colab_type":"text"},"source":["## Lab 4\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/Labs/Lab-4/Lab-4.ipynb)\n","\n","In this lab we will become familiar with distributions, histograms, and functional programming. \n","\n","\n","### Uniform Distribution\n","Lets start with generating some fake random data. You can get a random number between 0 and 1 using the python random module as follow:"]},{"cell_type":"code","metadata":{"id":"yEQxT5NGn1iL","colab_type":"code","outputId":"c066dae8-852f-470a-eb42-ec1f21e1ef63","colab":{"base_uri":"https://localhost:8080/","height":34},"executionInfo":{"status":"ok","timestamp":1583511245546,"user_tz":360,"elapsed":525,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["import random\n","x=random.random()\n","print(\"The Value of x is\", x)"],"execution_count":1,"outputs":[{"output_type":"stream","text":["The Value of x is 0.4049658004556974\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"RTW5hUCrn1iQ","colab_type":"text"},"source":["Everytime you call random, you will get a new number.\n","\n","*Exercise 1:* Using random, write a function `generate_uniform(N, mymin, mymax)`, that returns a python list containing N random numbers between specified minimum and maximum value. Note that you may want to quickly work out on paper how to turn numbers between 0 and 1 to between other values. "]},{"cell_type":"code","metadata":{"id":"TGEvDfVUn1iR","colab_type":"code","colab":{}},"source":["# Skeleton\n","def generate_uniform(N,x_min,x_max):\n"," out = []\n"," for i in range (0,N):\n"," x=random.randint(x_min,x_max)\n"," out.append(x)\n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"RsfIGDdAn1iT","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":102},"outputId":"6da67292-61f4-4941-8b3d-a78060821736","executionInfo":{"status":"ok","timestamp":1583511495011,"user_tz":360,"elapsed":326,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","data=generate_uniform(1000,-10,10)\n","print (\"Data Type:\", type(data))\n","print (\"Data Length:\", len(data))\n","if len(data)>0: \n"," print (\"Type of Data Contents:\", type(data[0]))\n"," print (\"Data Minimum:\", min(data))\n"," print (\"Data Maximum:\", max(data))"],"execution_count":4,"outputs":[{"output_type":"stream","text":["Data Type: \n","Data Length: 1000\n","Type of Data Contents: \n","Data Minimum: -10\n","Data Maximum: 10\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"KX-GmK5In1iW","colab_type":"text"},"source":["*Exercise 2a:* \n","Write a function that computes the mean of values in a list."]},{"cell_type":"code","metadata":{"id":"YkSzFEkvn1iX","colab_type":"code","colab":{}},"source":["# Skeleton\n","def mean(Data):\n"," m=0.\n"," m=sum(Data)/len(Data)\n"," \n"," return m"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"HAxcYUUdn1ib","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"d8b554c7-2100-4e52-803d-7bf913a42c1a","executionInfo":{"status":"ok","timestamp":1583511667561,"user_tz":360,"elapsed":337,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","print (\"Mean of Data:\", mean(data))"],"execution_count":6,"outputs":[{"output_type":"stream","text":["Mean of Data: 0.033\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"M3kUsJGWn1ie","colab_type":"text"},"source":["*Exercise 2b:* \n","Write a function that computes the variance of values in a list."]},{"cell_type":"code","metadata":{"id":"P1solaF7n1if","colab_type":"code","colab":{}},"source":["# Skeleton\n","import statistics\n","def variance(Data):\n"," m=0.\n"," m=statistics.variance(data)\n","\n"," return m"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"G1XBcmjan1ih","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"aa7b8001-105c-4d8a-e064-99990a400fe9","executionInfo":{"status":"ok","timestamp":1583511985131,"user_tz":360,"elapsed":228,"user":{"displayName":"Jeslin Joseph","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Gh9Z75F6WeXCk3JD7LmHP6FcpoLRvAuHaO3_-TdUg=s64","userId":"12354079780932175318"}}},"source":["# Test your solution here\n","print (\"Variance of Data:\", variance(data))"],"execution_count":12,"outputs":[{"output_type":"stream","text":["Variance of Data: 35.51542642642642\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"IirsOIVWn1ik","colab_type":"text"},"source":["## Histogramming"]},{"cell_type":"markdown","metadata":{"id":"JjsvPalGn1il","colab_type":"text"},"source":["*Exercise 3:* Write a function that bins the data so that you can create a histogram. An example of how to implement histogramming is the following logic:\n","\n","* User inputs a list of values `x` and optionally `n_bins` which defaults to 10.\n","* If not supplied, find the minimum and maximum (`x_min`,`x_max`) of the values in x.\n","* Determine the bin size (`bin_size`) by dividing the range of the function by the number of bins.\n","* Create an empty list of zeros of size `n_bins`, call it `hist`.\n","* Loop over the values in `x`\n"," * Loop over the values in `hist` with index `i`:\n"," * If x is between `x_min+i*bin_size` and `x_min+(i+1)*bin_size`, increment `hist[i].` \n"," * For efficiency, try to use continue to goto the next bin and data point.\n","* Return `hist` and the list corresponding of the bin edges (i.e. of `x_min+i*bin_size`). "]},{"cell_type":"code","metadata":{"id":"xDa5AtIDn1im","colab_type":"code","colab":{}},"source":["# Solution\n","def histogram(x,n_bins=10,x_min=None,x_max=None):\n"," \n","\n","\n"," return hist,bin_edges"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"1P5HnmTIn1ip","colab_type":"code","colab":{}},"source":["# Test your solution here\n","h,b=histogram(data,100)\n","print(h)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"erSLdAecn1it","colab_type":"text"},"source":["*Exercise 4:* Write a function that uses the histogram function in the previous exercise to create a text-based \"graph\". For example the output could look like the following:\n","```\n","[ 0, 1] : ######\n","[ 1, 2] : #####\n","[ 2, 3] : ######\n","[ 3, 4] : ####\n","[ 4, 5] : ####\n","[ 5, 6] : ######\n","[ 6, 7] : #####\n","[ 7, 8] : ######\n","[ 8, 9] : ####\n","[ 9, 10] : #####\n","```\n","\n","Where each line corresponds to a bin and the number of `#`'s are proportional to the value of the data in the bin. "]},{"cell_type":"code","metadata":{"id":"3DP7KMqRn1iu","colab_type":"code","colab":{}},"source":["# Solution\n","def draw_histogram(x,n_bins,x_min=None,x_max=None,character=\"#\",max_character_per_line=20):\n"," ### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n"," ### END SOLUTION\n","\n"," return hist,bin_edges"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"zkCVWu0nn1iw","colab_type":"code","colab":{}},"source":["# Test your solution here\n","h,b=histogram(data,20)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"mNxJ1cVNn1iy","colab_type":"text"},"source":["## Functional Programming\n","\n","*Exercise 5:* Write a function the applies a booling function (that returns true/false) to every element in data, and return a list of indices of elements where the result was true. Use this function to find the indices of entries greater than 0.5. "]},{"cell_type":"code","metadata":{"id":"o6LPTfm6n1i0","colab_type":"code","colab":{}},"source":["def where(mylist,myfunc):\n"," out= []\n"," \n"," \n"," \n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"w2PiMQS6n1i2","colab_type":"code","colab":{}},"source":["# Test your solution here"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"XKlshea7n1i6","colab_type":"text"},"source":["*Exercise 6:* The inrange(mymin,mymax) function below returns a function that tests if it's input is between the specified values. Write corresponding functions that test:\n","* Even\n","* Odd\n","* Greater than\n","* Less than\n","* Equal\n","* Divisible by"]},{"cell_type":"code","metadata":{"id":"PgXgF8uKn1i6","colab_type":"code","colab":{}},"source":["def in_range(mymin,mymax):\n"," def testrange(x):\n"," return x=mymin\n"," return testrange\n","\n","# Examples:\n","F1=inrange(0,10)\n","F2=inrange(10,20)\n","\n","# Test of in_range\n","print (F1(0), F1(1), F1(10), F1(15), F1(20))\n","print (F2(0), F2(1), F2(10), F2(15), F2(20))\n","\n","print (\"Number of Entries passing F1:\", len(where(data,F1)))\n","print (\"Number of Entries passing F2:\", len(where(data,F2)))"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"Q_HjcyH7n1i9","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"s3PLinaDn1i_","colab_type":"code","colab":{}},"source":["# Test your solution"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"ZhkwG7n1n1jC","colab_type":"text"},"source":["*Exercise 7:* Repeat the previous exercise using `lambda` and the built-in python functions sum and map instead of your solution above. "]},{"cell_type":"code","metadata":{"id":"jVIM_dcQn1jD","colab_type":"code","colab":{}},"source":["### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n","### END SOLUTION"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"sV-gUy3Hn1jF","colab_type":"text"},"source":["## Monte Carlo\n","\n","*Exercise 7:* Write a \"generator\" function called `generate_function(func,x_min,x_max,N)`, that instead of generating a flat distribution, generates a distribution with functional form coded in `func`. Note that `func` will always be > 0. \n","\n","Use the test function below and your histogramming functions above to demonstrate that your generator is working properly.\n","\n","Hint: A simple, but slow, solution is to a draw random number test_x within the specified range and another number p between the min and max of the function (which you will have to determine). If p<=function(test_x), then place test_x on the output. If not, repeat the process, drawing two new numbers. Repeat until you have the specified number of generated numbers, N. For this problem, it's OK to determine the min and max by numerically sampling the function. "]},{"cell_type":"code","metadata":{"id":"uodRGGvSn1jG","colab_type":"code","colab":{}},"source":["def generate_function(func,x_min,x_max,N=1000):\n"," out = list()\n"," ### BEGIN SOLUTION\n","\n"," # Fill in your solution here \n"," \n"," ### END SOLUTION\n"," \n"," return out"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"3AcyVd4Dn1jI","colab_type":"code","colab":{}},"source":["# A test function\n","def test_func(x,a=1,b=1):\n"," return abs(a*x+b)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"8Dxz8u-3n1jK","colab_type":"text"},"source":["*Exercise 8:* Use your function to generate 1000 numbers that are normal distributed, using the `gaussian` function below. Confirm the mean and variance of the data is close to the mean and variance you specify when building the Gaussian. Histogram the data. "]},{"cell_type":"code","metadata":{"id":"imbkswPZn1jL","colab_type":"code","colab":{}},"source":["import math\n","\n","def gaussian(mean, sigma):\n"," def f(x):\n"," return math.exp(-((x-mean)**2)/(2*sigma**2))/math.sqrt(math.pi*sigma)\n"," return f\n","\n","# Example Instantiation\n","g1=gaussian(0,1)\n","g2=gaussian(10,3)"],"execution_count":0,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"x5ASGq12n1jN","colab_type":"text"},"source":["*Exercise 9:* Combine your `generate_function`, `where`, and `in_range` functions above to create an integrate function. Use your integrate function to show that approximately 68% of Normal distribution is within one variance."]},{"cell_type":"code","metadata":{"id":"_gLjho7Vn1jO","colab_type":"code","colab":{}},"source":["def integrate(func, x_min, x_max, n_points=1000):\n"," \n"," return integral"],"execution_count":0,"outputs":[]},{"cell_type":"code","metadata":{"id":"jHUyEJogn1jQ","colab_type":"code","colab":{}},"source":[""],"execution_count":0,"outputs":[]}]} \ No newline at end of file diff --git a/Labs/Lab-5/Lab_5_Answers.ipynb b/Labs/Lab-5/Lab_5_Answers.ipynb new file mode 100644 index 0000000..233f130 --- /dev/null +++ b/Labs/Lab-5/Lab_5_Answers.ipynb @@ -0,0 +1,504 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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.1" + }, + "colab": { + "name": "Lab-5-Answers.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "q79noSDwno2w", + "colab_type": "text" + }, + "source": [ + "# Lab 5- Object Oriented Programming\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/Labs/Lab-5/Lab-5.ipynb)\n", + "\n", + "For all of the exercises below, make sure you provide tests of your solutions.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3my9M0Cino2x", + "colab_type": "text" + }, + "source": [ + "1. Write a \"counter\" class that can be incremented up to a specified maximum value, will print an error if an attempt is made to increment beyond that value, and allows reseting the counter. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kUsz8BA9no2x", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class counter:\n", + " def __init__(self,max_val):\n", + " self.max_val=max_val\n", + " self.cur_val=1\n", + " \n", + " def increment(self):\n", + " if self.cur_val>self.max_val:\n", + " print(\"Max value reached.\")\n", + " else:\n", + " self.cur_val+=1\n", + " \n", + " def reset(self):\n", + " self.cur_val=1\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "gRtjFYeDno21", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "xQZxiXOGno23", + "colab_type": "code", + "outputId": "81692185-872c-495c-8336-57e821d08778", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + } + }, + "source": [ + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()\n", + "my_counter.increment()" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Max value reached.\n", + "Max value reached.\n", + "Max value reached.\n", + "Max value reached.\n", + "Max value reached.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PlNu9VMSno28", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter.cur_val=100" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "itqx2OiGno3B", + "colab_type": "text" + }, + "source": [ + "2. Copy and paste your solution to question 1 and modify it so that all the data held by the counter is private. Implement functions to check the value of the counter, check the maximum value, and check if the counter is at the maximum." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZIfeJzqino3C", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class counter:\n", + " def __init__(self,max_val):\n", + " self.__max_val=max_val\n", + " self.__cur_val=1\n", + " \n", + " def increment(self):\n", + " if self.__cur_val>self.__max_val:\n", + " print(\"Max value reached.\")\n", + " else:\n", + " self.__cur_val+=1\n", + " \n", + " def reset(self):\n", + " self.__cur_val=1\n", + " \n", + " def cur_val(self):\n", + " return self.__cur_val\n", + "\n", + " def max_val(self):\n", + " return self.__max_val\n", + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Q72Izyppno3F", + "colab_type": "code", + "colab": {} + }, + "source": [ + "my_counter=counter(3)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "npKqiWN2no3K", + "colab_type": "code", + "outputId": "a9cb4881-f864-44a6-aecf-8ac033475fca", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "my_counter.cur_val()" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 10 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FQQqEz_Qno3N", + "colab_type": "text" + }, + "source": [ + "3. Implement a class to represent a rectangle, holding the length, width, and $x$ and $y$ coordinates of a corner of the object. Implement functions that compute the area and parameter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "k2Y906dEno3O", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class rectangle:\n", + " def __init__(self,width,length,x,y):\n", + " self.__width=width\n", + " self.__length=length\n", + " self.__x=x\n", + " self.__y=y\n", + " \n", + " def area(self):\n", + " return self.__width*self.__length\n", + " \n", + " def perimeter(self):\n", + " return 2*(self.__width+self.__length)\n", + " \n", + " def x(self):\n", + " return self.__x\n", + " \n", + " def y(self):\n", + " return self.__y\n", + " \n", + " def width(self):\n", + " return self.__width\n", + " \n", + " def length(self):\n", + " return self.__length\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "69_XCquhno3R", + "colab_type": "text" + }, + "source": [ + "4. Implement a class to represent a circle, holding the radius and $x$ and $y$ coordinates of center of the object. Implement functions that compute the area and parameter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PaBgusBj0rOW", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy\n", + "\n", + "class circle:\n", + " def __init__(self,radius,x,y):\n", + " self.__radius=radius\n", + " self.__x=x\n", + " self.__y=y\n", + "\n", + " def area(self):\n", + " return numpy.pi*self.__radius*self.__radius\n", + " \n", + " def parameter(self):\n", + " return 2*numpy.pi*self.__radius\n", + "\n", + " def x(self):\n", + " return self.__x\n", + "\n", + " def y(self):\n", + " return self.__y\n", + "\n", + " def radius(self):\n", + " return self.__radius\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LSHTLDAano3R", + "colab_type": "text" + }, + "source": [ + "5. Implement a common base class for the classes implemented in 3 and 4 above which implements all common methods as dummy functions. Re-implement those classes to inherit from the base class and overload the functions accordingly. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "w5ivTJsR4Y3U", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy\n", + "\n", + "class base:\n", + " def __init__(self,x,y):\n", + " self.__x=x\n", + " self.__y=y\n", + "\n", + "class rectangle(base):\n", + " def __init__(self,width,length,x,y):\n", + " super(rectangle,self).__init__(x,y)\n", + " self.__width=width\n", + " self.__length=length\n", + " \n", + " def area(self):\n", + " return self.__width*self.__length\n", + "\n", + " def perimeter(self):\n", + " return 2*(self.__width+self.__length)\n", + " \n", + " def x(self):\n", + " return self.__x\n", + " \n", + " def y(self):\n", + " return self.__y\n", + "\n", + " def width(self):\n", + " return self.__width\n", + " \n", + " def length(self):\n", + " return self.__length\n", + "\n", + "\n", + "class circle(base):\n", + " def __init__(self,radius,x,y):\n", + " super(circle,self).__init__(x,y)\n", + " self._radius=radius\n", + "\n", + " def area(self):\n", + " return numpy.pi*self.__radius*self.__radius\n", + " \n", + " def parameter(self):\n", + " return 2*numpy.pi*self.__radius\n", + "\n", + " def x(self):\n", + " return self.__x\n", + "\n", + " def y(self):\n", + " return self.__y\n", + "\n", + " def radius(self):\n", + " return self.__radius" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WQ-cJ0bUno3S", + "colab_type": "text" + }, + "source": [ + "6. Implement an analogous triangle class." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yzNAI9AN7nlR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class triangle:\n", + " def __init__(self,base,height,x,y):\n", + " self.__base=base\n", + " self.__height=height\n", + " self.__x=x\n", + " self.__y=y\n", + " \n", + " def area(self):\n", + " return (self.__base*self.__height)/2\n", + "\n", + " def parameter(self):\n", + " return self.__base*3 #if equilateral triangle\n", + "\n", + " def x(self):\n", + " return self.__x\n", + "\n", + " def y(self):\n", + " return self.__y\n", + "\n", + " def base(self):\n", + " return self.__base\n", + "\n", + " def height(self):\n", + " return self.__height" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gF4XZkSMno3S", + "colab_type": "text" + }, + "source": [ + "7. Add a function to the object classes that test if a given set of $x$ and $y$ coordinates are inside of the object." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6_FSdpy09_WM", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def Pointcircle(x,y):\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eadAxA4Kno3T", + "colab_type": "text" + }, + "source": [ + "8. Add a function to the object classes that return a list of up to 16 pairs of $x$ and $y$ points on the parameter of the object.\n", + "\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "biFQojCo9_7E", + "colab_type": "code", + "colab": {} + }, + "source": [ + "\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-YNk4xwBno3T", + "colab_type": "text" + }, + "source": [ + "9. Add a function in the base class of the object classes that returns true/false testing that the object overlaps with another object." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BTqR2sIz-Afm", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Labs/Lab-7/Lab_7_Answers.ipynb b/Labs/Lab-7/Lab_7_Answers.ipynb new file mode 100644 index 0000000..4ba1ae5 --- /dev/null +++ b/Labs/Lab-7/Lab_7_Answers.ipynb @@ -0,0 +1,1603 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "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" + }, + "colab": { + "name": " Lab-7-Answers.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ceBhdufCNXx8", + "colab_type": "text" + }, + "source": [ + "# Lab 7\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/Labs/Lab-7/Lab-7.ipynb)\n", + "\n", + "Here are the \"Gradebook\" classes from lecture. For this lab, you will use these classes and are encouraged to modify them as you need." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jcKNzEbyNXx9", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy as np\n", + "import math\n", + "\n", + "# Create some virtual classes\n", + "class base:\n", + " __name=\"\"\n", + " \n", + " def __init__(self,name):\n", + " self.__name=name\n", + "\n", + " def name(self):\n", + " return self.__name\n", + "\n", + "class data(base):\n", + " def __init__(self,name):\n", + " base.__init__(self,name)\n", + " \n", + "class alg(base):\n", + " def __init__(self,name):\n", + " base.__init__(self,name)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "sGfxVzO-NXyA", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class grade(data):\n", + " __value=0\n", + " __numerical=True\n", + " __gradebook_name=str()\n", + " __letter_grades=[\"F-\",\"F\",\"F+\",\"D-\",\"D\",\"D+\",\"C-\",\"C\",\"C+\",\"B-\",\"B\",\"B+\",\"A-\",\"A\",\"A+\"]\n", + " \n", + " def __init__(self,name,numerical=True,value=None):\n", + " if value:\n", + " if isinstance(value,(int,float)):\n", + " self.__numerical=True\n", + " elif isinstance(value,str):\n", + " self.__numerical=False\n", + " self.set(value)\n", + " else: \n", + " self.__numerical=numerical\n", + " self.__gradebook_name=name\n", + " data.__init__(self,name+\" Grade Algorithm\") \n", + "\n", + " def set(self,value):\n", + " if isinstance(value,(int,float)) and self.__numerical:\n", + " self.__value=value\n", + " elif isinstance(value,str) and not self.__numerical:\n", + " if value in self.__letter_grades:\n", + " self.__value=value\n", + " else:\n", + " print (self.name()+\" Error: Bad Grade.\")\n", + " raise Exception\n", + " \n", + " def value(self):\n", + " return self.__value\n", + " \n", + " def numerical(self):\n", + " return self.__numerical\n", + " \n", + " def gradebook_name(self):\n", + " return self.__gradebook_name\n", + " \n", + " def __str__(self):\n", + " return self.__gradebook_name+\": \"+str(self.__value)\n", + "\n", + "class student(data):\n", + " __id_number=0\n", + " __grades=dict()\n", + " \n", + " def __init__(self,first_name, last_name,id_number):\n", + " self.__id_number=id_number\n", + " self.__grades=dict()\n", + " data.__init__(self,first_name+\" \"+last_name+\" Student Data\")\n", + "\n", + " def add_grade(self,a_grade,overwrite=False):\n", + " if overwrite or not a_grade.gradebook_name() in self.__grades:\n", + " self.__grades[a_grade.gradebook_name()]=a_grade\n", + " else:\n", + " print (self.name()+\" Error Adding Grade \"+a_grade.name()+\". Grade already exists.\")\n", + " raise Exception\n", + "\n", + " def id_number(self):\n", + " return self.__id_number\n", + " \n", + " def __getitem__(self,key):\n", + " return self.__grades[key]\n", + " \n", + " def print_grades(self):\n", + " for grade in self.__grades:\n", + " print (self.__grades[grade])\n", + " \n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "fHTd_vlINXyD", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class calculator(alg): \n", + " def __init__(self,name):\n", + " alg.__init__(self,name)\n", + "\n", + " def apply(self,a_grade_book):\n", + " raise NotImplementedError\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "KQc52jrSNXyI", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class grade_book(data):\n", + " # New member class to hold arbitrary data associated with the class\n", + "\n", + " __data=dict()\n", + " __students=dict()\n", + " \n", + " def __init__(self,name):\n", + " data.__init__(self,name+\" Course Grade Book\")\n", + " self.__students=dict()\n", + " self.__data=dict()\n", + " \n", + " # New method to access data\n", + " def __getitem__(self,key):\n", + " return self.__data[key]\n", + " \n", + " # New method to add data\n", + " def __setitem__(self, key, value):\n", + " self.__data[key] = value\n", + " \n", + " def add_student(self,a_student):\n", + " self.__students[a_student.id_number()]=a_student\n", + "\n", + " # New method to allow iterating over students\n", + " def get_students(self):\n", + " return self.__students\n", + " \n", + " def assign_grade(self,key,a_grade):\n", + " the_student=None\n", + " try:\n", + " the_student=self.__students[key]\n", + " except:\n", + " for id in self.__students:\n", + " if key == self.__students[id].name():\n", + " the_student=self.__students[id]\n", + " break\n", + " if the_student:\n", + " the_student.add_grade(a_grade)\n", + " else:\n", + " print (self.name()+\" Error: Did not find student.\")\n", + " \n", + " def apply_calculator(self,a_calculator,**kwargs):\n", + " a_calculator.apply(self,**kwargs)\n", + " \n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VZE4-eNnNXyM", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class uncurved_letter_grade_percent(calculator):\n", + " __grades_definition=[ (.97,\"A+\"),\n", + " (.93,\"A\"),\n", + " (.9,\"A-\"),\n", + " (.87,\"B+\"),\n", + " (.83,\"B\"),\n", + " (.8,\"B-\"),\n", + " (.77,\"C+\"),\n", + " (.73,\"C\"),\n", + " (.7,\"C-\"),\n", + " (.67,\"C+\"),\n", + " (.63,\"C\"),\n", + " (.6,\"C-\"),\n", + " (.57,\"F+\"),\n", + " (.53,\"F\"),\n", + " (0.,\"F-\")]\n", + " __max_grade=100.\n", + " __grade_name=str()\n", + " \n", + " def __init__(self,grade_name,max_grade=100.):\n", + " self.__max_grade=max_grade\n", + " self.__grade_name=grade_name\n", + " calculator.__init__(self,\n", + " \"Uncurved Percent Based Grade Calculator \"+self.__grade_name+\" Max=\"+str(self.__max_grade))\n", + " \n", + " def apply(self,a_grade_book,grade_name=None,**kwargs):\n", + " if grade_name:\n", + " pass\n", + " else:\n", + " grade_name=self.__grade_name\n", + " \n", + " \n", + " for k,a_student in a_grade_book.get_students().iteritems():\n", + " a_grade=a_student[grade_name]\n", + "\n", + " if not a_grade.numerical():\n", + " print (self.name()+ \" Error: Did not get a numerical grade as input.\")\n", + " raise Exception\n", + " \n", + " percent=a_grade.value()/self.__max_grade\n", + " \n", + " for i,v in enumerate(self.__grades_definition):\n", + " if percent>=v[0]:\n", + " break\n", + " \n", + " a_student.add_grade(grade(grade_name+\" Letter\",value=self.__grades_definition[i][1]))\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "rY8nqVj4NXyP", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class mean_std_calculator(calculator):\n", + " def __init__(self):\n", + " calculator.__init__(self,\"Mean and Standard Deviation Calculator\")\n", + " \n", + " def apply(self,a_grade_book,grade_name,**kwargs):\n", + " grades=list()\n", + " for k,a_student in a_grade_book.get_students().iteritems():\n", + " grades.append(a_student[grade_name].value())\n", + " \n", + " a_grade_book[grade_name+\" Mean\"] = np.mean(grades)\n", + " a_grade_book[grade_name+\" STD\"] = math.sqrt(np.var(grades))\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "id": "PznEbSmXNXyS", + "colab_type": "text" + }, + "source": [ + "## CSV Reader\n", + "\n", + "*Exercise 1*: The data for a class are stored in a \"camma separated values\" (CSV) file name `Data1401-Grades.csv` in the directory of this lab. You can see the contents using the `cat` shell command:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xda0YcqfNXyT", + "colab_type": "code", + "outputId": "01314d96-ada7-4ce6-e420-eb28b1c80065", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 326 + } + }, + "source": [ + "!cat Data1401-Grades.csv " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "l1_n,l1_1,12_n,l2_1,l2_2,l2_3,l2_4,l2_5,l2_6,l2_7,l3_n,l3_1,l3_2,l3_3,l3_4,l3_5,l3_6,l3_7,l3_8,l3_9,l3_10,l3_11,l3_12,l3_13,l3_14,l4_n,l4_1,l4_2,l4_3,l4_4,l4_5,l4_6,l4_7,l4_8,l4_9,l4_10,l4_11,q1_n,q1_1,e1_n,e1_1,e1_2,e1_3,e1_4,e1_5,e1_6,e1_7,e1_8,e1_9,e1_10,e1_11,e1_12,e1_13,e1_14,e1_15\r\n", + "1,10,7,0,10,10,8,10,10,10,14,9,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0,1,9.5,15,9,9,0,9,8,0,0,0,0,0,0,0,0,0,0\r\n", + "1,10,7,0,0,0,0,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0,1,0,15,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\r\n", + "1,10,7,0,0,0,0,0,0,0,14,9,10,10,10,7,10,3,6,3,3,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0,1,5,15,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0\r\n", + "1,10,7,10,10,3,9.5,10,10,9.5,14,10,10,10,8,5,10,5,10,3,0,10,3,10,8,11,10,10,10,10,10,10,0,0,10,5,0,1,10,15,9,9,10,9,7,9,0,0,10,10,9,5,10,8,10\r\n", + "1,10,7,10,10,9.5,0,10,10,0,14,9.5,0,0,10,0,10,5,10,7,0,10,6,10,0,11,10,10,6,0,0,0,0,0,0,0,0,1,0,15,0,0,0,0,5,0,7,0,3,3,3,0,3,0,0\r\n", + "1,10,7,10,10,10,9.5,10,10,9.5,14,5,9.5,9.5,8,10,10,8,10,8,0,5,6,0,0,11,0,10,10,10,0,5,0,0,0,0,0,1,9.5,15,9,9,10,9,9,10,7,0,9,9,9,0,5,0,0\r\n", + "1,10,7,10,10,0,5,10,10,9.5,14,9.5,10,10,8,10,8,9,0,0,0,0,0,0,0,11,0,10,10,0,0,10,0,0,0,0,0,1,10,15,9,9,10,9,0,0,0,0,0,0,0,0,0,0,0\r\n", + "1,10,7,10,10,10,9.5,10,10,9.5,14,10,10,10,10,0,0,0,0,0,0,0,0,0,0,11,10,10,10,10,3,3,0,0,5,0,0,1,10,15,9,9,10,0,10,0,7,5,9,9,9,0,0,0,0\r\n", + "1,10,7,0,10,9.5,0,10,10,0,14,10,10,10,10,0,0,0,0,0,0,0,0,0,0,11,10,10,10,10,5,3,0,3,10,7,0,1,9.5,15,9,9,10,5,10,0,9,9,9,9,9,10,5,0,0\r\n", + "1,10,7,10,10,0,10,10,10,10,14,10,6,10,0,0,0,0,0,0,0,0,0,0,0,11,10,10,0,7,0,0,0,0,0,0,0,1,9.5,15,9,9,10,9,5,9,7,9,10,10,10,5,10,5,0\r\n", + "1,10,7,10,10,0,0,10,10,7,14,10,10,10,10,7,10,6,3,10,10,10,10,10,10,11,10,10,10,10,10,5,10,10,10,10,10,1,0,15,9,9,9,9,9,10,9,9,10,10,10,10,10,5,10\r\n", + "1,10,7,10,10,9.5,9.5,10,10,9.5,14,9.5,10,10,10,8,10,8,10,10,7,5,0,0,0,11,10,10,10,10,5,6,0,0,0,0,0,1,10,15,9,9,10,9,8,9,7,9,10,10,10,10,0,0,0\r\n", + "1,10,7,10,10,5,9.5,10,10,9.5,14,5,9,9,10,7,10,10,10,10,7,10,3,5,10,11,0,0,0,0,0,0,0,0,0,0,0,1,10,15,9,9,9,8,7,10,0,9,10,9,10,9,5,0,0\r\n", + "1,10,7,10,10,9.5,0,10,10,0,14,9.5,10,10,10,10,10,10,10,0,0,10,5,10,10,11,0,10,10,0,0,5,0,0,0,0,0,1,0,15,9,9,10,0,8,9,7,9,10,10,10,10,10,0,0\r\n", + "1,10,7,10,10,9.5,9,10,10,9.5,14,10,10,10,10,10,10,9,10,3,0,3,3,5,2,11,0,0,0,0,0,0,0,0,0,0,0,1,0,15,9,9,10,5,5,0,0,10,10,10,10,0,10,5,10\r\n", + "1,10,7,10,10,3,7,10,10,9,14,10,10,10,10,0,10,9,10,7,7,3,7,5,8,11,10,10,10,8,5,3,0,0,7,0,0,1,9.5,15,9,9,10,10,7,10,10,10,10,10,10,10,9,8,2" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Yhrsv9dHNXyW", + "colab_type": "text" + }, + "source": [ + "You will note that the first line has the names of the \"columns\" of data, and that subsequent lines (or \"rows\") have the data for each student, separated by cammas.\n", + "\n", + "Recalling that in lecture we created a file reader, create a CSV reader function that takes a filename as input and returns data structure(s) that store the data in the file. Note that you are not allowed to use a library. The point here is for *you* to write the CSV reader. Some options for your data structures (pick one):\n", + "\n", + "* A list of dictionaries, where each element of the list is corresponds to a row of data and the dictionaries are keyed by the column name. For example `data[5][\"l3_5\"]` corresponds to the 6th student's grade on lab 3 question 5.\n", + "\n", + "* A list of lists (i.e. a 2-D array or matrix) and a dictionary, where each element of the \"matrix\" corresponds to a a specific grade for a specific student and the dictionary maps the name of the column to the column index. For example `data[5][column_names[\"l1_5\"]]` corresponds to the 6th student's grade on lab 3 question 5.\n", + "\n", + "* A dictionary of lists, where each element of the dictionary corresponds to a column of data and the lists contain the data in that column. For example `data[\"l3_5\"][5]` corresponds to the 6th student's grade on lab 3 question 5.\n", + "\n", + "* (Extra Credit) A class that simultaneously supports all of the above methods." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NdbKQRAQNXyX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Your solution here.\n", + "\n", + "def csv_reader(filename):\n", + " f=open(filename,\"r\")\n", + " first_line = f.readline().rstrip()\n", + " fields=first_line.split(\",\")\n", + "\n", + " data=list()\n", + "\n", + " line = f.readline().rstrip()\n", + " while line:\n", + " data.append(line.split(\",\"))\n", + " line = f.readline().rstrip()\n", + " new_data=list()\n", + " for row in data:\n", + " new_data.append(dict(list(zip(fields,row))))\n", + " return new_data" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "D7ig_ZwSQ2LI", + "colab_type": "code", + "outputId": "774595c9-6f44-4054-ea84-18c59fd8174d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + } + }, + "source": [ + "csv_reader(\"Data1401-Grades.csv\")" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[{'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '0',\n", + " 'e1_11': '0',\n", + " 'e1_12': '0',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '0',\n", + " 'e1_4': '9',\n", + " 'e1_5': '8',\n", + " 'e1_6': '0',\n", + " 'e1_7': '0',\n", + " 'e1_8': '0',\n", + " 'e1_9': '0',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '0',\n", + " 'l2_2': '10',\n", + " 'l2_3': '10',\n", + " 'l2_4': '8',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '10',\n", + " 'l3_1': '9',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '0',\n", + " 'l3_3': '0',\n", + " 'l3_4': '0',\n", + " 'l3_5': '0',\n", + " 'l3_6': '0',\n", + " 'l3_7': '0',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '0',\n", + " 'l4_3': '0',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '9.5',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '0',\n", + " 'e1_10': '0',\n", + " 'e1_11': '0',\n", + " 'e1_12': '0',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '0',\n", + " 'e1_3': '0',\n", + " 'e1_4': '0',\n", + " 'e1_5': '0',\n", + " 'e1_6': '0',\n", + " 'e1_7': '0',\n", + " 'e1_8': '0',\n", + " 'e1_9': '0',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '0',\n", + " 'l2_2': '0',\n", + " 'l2_3': '0',\n", + " 'l2_4': '0',\n", + " 'l2_5': '0',\n", + " 'l2_6': '0',\n", + " 'l2_7': '0',\n", + " 'l3_1': '0',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '0',\n", + " 'l3_3': '0',\n", + " 'l3_4': '0',\n", + " 'l3_5': '0',\n", + " 'l3_6': '0',\n", + " 'l3_7': '0',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '0',\n", + " 'l4_3': '0',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '0',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '5',\n", + " 'e1_10': '0',\n", + " 'e1_11': '0',\n", + " 'e1_12': '0',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '5',\n", + " 'e1_3': '5',\n", + " 'e1_4': '5',\n", + " 'e1_5': '0',\n", + " 'e1_6': '0',\n", + " 'e1_7': '0',\n", + " 'e1_8': '0',\n", + " 'e1_9': '0',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '0',\n", + " 'l2_2': '0',\n", + " 'l2_3': '0',\n", + " 'l2_4': '0',\n", + " 'l2_5': '0',\n", + " 'l2_6': '0',\n", + " 'l2_7': '0',\n", + " 'l3_1': '9',\n", + " 'l3_10': '3',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '7',\n", + " 'l3_6': '10',\n", + " 'l3_7': '3',\n", + " 'l3_8': '6',\n", + " 'l3_9': '3',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '0',\n", + " 'l4_3': '0',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '5',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '9',\n", + " 'e1_12': '5',\n", + " 'e1_13': '10',\n", + " 'e1_14': '8',\n", + " 'e1_15': '10',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '9',\n", + " 'e1_5': '7',\n", + " 'e1_6': '9',\n", + " 'e1_7': '0',\n", + " 'e1_8': '0',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '3',\n", + " 'l2_4': '9.5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '10',\n", + " 'l3_10': '0',\n", + " 'l3_11': '10',\n", + " 'l3_12': '3',\n", + " 'l3_13': '10',\n", + " 'l3_14': '8',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '8',\n", + " 'l3_5': '5',\n", + " 'l3_6': '10',\n", + " 'l3_7': '5',\n", + " 'l3_8': '10',\n", + " 'l3_9': '3',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '5',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '10',\n", + " 'l4_6': '10',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '10',\n", + " 'l4_n': '11',\n", + " 'q1_1': '10',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '0',\n", + " 'e1_10': '3',\n", + " 'e1_11': '3',\n", + " 'e1_12': '0',\n", + " 'e1_13': '3',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '0',\n", + " 'e1_3': '0',\n", + " 'e1_4': '0',\n", + " 'e1_5': '5',\n", + " 'e1_6': '0',\n", + " 'e1_7': '7',\n", + " 'e1_8': '0',\n", + " 'e1_9': '3',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '9.5',\n", + " 'l2_4': '0',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '0',\n", + " 'l3_1': '9.5',\n", + " 'l3_10': '0',\n", + " 'l3_11': '10',\n", + " 'l3_12': '6',\n", + " 'l3_13': '10',\n", + " 'l3_14': '0',\n", + " 'l3_2': '0',\n", + " 'l3_3': '0',\n", + " 'l3_4': '10',\n", + " 'l3_5': '0',\n", + " 'l3_6': '10',\n", + " 'l3_7': '5',\n", + " 'l3_8': '10',\n", + " 'l3_9': '7',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '6',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '0',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '9',\n", + " 'e1_11': '9',\n", + " 'e1_12': '0',\n", + " 'e1_13': '5',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '9',\n", + " 'e1_5': '9',\n", + " 'e1_6': '10',\n", + " 'e1_7': '7',\n", + " 'e1_8': '0',\n", + " 'e1_9': '9',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '10',\n", + " 'l2_4': '9.5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '5',\n", + " 'l3_10': '0',\n", + " 'l3_11': '5',\n", + " 'l3_12': '6',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '9.5',\n", + " 'l3_3': '9.5',\n", + " 'l3_4': '8',\n", + " 'l3_5': '10',\n", + " 'l3_6': '10',\n", + " 'l3_7': '8',\n", + " 'l3_8': '10',\n", + " 'l3_9': '8',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '0',\n", + " 'l4_6': '5',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '9.5',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '0',\n", + " 'e1_11': '0',\n", + " 'e1_12': '0',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '9',\n", + " 'e1_5': '0',\n", + " 'e1_6': '0',\n", + " 'e1_7': '0',\n", + " 'e1_8': '0',\n", + " 'e1_9': '0',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '0',\n", + " 'l2_4': '5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '9.5',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '8',\n", + " 'l3_5': '10',\n", + " 'l3_6': '8',\n", + " 'l3_7': '9',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '10',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '10',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '9',\n", + " 'e1_11': '9',\n", + " 'e1_12': '0',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '0',\n", + " 'e1_5': '10',\n", + " 'e1_6': '0',\n", + " 'e1_7': '7',\n", + " 'e1_8': '5',\n", + " 'e1_9': '9',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '10',\n", + " 'l2_4': '9.5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '10',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '0',\n", + " 'l3_6': '0',\n", + " 'l3_7': '0',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '3',\n", + " 'l4_6': '3',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '5',\n", + " 'l4_n': '11',\n", + " 'q1_1': '10',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '9',\n", + " 'e1_11': '9',\n", + " 'e1_12': '10',\n", + " 'e1_13': '5',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '5',\n", + " 'e1_5': '10',\n", + " 'e1_6': '0',\n", + " 'e1_7': '9',\n", + " 'e1_8': '9',\n", + " 'e1_9': '9',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '0',\n", + " 'l2_2': '10',\n", + " 'l2_3': '9.5',\n", + " 'l2_4': '0',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '0',\n", + " 'l3_1': '10',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '0',\n", + " 'l3_6': '0',\n", + " 'l3_7': '0',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '7',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '5',\n", + " 'l4_6': '3',\n", + " 'l4_7': '0',\n", + " 'l4_8': '3',\n", + " 'l4_9': '10',\n", + " 'l4_n': '11',\n", + " 'q1_1': '9.5',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '5',\n", + " 'e1_13': '10',\n", + " 'e1_14': '5',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '9',\n", + " 'e1_5': '5',\n", + " 'e1_6': '9',\n", + " 'e1_7': '7',\n", + " 'e1_8': '9',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '0',\n", + " 'l2_4': '10',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '10',\n", + " 'l3_1': '10',\n", + " 'l3_10': '0',\n", + " 'l3_11': '0',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '6',\n", + " 'l3_3': '10',\n", + " 'l3_4': '0',\n", + " 'l3_5': '0',\n", + " 'l3_6': '0',\n", + " 'l3_7': '0',\n", + " 'l3_8': '0',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '0',\n", + " 'l4_4': '7',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '9.5',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '10',\n", + " 'e1_13': '10',\n", + " 'e1_14': '5',\n", + " 'e1_15': '10',\n", + " 'e1_2': '9',\n", + " 'e1_3': '9',\n", + " 'e1_4': '9',\n", + " 'e1_5': '9',\n", + " 'e1_6': '10',\n", + " 'e1_7': '9',\n", + " 'e1_8': '9',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '0',\n", + " 'l2_4': '0',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '7',\n", + " 'l3_1': '10',\n", + " 'l3_10': '10',\n", + " 'l3_11': '10',\n", + " 'l3_12': '10',\n", + " 'l3_13': '10',\n", + " 'l3_14': '10',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '7',\n", + " 'l3_6': '10',\n", + " 'l3_7': '6',\n", + " 'l3_8': '3',\n", + " 'l3_9': '10',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '10',\n", + " 'l4_11': '10',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '10',\n", + " 'l4_6': '5',\n", + " 'l4_7': '10',\n", + " 'l4_8': '10',\n", + " 'l4_9': '10',\n", + " 'l4_n': '11',\n", + " 'q1_1': '0',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '10',\n", + " 'e1_13': '0',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '9',\n", + " 'e1_5': '8',\n", + " 'e1_6': '9',\n", + " 'e1_7': '7',\n", + " 'e1_8': '9',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '9.5',\n", + " 'l2_4': '9.5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '9.5',\n", + " 'l3_10': '7',\n", + " 'l3_11': '5',\n", + " 'l3_12': '0',\n", + " 'l3_13': '0',\n", + " 'l3_14': '0',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '8',\n", + " 'l3_6': '10',\n", + " 'l3_7': '8',\n", + " 'l3_8': '10',\n", + " 'l3_9': '10',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '10',\n", + " 'l4_5': '5',\n", + " 'l4_6': '6',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '10',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '9',\n", + " 'e1_11': '10',\n", + " 'e1_12': '9',\n", + " 'e1_13': '5',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '9',\n", + " 'e1_4': '8',\n", + " 'e1_5': '7',\n", + " 'e1_6': '10',\n", + " 'e1_7': '0',\n", + " 'e1_8': '9',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '5',\n", + " 'l2_4': '9.5',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '5',\n", + " 'l3_10': '7',\n", + " 'l3_11': '10',\n", + " 'l3_12': '3',\n", + " 'l3_13': '5',\n", + " 'l3_14': '10',\n", + " 'l3_2': '9',\n", + " 'l3_3': '9',\n", + " 'l3_4': '10',\n", + " 'l3_5': '7',\n", + " 'l3_6': '10',\n", + " 'l3_7': '10',\n", + " 'l3_8': '10',\n", + " 'l3_9': '10',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '0',\n", + " 'l4_3': '0',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '10',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '10',\n", + " 'e1_13': '10',\n", + " 'e1_14': '0',\n", + " 'e1_15': '0',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '0',\n", + " 'e1_5': '8',\n", + " 'e1_6': '9',\n", + " 'e1_7': '7',\n", + " 'e1_8': '9',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '9.5',\n", + " 'l2_4': '0',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '0',\n", + " 'l3_1': '9.5',\n", + " 'l3_10': '0',\n", + " 'l3_11': '10',\n", + " 'l3_12': '5',\n", + " 'l3_13': '10',\n", + " 'l3_14': '10',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '10',\n", + " 'l3_6': '10',\n", + " 'l3_7': '10',\n", + " 'l3_8': '10',\n", + " 'l3_9': '0',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '5',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '0',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '0',\n", + " 'e1_13': '10',\n", + " 'e1_14': '5',\n", + " 'e1_15': '10',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '5',\n", + " 'e1_5': '5',\n", + " 'e1_6': '0',\n", + " 'e1_7': '0',\n", + " 'e1_8': '10',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '9.5',\n", + " 'l2_4': '9',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9.5',\n", + " 'l3_1': '10',\n", + " 'l3_10': '0',\n", + " 'l3_11': '3',\n", + " 'l3_12': '3',\n", + " 'l3_13': '5',\n", + " 'l3_14': '2',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '10',\n", + " 'l3_6': '10',\n", + " 'l3_7': '9',\n", + " 'l3_8': '10',\n", + " 'l3_9': '3',\n", + " 'l3_n': '14',\n", + " 'l4_1': '0',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '0',\n", + " 'l4_3': '0',\n", + " 'l4_4': '0',\n", + " 'l4_5': '0',\n", + " 'l4_6': '0',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '0',\n", + " 'l4_n': '11',\n", + " 'q1_1': '0',\n", + " 'q1_n': '1'},\n", + " {'12_n': '7',\n", + " 'e1_1': '9',\n", + " 'e1_10': '10',\n", + " 'e1_11': '10',\n", + " 'e1_12': '10',\n", + " 'e1_13': '9',\n", + " 'e1_14': '8',\n", + " 'e1_15': '2',\n", + " 'e1_2': '9',\n", + " 'e1_3': '10',\n", + " 'e1_4': '10',\n", + " 'e1_5': '7',\n", + " 'e1_6': '10',\n", + " 'e1_7': '10',\n", + " 'e1_8': '10',\n", + " 'e1_9': '10',\n", + " 'e1_n': '15',\n", + " 'l1_1': '10',\n", + " 'l1_n': '1',\n", + " 'l2_1': '10',\n", + " 'l2_2': '10',\n", + " 'l2_3': '3',\n", + " 'l2_4': '7',\n", + " 'l2_5': '10',\n", + " 'l2_6': '10',\n", + " 'l2_7': '9',\n", + " 'l3_1': '10',\n", + " 'l3_10': '7',\n", + " 'l3_11': '3',\n", + " 'l3_12': '7',\n", + " 'l3_13': '5',\n", + " 'l3_14': '8',\n", + " 'l3_2': '10',\n", + " 'l3_3': '10',\n", + " 'l3_4': '10',\n", + " 'l3_5': '0',\n", + " 'l3_6': '10',\n", + " 'l3_7': '9',\n", + " 'l3_8': '10',\n", + " 'l3_9': '7',\n", + " 'l3_n': '14',\n", + " 'l4_1': '10',\n", + " 'l4_10': '0',\n", + " 'l4_11': '0',\n", + " 'l4_2': '10',\n", + " 'l4_3': '10',\n", + " 'l4_4': '8',\n", + " 'l4_5': '5',\n", + " 'l4_6': '3',\n", + " 'l4_7': '0',\n", + " 'l4_8': '0',\n", + " 'l4_9': '7',\n", + " 'l4_n': '11',\n", + " 'q1_1': '9.5',\n", + " 'q1_n': '1'}]" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 65 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rRxKQRpGNXyZ", + "colab_type": "text" + }, + "source": [ + "## Creating a Gradebook\n", + "\n", + "*Exercise 2:* In lecture we used pandas to read the CSV file and create the grade book. The example below works for the CSV file for this lab. Modify the code below to use your CSV reader instead." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3Wj5fq_1NXyZ", + "colab_type": "code", + "outputId": "7f685d65-3fd5-4c7e-d56b-ff3a88e3a463", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 231 + } + }, + "source": [ + "import pandas as pd\n", + "#class_data=pd.read_csv(\"Data1401-Grades.csv\")\n", + "class_data=csv_reader(\"Data1401-Grades.csv\")\n", + "a_grade_book=grade_book(\"Data 1401\")\n", + "\n", + "for student_i in range(60):\n", + " a_student_0=student(\"Student\",str(student_i),student_i)\n", + "\n", + " for k in class_data.keys():\n", + " a_student_0.add_grade(grade(k,value=class_data[k][student_i]))\n", + "\n", + " a_grade_book.add_student(a_student_0)\n", + " " + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "error", + "ename": "AttributeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0ma_student_0\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstudent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Student\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstudent_i\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mstudent_i\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mclass_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0ma_student_0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_grade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgrade\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mclass_data\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mstudent_i\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'keys'" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3ZHdkb52NXyb", + "colab_type": "text" + }, + "source": [ + "## Grade Summing\n", + "\n", + "*Exercise 3:* In lectre we will change the design of our algorithm classes and then update the `uncurved_letter_grade_percent` calculator. In lecture we also created a `grade_summer` calcuator that takes a prefix (for example `e1_` and a number `n`) and sums all grades starting with that prefix up to `n` and creates a new sum grade. Update this calculator (below) to the new design of our algorithm classes. Test your updated calculator by using it to sum the grades for all labs, quizzes, and exams of each student." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "H-NNqAmiNXyc", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Note this is the OLD design... you will need to modify it.\n", + "\n", + "class summary_calculator(alg): \n", + " def __init__(self,name):\n", + " alg.__init__(self,name)\n", + "\n", + " def apply(self,a_student):\n", + " raise NotImplementedError\n", + "\n", + "class grade_summer(summary_calculator):\n", + " def __init__(self,prefix,n):\n", + " self.__prefix=prefix\n", + " self.__n=n\n", + " summary_calculator.__init__(self,\"Sum Grades\")\n", + " \n", + " def apply(self,a_student):\n", + " labels=[self.__prefix+str(x) for x in range(1,self.__n)]\n", + " \n", + " grade_sum=0.\n", + " for label in labels:\n", + " grade_sum+=a_student[label].value()\n", + "\n", + " a_student.add_grade(grade(self.__prefix+\"sum\",value=grade_sum))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "59G04SJWNXye", + "colab_type": "text" + }, + "source": [ + "## Curving Grades\n", + "\n", + "*Exercise 4:* Use the `mean_std_calculator` above to calculate the mean and standard deviation for every lab, quiz, and exam in the class. Add a new print function to the `grade_book` class to print out such information in a nice way, and use this function to show your results.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "E6nzMm5ZNXyf", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Your solution here\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b4p-xSMLNXyi", + "colab_type": "text" + }, + "source": [ + "*Exercise 5:* In lecture we will change the design of our algorithms classes and then update the `uncurved_letter_grade_percent` calculator. Do the same for the `curved_letter_grade` calculator below and by curving all the lab, quiz, and exam grades." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SfPb8ZulNXyi", + "colab_type": "code", + "colab": {} + }, + "source": [ + "class curved_letter_grade(grade_calculator):\n", + " __grades_definition=[ (.97,\"A+\"),\n", + " (.93,\"A\"),\n", + " (.9,\"A-\"),\n", + " (.87,\"B+\"),\n", + " (.83,\"B\"),\n", + " (.8,\"B-\"),\n", + " (.77,\"C+\"),\n", + " (.73,\"C\"),\n", + " (.7,\"C-\"),\n", + " (.67,\"C+\"),\n", + " (.63,\"C\"),\n", + " (.6,\"C-\"),\n", + " (.57,\"F+\"),\n", + " (.53,\"F\"),\n", + " (0.,\"F-\")]\n", + " __max_grade=100.\n", + " __grade_name=str()\n", + " \n", + " def __init__(self,grade_name,mean,std,max_grade=100.):\n", + " self.__max_grade=max_grade\n", + " self.__mean=mean\n", + " self.__std=std\n", + " self.__grade_name=grade_name\n", + " grade_calculator.__init__(self,\n", + " \"Curved Percent Based Grade Calculator \"+self.__grade_name+ \\\n", + " \" Mean=\"+str(self.__mean)+\\\n", + " \" STD=\"+str(self.__std)+\\\n", + " \" Max=\"+str(self.__max_grade))\n", + " \n", + "\n", + " def apply(self,a_grade):\n", + " if not isinstance(a_grade,grade):\n", + " print (self.name()+ \" Error: Did not get an proper grade as input.\")\n", + " raise Exception\n", + " if not a_grade.numerical():\n", + " print (self.name()+ \" Error: Did not get a numerical grade as input.\")\n", + " raise Exception\n", + " \n", + " # Rescale the grade\n", + " percent=a_grade.value()/self.__max_grade\n", + " shift_to_zero=percent-(self.__mean/self.__max_grade)\n", + " scale_std=0.1*shift_to_zero/(self.__std/self.__max_grade)\n", + " scaled_percent=scale_std+0.8\n", + " \n", + " for i,v in enumerate(self.__grades_definition):\n", + " if scaled_percent>=v[0]:\n", + " break\n", + " \n", + " return grade(self.__grade_name,value=self.__grades_definition[i][1])\n", + " " + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "e9k7ljGUNXyk", + "colab_type": "text" + }, + "source": [ + "## Final Course Grade\n", + "\n", + "*Exercise 6:* Write a new calculator that sums grades with a prefix, as in the `grade_summer` calculator, but drops `n` lowest grades. Apply the algorithm to drop the lowest lab grade in the data.\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dOeRcpQ1NXyk", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Your solution here" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sPIafA-lNXym", + "colab_type": "text" + }, + "source": [ + "*Exercise 7*: Write a new calculator that creates a new letter grade based on a weighted average of letter grades, by assigning the following numerical values to letter grades:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LprJkKXDNXym", + "colab_type": "code", + "colab": {} + }, + "source": [ + "GradeMap={\"A+\":12,\n", + " \"A\":11,\n", + " \"A-\":10,\n", + " \"B+\":9,\n", + " \"B\":8,\n", + " \"B-\":7,\n", + " \"C+\":6,\n", + " \"C\":5,\n", + " \"C-\":4,\n", + " \"D+\":3,\n", + " \"D\":2,\n", + " \"D-\":1,\n", + " \"F\":0}" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UZTNC6bXNXyo", + "colab_type": "text" + }, + "source": [ + "Test you calculator by applying the weights from the syllabus of this course and computing everyone's grade in the course." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9Nd6fe-WNXyp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Your solution here" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file