{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

SM286D · Introduction to Applied Mathematics with Python · Spring 2020 · Uhan

\n", "\n", "

Exam 1 — Review Problems — Solutions

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What's up?\n" ] } ], "source": [ "message = \"What's up?\"\n", "print(message)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer is 15.\n" ] } ], "source": [ "x = 5\n", "y = 10\n", "z = x + y\n", "print(f'The answer is {z}.')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "45\n" ] } ], "source": [ "L = list(range(1,10))\n", "s = 0\n", "for i in range(0, 9):\n", " s = s + L[i]\n", "print(s)\n", "# or just sum(L)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "110\n" ] } ], "source": [ "s = 0\n", "for i in range(1,21): \n", " if i % 2 == 0: #Missing :\n", " s = s + i\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "n = 0\n", "while n <= 21:\n", " n += 1\n", " if n % 4 == 1:\n", " break # need the spacing!\n", "print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10405071317\n" ] } ], "source": [ "s = 0\n", "for i in range(1,11):\n", " s = s + i**i\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10405071317\n" ] } ], "source": [ "s = sum([i**i for i in range(1,11)])\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "36\n" ] } ], "source": [ "x = 1\n", "while (x >= 0) and (x <= 19):\n", " for i in range(2, 4):\n", " x = i*x\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "36\n" ] } ], "source": [ "x = 1\n", "while (x >= 0) and (x <= 19):\n", " for i in range(2, 4):\n", " x = i*x\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "6\n", "12\n", "36\n" ] } ], "source": [ "#Part c:\n", "x = 1\n", "while (x >= 0) and (x <= 19):\n", " for i in range(2,4):\n", " x = i*x\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 4" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mW\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m'a'\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mW\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m't'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mW\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mW\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mW\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;31m# It runs forever unless you stop the kernel\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "W = ['m', 'a', 't', 'h']\n", "i = 0\n", "while W[i] != 'a' or W[i] != 't':\n", " i += 1\n", " W.append(W[i])\n", "print(W)\n", "# It runs forever unless you stop the kernel" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['m', 'a', 't', 'h']\n" ] } ], "source": [ "W = ['m', 'a', 't', 'h']\n", "i = 0\n", "while W[i] == 'a' or W[i] == 't':\n", " i += 1\n", " W.append(W[i])\n", "print(W)\n", "# This will print ['m', 'a', 't', 'h'] since it will never enter the while loop" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['m', 'a', 't', 'h', 'a', 't']\n" ] } ], "source": [ "W = ['m', 'a', 't', 'h']\n", "i = 0\n", "while W[i] == 'm' or W[i] == 'a':\n", " i += 1\n", " W.append(W[i])\n", "print(W) \n", "# Here the first two intances of the while loop are true so we carry out the \n", "# instructions. \n", "# Note that the variable i changes before we add letters to the string. Hence \n", "# we add 'a' and 't'." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 5" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def sumavefunc(L):\n", " s = sum(L)\n", " a = sum(L) / len(L)\n", " return [s,a]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a file called `sumavefunc.py` that has this function in it.\n", "Then in a another file have the scripts:\n", "\n", "```\n", "from sumavefunc import sumavefunc\n", "```\n", "\n", "Then you can call this function from this separate file as long as these \n", "files are in the same directory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 6" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def truckprice(model, wd, color, accessories):\n", " if model == 101:\n", " price = 10000\n", " elif model == 201:\n", " price = 12000\n", " if wd == 4:\n", " price = price + 2000\n", " if color == 'metallic':\n", " price = price + 500\n", " elif color == 'race stripe':\n", " price = price + 700\n", " for x in accessories:\n", " price = price + 200\n", " return price" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12400" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "truckprice(101, 4, 'red', ['heated seats', 'premium sound'])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12900" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "truckprice(201, 2, 'race stripe', ['sun roof'])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "ename": "UnboundLocalError", "evalue": "local variable 'price' referenced before assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtruckprice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m301\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'blue'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'self drive'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'self wash'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mtruckprice\u001b[0;34m(model, wd, color, accessories)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m12000\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mwd\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mprice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprice\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2000\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcolor\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'metallic'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprice\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m500\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'price' referenced before assignment" ] } ], "source": [ "truckprice(301, 4, 'blue', ['self drive', 'self wash'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 7" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def fibsq(f1, f0=1): # The == should be = and Missing the : and the default argument should follow the non-default argument.\n", " F = [f0, f1]\n", " sumsq = lambda x, y : x**2 + y**2 # == should be : and ^ should be **\n", " lastnumber=0\n", " while lastnumber < 100: # delete for\n", " lastnumber = sumsq(F[-1], F[-2])\n", " F.append(lastnumber)\n", " return F" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 10, 109]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fibsq(3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, -2, 20, 404]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fibsq(-2, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 8" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "D = {'Alice': 45, 'Bob': 22, 'Carl': 11, 'Diana': 57}\n", "\n", "L = []\n", "for x in D.keys():\n", " L.append(x)\n", "\n", "S = 0\n", "for y in D.values():\n", " S += y" ] } ], "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }