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..f651bb0 --- /dev/null +++ b/.ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb @@ -0,0 +1,387 @@ +{ + "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": null, + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "#run the funtion\n", + "sumTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#test the function with an assert\n", + "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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def squaresTo(num):\n", + " squares = 0\n", + " array = []\n", + " for i in range(1, num+1):\n", + " total = (i * i)\n", + " array.append(total)\n", + " \n", + " squares = sum(array)\n", + " #add your code with the required loop here\n", + " \n", + "\n", + " return squares" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "338350" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#run the funtion\n", + "squaresTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "#test the functionality\n", + "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": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfFactors(factor, num):\n", + " sumF=0\n", + " array = []\n", + " # add the loop here\n", + " for i in range(1, num+1):\n", + " if i % factor == 0:\n", + " array.append(i)\n", + " sumF = sum(array)\n", + " \n", + " \n", + " \n", + " return sumF" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#run the function\n", + "sumOfFactors(3, 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", + " sum2F = 0\n", + " array = []\n", + " #add your code with loop here\n", + " for i in range(1, num+1):\n", + " if i % f1 == 0 and i % f2 == 0:\n", + " array.append(i)\n", + " sum2F = sum(array)\n", + "\n", + " return sum2F\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#call the function\n", + "sumOfMultiplesOfTwoFactors(3,4,100)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumOfMultiplesOfTwoFactors(3,4,100) == 432, \"error sumOfMultiplesOfTwoFactors(3,4,100)\"" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "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": 11, + "metadata": {}, + "outputs": [], + "source": [ + "def primeTest(num):\n", + " \n", + " #hint: start by assuming the number IS a prime\n", + " isPrime = True\n", + " \n", + " if num > 1:\n", + " for i in range(2, num):\n", + " if (num % i) == 0:\n", + " isPrime = False\n", + " \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" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# run the function\n", + "primeTest(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "#test the function\n", + "assert primeTest(17), \"error testing 17 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "assert primeTest(103), \"error testing 103 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "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..f651bb0 100644 --- a/cs-python-loops-challenge.ipynb +++ b/cs-python-loops-challenge.ipynb @@ -110,12 +110,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def squaresTo(num):\n", " squares = 0\n", + " array = []\n", + " for i in range(1, num+1):\n", + " total = (i * i)\n", + " array.append(total)\n", + " \n", + " squares = sum(array)\n", " #add your code with the required loop here\n", " \n", "\n", @@ -124,9 +130,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "338350" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", "squaresTo(100)" @@ -134,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -151,13 +168,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def sumOfFactors(factor, num):\n", " sumF=0\n", + " array = []\n", " # add the loop here\n", + " for i in range(1, num+1):\n", + " if i % factor == 0:\n", + " array.append(i)\n", + " sumF = sum(array)\n", + " \n", " \n", " \n", " return sumF" @@ -165,9 +188,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1683" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the function\n", "sumOfFactors(3, 100)" @@ -175,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -191,14 +225,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", " sum2F = 0\n", + " array = []\n", " #add your code with loop here\n", - " \n", + " for i in range(1, num+1):\n", + " if i % f1 == 0 and i % f2 == 0:\n", + " array.append(i)\n", + " sum2F = sum(array)\n", "\n", " return sum2F\n", " " @@ -206,9 +244,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#call the function\n", "sumOfMultiplesOfTwoFactors(3,4,100)" @@ -216,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -225,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -243,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -252,6 +301,12 @@ " #hint: start by assuming the number IS a prime\n", " isPrime = True\n", " \n", + " if num > 1:\n", + " for i in range(2, num):\n", + " if (num % i) == 0:\n", + " isPrime = False\n", + " \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", @@ -260,9 +315,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# run the function\n", "primeTest(23)" @@ -270,7 +336,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -280,7 +346,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -289,7 +355,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [