{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## While Loops\n", "\n", "While loops are usually used when we do not know in advance how many times we want to iterate, or if we want to stop as soon as some condition is reached. The syntax of while loops is:\n", "\n", "```\n", "while :\n", " \n", "```\n", "\n", "The condition is any expression that can be true or false (i.e. expressions that evaluate to a boolean). This is the same as you use on `if` and `elif`.\n", "\n", "The way while loops work is: \n", "\n", "1. Check the condition\n", "\n", " a. If it is true: run the steps, go back to 1\n", " \n", " b. If it is false: go to the first line after the loop (starting at the same column as while)\n", " \n", "**Note:** If the condition is always true, your program will run forever! This is what is called an *infinite loop*.\n", "\n", "Typically the loop condition will use a variable that is modified inside the loop, for example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = 0\n", "sum = 0\n", "while x < 1000:\n", " sum = sum + x\n", " x = x + 1 # Increases x, so it will reach 1000 eventually" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Be careful about how the variable is changed." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#x = 0\n", "#sum = 0\n", "#while x < 1000:\n", "# sum = sum + x\n", "# x = x - 1\n", " \n", "# Infinite loop." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Similar to for-loops, if the program flow hits a return in the middle of an iteration, it simply returns and does not run more iterations.\n", "\n", "The code below, in particular, only runs the first iteration, and then stops." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def doesNotLoopTechnically(n):\n", " while(n < 100):\n", " if n % 10 == 0:\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1\n", "\n", "Implement the function `numberOfDigits(n)` that returns the number of digits `n` has. \n", "For example `numberOfDigits(15110)` should return 5." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def numberOfDigits(n):\n", " d = 0\n", " while n > 0:\n", " n = n // 10;\n", " d = d + 1\n", " \n", " return d\n", "\n", "def numberOfDigits(n):\n", " p = 0\n", " while (n % (10 ** p)) != n:\n", " p = p + 1\n", " \n", " return p\n", "\n", "numberOfDigits(123456789)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2: population increase\n", "\n", "You are studying two populations A and B. You know that population A is initially smaller than population B, but it grows at a faster rate. You would like to know haw many days it would take for population A to overtake population B. Luckily you have taken 15-110, and you know it would be faster to sit down and implement a program to compute that for you, than having to do the math each time (you work with a lot of populations of many different organisms...).\n", "\n", "Implement the function `populationIncrease(pa, pb, ga, gb)` that takes as parameters:\n", "\n", "- The number `pa` of individuals in population A\n", "- The number `pb` of individuals in population B\n", "- The growth rate `ga` of population A (in percent per day)\n", "- The growth rate `gb` of poplation B (in percent per day)\n", "\n", "This function should return the number of days it takes for population A to overtake population B.\n", "\n", "**Important:** Populations grow by an integer number of individuals. So if the growth rate is 3.6% and the population is 100, in one day there will be 103 (not 103.6) individuals. If the population is 1000, there will be 1036 individuals.\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import math\n", "def populationIncrease(pa, pb, ga, gb):\n", " return 42\n", "\n", "# assert(populationIncrease(100, 150, 1.0, 0) == 51)\n", "# assert(populationIncrease(90000, 120000, 5.5, 3.5) == 16)\n", "# assert(populationIncrease(56700, 72000, 5.2, 3.0) == 12)\n", "# assert(populationIncrease(123, 2000, 3.0, 2.0) == 300)\n", "# assert(populationIncrease(100000, 110000, 1.5, 0.5) == 10)\n", "# assert(populationIncrease(62422, 484317, 3.1, 1.0) == 100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3: the straggler (again)\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:\n", "- Lap 1: Fastest crosses the start line at 5, slowest is 2 seconds behind\n", "- Lap 2: Fastest crosses the start line at 10, slowest is 4 seconds behind\n", "- Lap 3: Fastest crosses the start line at 15, slowest is 6 seconds behind\n", "\n", "**Hint:** Can you solve this by simulating the race?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def straggler(fastest, slowest):\n", " return 42" ] } ], "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 }