{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Before you turn this problem in, make sure everything runs as expected. First, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) and then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", "\n", "Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\", as well as your name and collaborators below:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NAME = \"\"\n", "COLLABORATORS = \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import os.path\n", "import random" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions as Objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def add1(n):\n", " return n+1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L1 = [ random.randint(1,30) for i in range(5) ]\n", "\n", "L2 = []\n", "for val in L1:\n", " newval = add1(val)\n", " L2.append(newval)\n", "\n", "print(L1)\n", "print(L2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def apply_func(func, data):\n", " result = []\n", " for val in data:\n", " newval = func(val)\n", " result.append(newval)\n", " return result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L2 = apply_func(add1, L1)\n", "print(L2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q** Create three more functions, `add2`, `add3`, and `add4` and then create a list called `funcs` whose elements are the four functions `add1` through `add4`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "2478482c1ceb7f8e685a6902d35da41c", "grade": true, "grade_id": "cell-2f7531b0cb1bed08", "locked": false, "points": 3, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q** Suppose you are given a list of numbers `L3` whose length is the same as `funcs`. Write code to generate list `L4` whose values come from applying the *corresponding* function from `funcs` to its *correspondent* in `L3`. Bonus to do this with a list comprehension." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "5215443771957ac74099c61688e23e7d", "grade": true, "grade_id": "cell-e2c2cdae2ba2ca46", "locked": false, "points": 2, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "L3 = [5, 9, 2, 7]\n", "# YOUR CODE HERE\n", "raise NotImplementedError()\n", "print(L4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lambda Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lambda x: x+1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lambda x, y: x/y * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lambda x, y: x/y * 2 if y != 0 else 999999" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lambda L: [ abs(x) for x in L ]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lambda L: [ abs(x) if x % 2 == 0 else x - 1 for x in L ]" ] } ], "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 }