{ "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": "markdown", "metadata": {}, "source": [ "**Q1** Write a function\n", "\n", " vectorLengths(data)\n", "\n", "that generates and returns a new list, where each element in the created list contains the integer length of the corresponding element from `data`. So, for example, \n", "\n", " vectorLengths(['this', 'is', 'a', 'short', 'list'])\n", "\n", "yields the list `[4, 2, 1, 5, 4]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "1b2975dfd3941b22eeba89f7ae92f1aa", "grade": false, "grade_id": "cell-22d5c239fc12e962", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "retval = vectorLengths(['this', 'is', 'a', 'short', 'list'])\n", "print(retval)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "99a9b3eb134bde4bc59fe63cecd85ea2", "grade": true, "grade_id": "cell-9f8dcb8c5c0b996a", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "L = ['this', 'is', 'a', 'short', 'list']\n", "L2 = vectorLengths(L)\n", "assert len(L2) == len(L)\n", "assert type(L2[0]) == int\n", "assert L2[0] == 4\n", "assert L2 == [4, 2, 1, 5, 4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2** Write a function\n", "\n", " vectorGreater(x, y)\n", " \n", "that generates a boolean vector whose corresponding elements indicate whether or not an element in `x` is greater than the corresponding element in `y`. So\n", "\n", " vectorGreater([4, 7, 2], [5, 6, 2])\n", " \n", "would yield `[False, True, False]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "4fca768ce8120fd7bd1827e8e8126ec2", "grade": false, "grade_id": "cell-9210dfc1ab8b3967", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "retval = vectorGreater([4, 7, 2], [5, 6, 2])\n", "print(retval)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "d4cde155e62a5e5cc3f58517c542c424", "grade": true, "grade_id": "cell-142a0e6adaaf9176", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "retval = vectorGreater([4, 7, 2], [5, 6, 2])\n", "assert retval == [False, True, False]\n", "assert vectorGreater([], []) == []\n", "retval = vectorGreater([4, 4, 4], [4, 4, 4])\n", "assert retval == [False, False, False]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q3** Write a function\n", "\n", " dropFives(data)\n", " \n", "that creates, accumulates, and returns a new list that has all the elements from the list, `data`, except for the items with integer value 5." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "5f4cf64b47bb5b7ee782fca64e4f4d70", "grade": false, "grade_id": "cell-48a42b292df5780f", "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": "1748e1672e98c841650a6cf4191d0205", "grade": true, "grade_id": "cell-b756f698c8fc9287", "locked": true, "points": 2, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# Testing cell\n", "\n", "L = [5, 3, 8, 7, 5, 2, 4]\n", "L2 = dropFives(L)\n", "assert L2 == [3, 8, 7, 2, 4]\n", "\n", "L = [5, 5, 5, 5]\n", "L2 = dropFives(L)\n", "assert L2 == []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q (TB)** Recall the unary vector operation pattern, where we applied `celsius2fahrenheit()` to each temperature in a list of Celsius temperatures. This is a common pattern, so it would be helpful to write a function to abstract this pattern in a callable way. Write a function \n", "\n", " apply_func(data)\n", "\n", "that applies `celsius2fahrenheit()` to each item in `data`. So here, we are simply creating an interface to pass the list to be applied upon. Rather than appending into and returning a new list, your function should return nothing and should update the list `data` as you iterate through it, as we did in the unary vector pattern. This is analogous to how the built-in `sort()` function does not return a list, but rather modifies the list it is called upon.\n", "\n", "Note that you must **define `celsius2fahrenheit()` as well as `apply_func()`** in your solution." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "fc566a1d4b2723c0b8b99c965263cbbd", "grade": true, "grade_id": "cell-5675935f58cddf33", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# Solution cell\n", "\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "data = [0, 100, 18, 31]\n", "apply_func(data)" ] } ], "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 }