diff --git a/.ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb b/.ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb new file mode 100644 index 0000000..31c236d --- /dev/null +++ b/.ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb @@ -0,0 +1,384 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Practicing Loops in Python\n", + "\n", + "In this notebook you need to write code for the funtions below. Each function will require a loop.\n", + "\n", + "Remember you can use: \n", + "\n", + "a. **FOR** or counting loops\n", + "\n", + "```python \n", + "for loopVar in range(start, stop, step):\n", + " #indent - do loop stuff\n", + " \n", + "#dedent - loop is done. Do more stuff\n", + "\n", + " ```\n", + "b. **WHILE** or conditional loops\n", + "\n", + "```python\n", + "loopVar = \n", + "while condition :\n", + " #indent - do loop stuff\n", + " loopVar +=1 # change loop varible or you get an infinite loop!\n", + " \n", + "#dedent - loop is done. Do next thing\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example - sumTo(num): takes a number as a parameter and adds all the numbers from 1 to that number\n", + "\n", + "The sumTo function has been done for you." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "#This funtion sums up all the numbers from 1 to num\n", + "def sumTo(num):\n", + " sum=0\n", + " for i in range(1,num+1):\n", + " sum+=i # keep adding i into the sum - same as sum = sum +i\n", + " return sum" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "See how the loop in the code above goes from 1 (incl) up to (but not including) num+1 ?\n", + "

Also, see the line:

\n", + "\n", + "```python\n", + "sum+=i\n", + "```\n", + "\n", + "This will keep adding the loop index **i** into the sum. It is cummulative! It is the same as: \n", + "\n", + "```python\n", + "sum = sum +i\n", + "```\n", + "\n", + "Run the code below to see the function in action and check for errors:" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5050" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumTo(200) == 20100, \"error\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Exercise - write the code for the following functions as described\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1) sumSquaresTo(num) - this takes a number and adds all the numbers squared between 1 and num (eg 1+4+9 + 16 etc)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "def squaresTo(num):\n", + " squares = 0\n", + " for i in range(1, num):\n", + " if math.sqrt(i).is_integer():\n", + " squares += i\n", + " \n", + " return squares" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "285" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "squaresTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "error", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\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 1\u001b[0m \u001b[0;31m#test the functionality\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0msquaresTo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m338350\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"error\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m: error" + ] + } + ], + "source": [ + "assert squaresTo(100) == 338350, \"error\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2) sumOfMultiples(factor, num) - takes a factor and adds up all the numbers to num that are a multiple of that factor eg sumOfFactors (3, 10 ) = 3+6+9" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfFactors(factor, num):\n", + " sumF = 0\n", + " for i in range(num):\n", + " if i % factor == 0:\n", + " sumF += i\n", + " \n", + " return sumF" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumOfFactors(3, 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumOfFactors(3,100) == 1683, \"error\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3) sumOfMultiplesOfTwoFactors(f1,f2,num) - adds up all the factors of both f1 and f2 up to including num. Eg sumOfMultiplesOfTwoFactors(3,4,50) = 12 + 24 + 36 + 48" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfMultiplesOfTwoFactors(f1, f2, num):\n", + " sum2F = 0\n", + " for i in range(num):\n", + " if (i % f1 == 0) and (i % f2 == 0):\n", + " sum2F += i\n", + " \n", + "\n", + " return sum2F " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sumOfMultiplesOfTwoFactors(3, 4, 100)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumOfMultiplesOfTwoFactors(3,4,100) == 432, \"error sumOfMultiplesOfTwoFactors(3,4,100)\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumOfMultiplesOfTwoFactors(3,5,1000) == 33165, \"error sumOfMultiplesOfTwoFactors(3,5,1000)\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4) primeTest(num) - returns true if a number is a prime. Eg primeTest(17) = true, primeTest(15)=false\n", + "\n", + "hint - use a WHILE loop" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [], + "source": [ + "def primeTest(num):\n", + " \n", + " for i in range(3, num, 2):\n", + " if num % i == 0:\n", + " return False\n", + " \n", + " return True\n" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "primeTest(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [], + "source": [ + "#test the function\n", + "assert primeTest(17), \"error testing 17 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [], + "source": [ + "assert primeTest(103), \"error testing 103 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [], + "source": [ + "assert not primeTest(15), \"error testing 15 is NOT prime\"" + ] + } + ], + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cs-python-loops-challenge.ipynb b/cs-python-loops-challenge.ipynb index ed55ae9..31c236d 100644 --- a/cs-python-loops-challenge.ipynb +++ b/cs-python-loops-challenge.ipynb @@ -42,7 +42,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -76,21 +76,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5050" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#run the funtion\n", "sumTo(100)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ - "#test the function with an assert\n", "assert sumTo(200) == 20100, \"error\"" ] }, @@ -110,35 +119,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ + "import math\n", + "\n", "def squaresTo(num):\n", " squares = 0\n", - " #add your code with the required loop here\n", + " for i in range(1, num):\n", + " if math.sqrt(i).is_integer():\n", + " squares += i\n", " \n", - "\n", " return squares" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "285" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#run the funtion\n", "squaresTo(100)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AssertionError", + "evalue": "error", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\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 1\u001b[0m \u001b[0;31m#test the functionality\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0msquaresTo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m100\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m338350\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"error\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m: error" + ] + } + ], "source": [ - "#test the functionality\n", "assert squaresTo(100) == 338350, \"error\"" ] }, @@ -151,31 +184,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def sumOfFactors(factor, num):\n", - " sumF=0\n", - " # add the loop here\n", - " \n", + " sumF = 0\n", + " for i in range(num):\n", + " if i % factor == 0:\n", + " sumF += i\n", " \n", " return sumF" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#run the function\n", "sumOfFactors(3, 100)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -191,27 +235,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", + "def sumOfMultiplesOfTwoFactors(f1, f2, num):\n", " sum2F = 0\n", - " #add your code with loop here\n", + " for i in range(num):\n", + " if (i % f1 == 0) and (i % f2 == 0):\n", + " sum2F += i\n", " \n", "\n", - " return sum2F\n", - " " + " return sum2F " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "120" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#call the function\n", - "sumOfMultiplesOfTwoFactors(3,4,100)" + "sumOfMultiplesOfTwoFactors(3, 4, 100)" ] }, { @@ -243,34 +298,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "def primeTest(num):\n", " \n", - " #hint: start by assuming the number IS a prime\n", - " isPrime = True\n", - " \n", - " #write your code using a loop to test whether a number is a prime\n", - " #hint: when is a number NOT a prime??\n", - " \n", - " return isPrime\n" + " for i in range(3, num, 2):\n", + " if num % i == 0:\n", + " return False\n", + " \n", + " return True\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# run the function\n", "primeTest(23)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, "outputs": [], "source": [ @@ -280,7 +343,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "metadata": {}, "outputs": [], "source": [ @@ -289,7 +352,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, "outputs": [], "source": [