{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Arithmetic in Python\n", "\n", "### Number types:\n", "\n", "- `int`: ... -3, -2, -1, 0, 1, 2, 3, ...\n", "- `float`: 3.141592\n", "\n", "### Operators:\n", "\n", "Addition" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "34.0 + 8.3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subtraction" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.400000000000006" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "57.6 - 15.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiplication" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6.0 * 7.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Floating point division**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8 / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Integer division** (always rounds down)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 // 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Modulo** (remainder of integer division)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that: \n", "\n", "- Can be used to check for divisibility\n", "- If `q = x // y`, and `r = x % y` then: `x = y * q + r`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exponentiation" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 ** 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Absolute value (disregard the sign)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ceiling function (rounding up)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math # tells python that we want to use functions from the math library\n", " # import will be in the starter file for the most part of the course\n", "math.ceil(4.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Flooring function (rounding down)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.floor(4.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1: temperature conversion\n", "\n", "Fahrenheit temperatures can be calculated in terms of Celcius temperatures using the following formula:\n", "\n", "$F = C \\times \\frac{9}{5} + 32$\n", "\n", "Implement the functions `celciusToFahrenheit(c)` and `fahrenheitToCelcius(f)` that converts the temperatures." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def celciusToFahrenheit(c):\n", " return c * 9/5 + 32\n", "\n", "def fahrenheitToCelcius(f):\n", " return (f-32)*5/9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2: mixing colors\n", "\n", "Colors in a computer are sometimes represented using integer RGB values, corresponding to the amount of red, green, and blue it is composed of.\n", "These values are integers between 0 and 255.\n", "For example, (0,0,0) is black, and (255,255,255) is white.\n", "\n", "Given two colors, we can combine them to form a palette (the colors in between the two). We can decide how many colors we want in this palette (the midpoints).\n", "\n", "For example, we can combine the colors crimson (RGB = (220, 20, 60)) and mint (RGB = (189, 252, 201)) using 3 midpoints to obtain:\n", "\n", "```\n", "color0: (220, 20, 60) # crimson\n", "color1: (212, 78, 95)\n", "color2: (205, 136, 131)\n", "color3: (197, 194, 166)\n", "color4: (189, 252, 201) # mint\n", "```\n", "\n", "This pallete has 5 colors. We could then ask ourselves: what is color 2?\n", "\n", "Implement the function `getColor(r1, g1, b1, r2, g2, b2, midpoints, n)` that takes as input the two initial colors, the number of midpoints, and the number of the color in the palette, and returns the RGB value of the n-th color.\n", "\n", "For example, following the case above: `getColor(220, 20, 60, 189, 252, 201, 3, 1)` returns `(212, 78, 95)`.\n", "\n", "**Hint:** for the sake of this exercise, it is ok if your result differs by one unit from the example, since integer division is not exact. We will learn how to fix this later.\n", "\n", "**Hint:** try thinking about the problem with one component at a time (red, green, or blue), and with small midpoints (e.g. 1, 2, 3).\n", "\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def getColor(r1, g1, b1, r2, g2, b2, midpoints, n):\n", " r_at_n = round(abs((r1-r2)/(midpoints+1)*n - r1))\n", " g_at_n = round(abs((g1-g2)/(midpoints+1)*n - g1))\n", " b_at_n = round(abs((b1-b2)/(midpoints+1)*n - b1))\n", " \n", " return (r_at_n, g_at_n, b_at_n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3: the straggler\n", "\n", "In motorsports it is very common that the leader pilot in a race, at a certain moment, overtakes the last pilot. The leader, at this moment, is one lap ahead of the last pilot, who then becomes a straggler (or tailender). \n", "\n", "Implement the function `straggler(fast, slow)` that takes as input the time it takes for the faster and slowest pilots to complete a lap, and returns the number of the lap when the fastest pilot overtakes the slowest one. The first lap is number 0.\n", "\n", "For example, `straggler(5, 7)` should return 3." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math\n", "\n", "def straggler(fastest, slowest):\n", " faster_by = slowest/fastest\n", " \n", " return math.ceil(1 + 2/faster_by)" ] } ], "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": 2 }