{ "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", "from contextlib import redirect_stdout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instructions\n", "\n", "- Complete all the problems below by replacing the two-line sequence of # YOUR CODE HERE and `raise NotImplementedError()` with your solution code.\n", "- All functions that you define for homework **must have docstrings** or you will lose a point **per function**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** Write a function that returns the square of the number $n$, if $n\\geq 1$, and returns 0 otherwise. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "5e42f399b8d273434ba59e5453abd8ad", "grade": false, "grade_id": "cell-bfb578e126c1b55f", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "def square(n):\n", " \"\"\"Compute the square of the number n, or return 0 if less than 1.\n", " \"\"\"\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "3ba131e8db23efb35595e5b5ade2bd19", "grade": true, "grade_id": "cell-c6379a4803420748", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "assert square(10) == 100\n", "\"\"\"Check that square returns the correct output for several inputs\"\"\"\n", "assert square(1) == 1\n", "assert square(2) == 4\n", "assert square(11) == 121\n", "\"\"\"Check that square returns None for invalid inputs\"\"\"\n", "assert square(-1) == 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Using your `square` function, write a function that computes the sum of the squares of the numbers from 1 to $n$. Your function should call the `square` function -- it should NOT reimplement its functionality." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1f268b5ba7c157b7b41736ebfb63950d", "grade": false, "grade_id": "cell-18bc2f28fbcc3eba", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "def sum_of_squares(n):\n", " \"\"\"Compute the sum of the squares of numbers from 1 to n.\"\"\"\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0dd297fbfe941d271c7576ed7489ddaa", "grade": true, "grade_id": "cell-1706850a8a0c3286", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "\"\"\"Check that sum_of_squares returns the correct answer for various inputs.\"\"\"\n", "assert sum_of_squares(1) == 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "90cbfae7f442547a3161036a518f1518", "grade": true, "grade_id": "cell-2fd1654ce0176d53", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "f082db7a1048bf50e424f9b0c64953e6", "grade": true, "grade_id": "cell-4db891d69ac65ac4", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "\"\"\"Check that sum_of_squares relies on square.\"\"\"\n", "assert True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Write a function `sum_of_evens` that only adds up the even numbers between 0 and the number $n$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "7bba0d4f8b711ea711f3b6f03024f760", "grade": false, "grade_id": "cell-9416b535d19fc949", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "def sum_of_evens(n):\n", " \"\"\"Compute the sum of the even numbers from 0 to n.\"\"\"\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a1b5e19f22dc15590fbe0ded58dd27e0", "grade": true, "grade_id": "cell-d9c5a7f8882a8e91", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "\"\"\"Check that sum_of_evens returns the correct answer for various inputs.\"\"\"\n", "assert sum_of_evens(1) == 0\n", "assert sum_of_evens(5) == 6\n", "assert sum_of_evens(10) == 30\n", "assert sum_of_evens(11) == 30" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4** Describe a **different** way to implement `sum_of_evens()`." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "7e8955af37a0a6ddcfa00619bbab6873", "grade": true, "grade_id": "cell-0083b8d23a14fc37", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5** Use three *different* ways to create three lists with the integer numbers 1 through 10, inclusive. Use the variable names `myList1`, `myList2`, and `myList3` to hold the results." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "6cd645dfff802a1a5903074a6a4bd5da", "grade": false, "grade_id": "cell-38d713dddb2b5cfe", "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": "1f336035240f7d13479b1d1e0e8b88cd", "grade": true, "grade_id": "cell-37f90a8bf6056a36", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q6** Write a function:\n", "\n", " mean(data)\n", " \n", "that takes a list of numbers given by `data` and computes and returns the arithmetic mean of the values in the list. Your function should work properly even if `data` is an empty list, and should return 0 in that case." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "cccac96272e3fc4ad010112f12f8c151", "grade": false, "grade_id": "cell-a32002531e280bd3", "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": "4e315a09750398244bb0f65defeb26e7", "grade": true, "grade_id": "cell-ef96cd921bb7b67b", "locked": true, "points": 2, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "list1 = [3, 4, 7, 2]\n", "assert mean(list1) == 4\n", "list2 = []\n", "assert mean(list2) == 0" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "2779f959a1baf1255baa68477a42d509", "grade": false, "grade_id": "cell-5511cc04854c54c9", "locked": true, "schema_version": 3, "solution": false } }, "source": [ "**Q7** For the following questions, each answer should consist of a single\n", "`print` with a single argument--an expression involving the list `data`, which, for these questions, references the list `[32, 42, 11, 15, 58, 44, 16]`.\n", "\n", "Leave the code we give you \"as is\", and your print should be indented so that your solution becomes the body of the `with`.\n", "\n", "Print a list that includes all elements *except* the first element." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9f9b480786c25427f9ebdbeb54bd4f26", "grade": false, "grade_id": "cell-8f7be805bddd4762", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "data = [32, 42, 11, 15, 58, 44, 16]\n", "\n", "result = io.StringIO()\n", "with redirect_stdout(result):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "3ad35bb327d4ca83591a2ab8a68aea1e", "grade": true, "grade_id": "cell-69e005cd46307fdb", "locked": true, "points": 1, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result.getvalue() == \"[42, 11, 15, 58, 44, 16]\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q8** Print the length of the list `data`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "f5063869d4a77795bc5e0141d4ed7963", "grade": false, "grade_id": "cell-20ff0e0c86a745c9", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "data = [32, 42, 11, 15, 58, 44, 16]\n", "\n", "result = io.StringIO()\n", "with redirect_stdout(result):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0eadd5de442038e4af5b62f84ae091dd", "grade": true, "grade_id": "cell-a1a0a273991fb0a4", "locked": true, "points": 1, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result.getvalue() == \"7\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q9** Print the third element of the list `data`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "63fcecabb20f813e5179b4b20a10959a", "grade": false, "grade_id": "cell-2769c568b2695d86", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "data = [32, 42, 11, 15, 58, 44, 16]\n", "\n", "result = io.StringIO()\n", "with redirect_stdout(result):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "b4f59dd4de30210040f675836b8fe353", "grade": true, "grade_id": "cell-0f56edb5b71e8344", "locked": true, "points": 1, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result.getvalue() == \"11\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q10** Print the last element of the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "32dd0a459a25ac06e4e6e3f7d4112a35", "grade": false, "grade_id": "cell-30bbcffba0b8be8d", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "data = [32, 42, 11, 15, 58, 44, 16]\n", "\n", "result = io.StringIO()\n", "with redirect_stdout(result):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "a28e9a9d5fecf87f30035c358352f22b", "grade": true, "grade_id": "cell-35bde65789e0106e", "locked": true, "points": 1, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result.getvalue() == \"16\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q11** Print a list containing the last four elements of `data`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "e57032807721d3a1bde7d2aab90e989c", "grade": false, "grade_id": "cell-ff997c6b4a2fc5eb", "locked": false, "schema_version": 3, "solution": true } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "data = [32, 42, 11, 15, 58, 44, 16]\n", "\n", "result = io.StringIO()\n", "with redirect_stdout(result):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "fa7af4bdae82f3ce11c73717e94debf1", "grade": true, "grade_id": "cell-b59a34979e5d93d7", "locked": true, "points": 1, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert result.getvalue() == \"[15, 58, 44, 16]\\n\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q12** Write a function\n", "\n", " decreaseList(L, amount)\n", "\n", "that, for each number in list L, decreases L by `amount`. The list should be modified in place, and the function should not create a new list, nor return any value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9a57ef6538233778e5c59ddd15aed799", "grade": false, "grade_id": "cell-2370a7c1bbe407fc", "locked": false, "schema_version": 3, "solution": true } }, "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": "4a8823d466ac8eb4519e2cd15d905b5a", "grade": true, "grade_id": "cell-012b8ea5dfc08243", "locked": true, "points": 2, "schema_version": 3, "solution": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "L = [0.65, 0.75, 0.85]\n", "decreaseList(L, 0.01)\n", "assert L == [0.64, 0.74, 0.84]\n", "\n", "data = [3,4,5]\n", "decreaseList(amount = 1, L = data)\n", "assert data == [2,3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q13** Write a function `swap_case(s)` that swap cases in the string `s`. In other words, convert all lowercase letters to uppercase letters and vice versa. Do not use the `swapcase()` method. Instead, use `ord()` and `chr()`. For example, if `s = \"My string\"` you return `\"mY STRING\"`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "30bc8cfd8da0ad88c274934e6125500b", "grade": false, "grade_id": "cell-9e32228bd18a25d0", "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": "69247676880a5877e6e02ce14411ed32", "grade": true, "grade_id": "cell-3b660b9b6f4b7390", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert swap_case(\"Lorem Ipsum\") == \"lOREM iPSUM\"\n", "assert swap_case(\"WikiPedia\") == \"wIKIpEDIA\"\n", "assert swap_case(\"The quick Brown fOX jumped Over THE laZy doG\") == \"tHE QUICK bROWN Fox JUMPED oVER the LAzY DOg\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q14** Implement a function\n", "\n", " insertElement(myList, e, i)\n", " \n", "that inserts an element `e` at index `i` of `myList` in place." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "df11901c037dcb2008aed9fb177c1e8d", "grade": false, "grade_id": "cell-453c1aac75de2844", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "def insertElement(myList, e, i):\n", " \"\"\"Inserts an element e into index i of myList\n", " \n", " \"\"\"\n", " # YOUR CODE HERE\n", " raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "d6c623fe9c4acd8123800460dcb71eb1", "grade": true, "grade_id": "cell-b8fd5f38b0641496", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "myList =[1, 2, 3, 4]\n", "assert insertElement(myList, 5, 0) == None\n", "assert myList[0] == 5\n", "insertElement(myList, 6, 1)\n", "assert myList[0] == 5\n", "assert myList[1] == 6\n", "insertElement(myList, 7, len(myList))\n", "assert len(myList) == 7\n", "assert myList[-1] == 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q15** Implement a function `findElement(myList, e)` that returns the index of the first occurence of element `e` in `myList`. Do not use any built-in functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "27741ea649981c03ff31c0d65114071d", "grade": false, "grade_id": "cell-061721e509002e48", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "9e80989153642f161cc80bc508ec3530", "grade": true, "grade_id": "cell-d5fd301307dccd31", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "myList = [3, 7]\n", "assert findElement(myList, 7) == 1\n", "assert findElement(myList, 14) == None" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }