{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"images/book_cover.jpg\" width=\"120\">\n",
    "\n",
    "*This notebook contains an excerpt from the [An Introduction To Python Programming And Numerical Methods For Scientists and Engineers](); the content is available [on GitHub]().*\n",
    "\n",
    "*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book]()!*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [4.1 If-Else Statements](chapter04.01-If-Else-Statements.ipynb) | [Contents](Index.ipynb) | [5.0 Iteration](chapter05.00-Iteration.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary\n",
    "\n",
    "1. Branching (if-else) statements allow functions to take different actions under different circumstances."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Problems\n",
    "\n",
    "1. Write a function *my_tip_calc(bill, party)*, where *bill* is the total cost of a meal and *party* is the number of people in the group. The tip should be calculated as 15% for a party strictly less than six people, 18% for a party strictly less than eight, 20% for a party less than 11, and 25% for a party 11 or more. A couple of test cases are given below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_tip_calc(bill, party):\n",
    "    # write your function code here\n",
    "    \n",
    "    return tips"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# t = 16.3935\n",
    "t = my_tip_calc(109.29,3) \n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# t = 19.6722\n",
    "t = my_tip_calc(109.29,7) \n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# t = 21.8580\n",
    "t = my_tip_calc(109.29,9)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# t = 27.3225\n",
    "t = my_tip_calc(109.29,12)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Write a function *my_mult_operation(a,b,operation)*. The input argument, *operation*, is a string that is either 'plus', 'minus', 'mult', 'div', or 'pow', and the function should compute: $a+b$, $a-b$, $a∗b$, $a/b$, and $a^b$ for the respective values for *operation*. A couple of test cases are given below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_mult_operation(a,b,operation):\n",
    "    # write your function code here\n",
    "    \n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1,2,3,4])\n",
    "y = np.array([2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [3,5,7,9]\n",
    "my_mult_operation(x,y,'plus')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [-1,-1,-1,-1]\n",
    "my_mult_operation(x,y,'minus')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [2,6,12,20]\n",
    "my_mult_operation(x,y,'mult')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [0.5,0.66666667,0.75,0.8]\n",
    "my_mult_operation(x,y,'div')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [1,8,81,1024]\n",
    "my_mult_operation(x,y,'pow')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Consider a triangle with vertices at $(0,0)$, $(1,0)$, and $(0,1)$. Write a function *my_inside_triangle(x,y)* where the output is the string 'outside' if the point $(x,y)$ is outside of the triangle, 'border' if the point is exactly on the border of the triangle, and 'inside' if the point is on the inside of the triangle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_inside_triangle(x,y):\n",
    "    # write your function code here\n",
    "    \n",
    "    return position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 'border'\n",
    "my_inside_triangle(.5,.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 'inside'\n",
    "my_inside_triangle(.25,.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 'outside'\n",
    "my_inside_triangle(5,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Write a function *my_make_size10(x)*, where *x* is an array and output is the first 10 elements of *x* if *x* has more than 10 elements, and output is the array *x* padded with enough zeros to make it length 10 if *x* has less than 10 elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_make_size10(x):\n",
    "    # write your function code here\n",
    "    \n",
    "    return size10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [1,2,0,0,0,0,0,0,0,0]\n",
    "my_make_size10(range(1,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [1,2,3,4,5,6,7,8,9,10]\n",
    "my_make_size10(range(1,15))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: [3,6,13,4,0,0,0,0,0,0]\n",
    "my_make_size10(5,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. Can you write my_make_size10 without using if-statements (i.e., using only logical and array operations)?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6. Write a function *my_letter_grader(percent)*, where grade is the string 'A+' if *percent* is greater than 97, 'A' if *percent* is greater than 93, 'A-' if *percent* is greater than 90, 'B+' if *percent* is greater than 87, 'B' if *percent* is greater than 83, 'B-' if *percent* is greater than 80, 'C+' if *percent* is greater than 77, 'C' if *percent* is greater than 73, 'C-' if *percent* is greater than 70, 'D+' if *percent* is greater than 67, 'D' if *percent* is greater than 63, 'D-' if *percent* is greater than 60, and 'F' for any *percent* less than 60. Grades exactly on the division should be included in the higher grade category."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_letter_grader(percent):\n",
    "    # write your function code here\n",
    "    \n",
    "    return grade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 'A+'\n",
    "my_letter_grader(97)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 'B'\n",
    "my_letter_grader(84)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7. Most engineering systems have redundancy. That is, an engineering system has more than is required to accomplish its purpose. Consider a nuclear reactor whose temperature is monitored by three sensors. An alarm should go off if any two of the sensor readings disagree. Write a function *my_nuke_alarm(s1,s2,s3)* where *s1*, *s2*, and *s3* are the temperature readings for sensor 1, sensor 2, and sensor 3, respectively. The output should be the string 'alarm!' if any two of the temperature readings disagree by strictly more than 10 degrees and 'normal' otherwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_nuke_alarm(s1,s2,s3):\n",
    "    # write your function code here\n",
    "    \n",
    "    return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Output: 'normal'\n",
    "my_nuke_alarm(94,96,90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Output: 'alarm!'\n",
    "my_nuke_alarm(94,96,80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Output: 'normal'\n",
    "my_nuke_alarm(100,96,90)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8. Let Q(x) be the quadratic equation $Q(x) = ax2 + bx + c$ for some scalar values *a*, *b*, and *c*. A root of $Q(x)$ is an *r* such that $Q(r) = 0$. The two roots of a quadratic equation can be described by the quadratic formula, which is\n",
    "\n",
    "$$r = \\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$. \n",
    "\n",
    "A quadratic equation has either two real roots (i.e., $b^2 > 4ac$), two imaginary roots (i.e., $b^2 < 4ac$), or one root, $r = − \\frac{b}{2a}$.\n",
    "\n",
    "Write a function *my_n_roots(a,b,c)*, where *a*, *b*, and *c* are the coefficients of the quadratic $Q(x)$, the function should return two values: *n_roots* and *r*. *n_roots* is 2 if *Q* has two real roots, 1 if *Q* has one root, −2 if *Q* has two imaginary roots, and *r* is an array containing the roots of *Q*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_n_roots(a,b,c):\n",
    "    # write your function code here\n",
    "    \n",
    "    return n_roots, r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: n_roots = 2, r = [3, -3]\n",
    "n_roots, r = my_n_roots(1,0,-9)\n",
    "print(n_roots, r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: n_roots = -2, r = [-0.6667 + 1.1055i, -0.6667 - 1.1055i]\n",
    "my_n_roots(3,4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: n_roots = 1, r = [1]\n",
    "my_n_roots(2,4,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9. Write a function *my_split_function(f,g,a,b,x)*, where *f* and *g* are handles to functions $f(x)$ and $g(x)$, respectively. The output should be $f(x)$ if $x≤a$, $g(x)$ if $x≥b$, and $0$ otherwise. You may assume that $b>a$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_split_function(f,g,a,b,x):\n",
    "    \n",
    "    if x<=a:\n",
    "        return f(x)\n",
    "    elif x>=b:\n",
    "        return g(x)\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 2.713\n",
    "my_split_function(np.exp,np.sin,2,4,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: 0\n",
    "my_split_function(np.exp,np.sin,2,4,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output: -0.9589\n",
    "my_split_function(np.exp,np.sin,2,4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [4.1 If-Else Statements](chapter04.01-If-Else-Statements.ipynb) | [Contents](Index.ipynb) | [5.0 Iteration](chapter05.00-Iteration.ipynb) >"
   ]
  }
 ],
 "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
