{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Denison CS181/DA210 Homework\n", "\n", "Before you turn this problem in, make sure everything runs as expected. This is a combination of **restarting the kernel** and then **running all cells** (in the menubar, select Kernel$\\rightarrow$Restart And Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import os.path\n", "import io\n", "import sys\n", "import string\n", "import math\n", "from contextlib import redirect_stdout\n", "\n", "datadir = \"publicdata\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following set of questions involve the `sorted()` built-in Python function and the list of tuples:\n", "\n", " students = [('bob', 'B', 19), ('chris', 'B', 18), \n", " ('chris', 'A', 18), ('alice', 'A', 19)]\n", "\n", "Documentation for `sorted()` may be found at \n", "\n", "- https://docs.python.org/3/library/functions.html#sorted" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "ff2fae613cce06587db16dbe57ef6c6b", "grade": false, "grade_id": "cell-4281156142386fd9", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "students = [('bob', 'B', 19), ('chris', 'B', 18), ('chris', 'A', 18), ('alice', 'A', 19)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** What is returned when `sorted()` is invoked on the `students` list? In this case, we are sorting a list whose elements are tuples. Describe how the algorithm used by `sorted()` works in the case that two elements are equal in their first element." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "93ef77c0230cc6c664eee38a22b7b518", "grade": true, "grade_id": "cell-2d58e657adf85d40", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Look at the documentation for `sorted()`. If we wanted to sort the `students` list based on the second element of the tuple (the grade), we want a function that is used to extract the \"key\" from a list element (tuple) to retrieve that second element. Write a function\n", "\n", " getGrade(record)\n", "\n", "that, given a student record that consists of a 3-tuple, accesses and returns the middle element of the record/tuple. You are welcome to define `getGrade` as a lambda function. With this `getGrade` function object passed as the `key` parameter, call `sorted()` and assign the result to the variable `gradeSorted`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "037d508e81cb541af4dc4ffe61ede5eb", "grade": false, "grade_id": "cell-06893f6918eb7e27", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print(gradeSorted)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e8493890299d8b35f6de02fb0a9e394a", "grade": true, "grade_id": "cell-0323a68fb56e401b", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert getGrade(('alice', 'A', 18)) == 'A'\n", "assert getGrade(('chris', 'B', 18)) == 'B'\n", "assert isinstance(gradeSorted, list)\n", "assert gradeSorted[0] == ('chris', 'A', 18)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Consider your result: when there are ties in the \"key\", do you get the elements involved in the tie in the same order as in the original list, or in different orders?" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "35b87cd4ad5c75c0cebdbfcc67cef530", "grade": true, "grade_id": "cell-d2d1f9d66c965c50", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4** Write a function\n", "\n", " whichIsBigger(f1,f2,n)\n", "\n", "which computes `f1(n)` and `f2(n)`, and returns whichever function (`f1` or `f2`) results in the larger value when an integer `n` is passed as a parameter. In case of a tie, return `f1`. Test with `f1(x)` defined as $x^3$ and `f2(x)` defined as $5x^2+4$ and using $n=2$. Test again with $n=100$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "d3fee1679fb575a0e3d103d47c10380e", "grade": false, "grade_id": "cell-76bddeeaf8ffa2ff", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "def whichIsBigger(f1,f2,n):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()\n", "\n", "def f1(x):\n", " return x**3\n", "\n", "def f2(x):\n", " return 5*x*x + 4\n", "\n", "print(whichIsBigger(f1,f2,2))\n", "print(whichIsBigger(f1,f2,100))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "78ff602a79283fd0ce958e3264967e5d", "grade": true, "grade_id": "cell-95a4c27fc69bb970", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "def f1(x):\n", " return x**3 - 7\n", "\n", "def f2(x):\n", " return 5*x*x + 4*x + 2\n", "\n", "assert whichIsBigger(f1,f2,0) == f2\n", "assert whichIsBigger(f1,f2,-10) == f2\n", "assert whichIsBigger(f1,f2,2) == f2\n", "assert whichIsBigger(f1,f2,50) == f1\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5** Write a lambda expression and assign it to the variable `evensquared` such that it squares the value of its single argument if the argument is even, and *cubes* the argument if it is odd.\n", "\n", "Afterward, for example, expression `evensquared(5)` should yield `125`, while `evensquared(4)` should yield `16`.\n", "\n", "Hint: We need an *expression* to handle an `if` type condition" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "618b145b6a9d07a044edd76672873927", "grade": false, "grade_id": "cell-260b5598d856ee88", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "80afe6f8dd718ea94cc38f61b5a87682", "grade": true, "grade_id": "cell-a34541c503ff5a43", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert(evensquared(1)==1)\n", "assert(evensquared(2)==4)\n", "assert(evensquared(3)==27)\n", "assert(evensquared(4)==16)\n", "assert(evensquared(0)==0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q6** Write a lambda function that takes three parameters and multiplies them together. Assign the lambda function expression to `mult3`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7f9b9657eb38096c504f43cd764ad059", "grade": false, "grade_id": "cell-2f45e6d4eb6451d7", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "mult3(10,12, 9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "934185981c65efce8ace1b1b7c37a479", "grade": true, "grade_id": "cell-2657c0facf794201", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert mult3(10, 12, 9) == 1080" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q7** The formula for the volume of a barrel is as follows:\n", "\n", "$$\n", "V={\\pi}h(2D^2+d^2)/12\n", "$$\n", "\n", "Where $h$=height, $D$=middle radius, and $d$=top or bottom radius. \n", "\n", "Write a lambda expression assigned to `vol` so that the resulting function, `vol(h,D,d)` computes the volume of a barrel. Use `math.pi` in your computations, not a hard-coded value for $\\pi$. Be careful that your Python precedence follows the precedence of the mathematical formula." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "d359c4cff4cc54a87c0b86480796c237", "grade": false, "grade_id": "cell-e0e2b9ed918c28e3", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "import math\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "d0add37351bd79238ffc623c6b415fb9", "grade": true, "grade_id": "cell-aa6e2eb76e11770a", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert(238.5