{ "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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q1** Use a list comprehension to write a function \n", "\n", " mult_by_2(data)\n", "\n", "that multiplies each item in `data` by 2, returning the resulting list. For example, `mult_by_2([1,2,3])` returns `[2,4,6]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "0caee3c1c6b62b16673b9edbc0ba4d67", "grade": false, "grade_id": "cell-75d78706c6b457bf", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "66259b5a5c76b89de690943591faf5f2", "grade": true, "grade_id": "cell-54db22cae09e3c07", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert mult_by_2([1]) == [2]\n", "assert mult_by_2([1, 2]) == [2, 4]\n", "assert mult_by_2([1,2,3,4,5,6,7,8,9,10]) == [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n", "assert mult_by_2([]) == []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function\n", "\n", " add2odd(data)\n", "\n", "that returns a new list based on `data` by including and adding 2 to every number that is odd, and leaving out any even numbers. Use a list comprehension with a Boolean condition. So, for instance `add2odd([1,2,3,4,5])` results in the list `[3 , 5, 7]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "cfdcfda5558e13c87baf99901e467b64", "grade": false, "grade_id": "cell-e45bf61a17020be8", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "add2odd([1,2,3,4,5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "ad44ed11c2fd741ad21ec5a37a15793d", "grade": true, "grade_id": "cell-f1afbb66fb298a2f", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert add2odd([]) == []\n", "assert add2odd([1,2,3,4,5]) == [3, 5, 7]\n", "assert add2odd([2,4,18,20,100,6]) == []\n", "assert add2odd([1, 1, 1, 1, 1]) == [3, 3, 3, 3, 3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Write a function\n", "\n", " get_tuples(data)\n", "\n", "that uses a list comprehension to extract a list of tuples based on `data`, where the first item of each tuple is the index of the data, and the second item is the corresponding item in `data`. Hint: there is a very useful Python built-in function for helping with this task, discussed in the reading. For example, if `data = [\"a\", \"b\", \"c\"]` you return `[(0, 'a'), (1, 'b'), (2, 'c')]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "d1efcba35a02c378152b05a1b057869a", "grade": false, "grade_id": "cell-b4370e23ea760754", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "8c3869405de61dc7f0bc32b0744a4a9e", "grade": true, "grade_id": "cell-40e9d187d35a6b50", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert get_tuples([]) == []\n", "assert get_tuples(['a', 'b', 'c', 'd', 'e']) == [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]\n", "assert get_tuples([0, 1, 2]) == [(0, 0), (1, 1), (2, 2)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q4** Write a function\n", "\n", " remove(data, value)\n", "\n", "that returns a new list that contains the same elements as the list `data` except for those that equal `value`. Use a list comprehension. Your function should remove all items equal to value. For example, `remove([3, 1, 5, 3, 9], 3)` should return the list `[1, 5, 9]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c1e9672d7eb0ed3354ce222c210b7d3b", "grade": false, "grade_id": "cell-3f464b42be4a40e5", "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": "4d29d99fe608697f4df30465dd9194fa", "grade": true, "grade_id": "cell-cfa882c037ff13ff", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert(remove([3,1,5,3,9], 1) ==[3,5,3,9])\n", "assert(remove([3,1,5,3,9], 9) ==[3,1,5,3])\n", "assert(remove([3,1,5,3,9], 3) == [1,5,9])\n", "assert(remove([3,1,5,3,9], 2) == [3,1,5,3,9])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q5** Write a function \n", "\n", " delete(data, index)\n", "\n", "that returns a new list that contains the same elements as the list `data` except for the one at the given `index`. If the value of index is negative or exceeds the length of data, return a copy of the original list. For example, delete([3, 1, 5, 9], 2) should return the list [3, 1, 9]. Use a list comprehension. Hint: Your source list must contain all the needed information for the list comprehension to do its job. Explore the `zip` function and/or the `enumerate` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c05e215906f03d7fb8049f3f0dfb1708", "grade": false, "grade_id": "cell-e2a5560beae8497e", "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": "3591ddb6a72bf60e81369e727939f3a0", "grade": true, "grade_id": "cell-7cd21bc67f04e583", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "assert(delete([3, 1, 5, 9], 0) == [1, 5, 9])\n", "assert(delete([3, 1, 5, 9], 2) == [3, 1, 9])\n", "assert(delete([3, 1, 5, 9], 3) == [3, 1, 5])\n", "assert(delete([3, 1, 5, 9], -3) == [3, 1, 5, 9])\n", "assert(delete([3, 1, 5, 9], 4) == [3, 1, 5, 9])" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }