From 820127ac624bd0a2ffba46c2d9c362ac4782b6f1 Mon Sep 17 00:00:00 2001 From: eraycemagpurada Date: Fri, 13 Oct 2017 14:20:38 +0000 Subject: [PATCH] Completed Activity --- ...cs-python-loops-challenge-checkpoint.ipynb | 391 ++++++++++++++++++ cs-python-loops-challenge.ipynb | 132 ++++-- 2 files changed, 492 insertions(+), 31 deletions(-) create mode 100644 .ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb 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..e63c409 --- /dev/null +++ b/.ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb @@ -0,0 +1,391 @@ +{ + "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": 2, + "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": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5050" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#run the funtion\n", + "sumTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "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": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def squaresTo(num):\n", + " squares = 0\n", + " #add your code with the required loop here\n", + " for i in range(1, num + 1):\n", + " squares += i ** 2\n", + " return squares" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "338350" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#run the funtion\n", + "squaresTo(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "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": 52, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfFactors(factor, num):\n", + " sumF=0\n", + " i = 0\n", + " # add the loop here\n", + " while factor * i <= num:\n", + " sumF += i * factor\n", + " i += 1\n", + "\n", + " return sumF" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#run the function\n", + "sumOfFactors(3, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "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": 77, + "metadata": {}, + "outputs": [], + "source": [ + "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", + " sum2F = 0\n", + " i = 0\n", + " #add your code with loop here\n", + " while (f1 * f2) * i <= num:\n", + " sum2F += (f1 * f2) * i\n", + " i += 1\n", + "\n", + " return sum2F\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#call the function\n", + "sumOfMultiplesOfTwoFactors(3,4,100)" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "assert sumOfMultiplesOfTwoFactors(3,4,100) == 432, \"error sumOfMultiplesOfTwoFactors(3,4,100)\"" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "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": 181, + "metadata": {}, + "outputs": [], + "source": [ + "def primeTest(num):\n", + " \n", + " #hint: start by assuming the number IS a prime\n", + " isPrime = True\n", + " track = 0\n", + " i = 0\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", + " for i in range(1, num + 1):\n", + " if (num % i) == 0:\n", + " track += 1\n", + " i += 1\n", + " if track > 2:\n", + " isPrime = False\n", + " return isPrime\n", + " else:\n", + " return isPrime\n" + ] + }, + { + "cell_type": "code", + "execution_count": 186, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 186, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# run the function\n", + "primeTest(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "metadata": {}, + "outputs": [], + "source": [ + "#test the function\n", + "assert primeTest(17), \"error testing 17 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 184, + "metadata": {}, + "outputs": [], + "source": [ + "assert primeTest(103), \"error testing 103 is prime\"" + ] + }, + { + "cell_type": "code", + "execution_count": 185, + "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..e63c409 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": 2, "metadata": {}, "outputs": [], "source": [ @@ -76,9 +76,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5050" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", "sumTo(100)" @@ -86,7 +97,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -110,23 +121,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def squaresTo(num):\n", " squares = 0\n", " #add your code with the required loop here\n", - " \n", - "\n", + " for i in range(1, num + 1):\n", + " squares += i ** 2\n", " return squares" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "338350" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the funtion\n", "squaresTo(100)" @@ -134,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -151,31 +173,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "def sumOfFactors(factor, num):\n", " sumF=0\n", + " i = 0\n", " # add the loop here\n", - " \n", - " \n", + " while factor * i <= num:\n", + " sumF += i * factor\n", + " i += 1\n", + "\n", " return sumF" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "18" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#run the function\n", - "sumOfFactors(3, 100)" + "sumOfFactors(3, 10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -191,14 +227,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "def sumOfMultiplesOfTwoFactors(f1,f2,num):\n", " sum2F = 0\n", + " i = 0\n", " #add your code with loop here\n", - " \n", + " while (f1 * f2) * i <= num:\n", + " sum2F += (f1 * f2) * i\n", + " i += 1\n", "\n", " return sum2F\n", " " @@ -206,9 +245,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "432" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#call the function\n", "sumOfMultiplesOfTwoFactors(3,4,100)" @@ -216,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, "outputs": [], "source": [ @@ -225,7 +275,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "metadata": {}, "outputs": [], "source": [ @@ -243,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 181, "metadata": {}, "outputs": [], "source": [ @@ -251,18 +301,38 @@ " \n", " #hint: start by assuming the number IS a prime\n", " isPrime = True\n", + " track = 0\n", + " i = 0\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(1, num + 1):\n", + " if (num % i) == 0:\n", + " track += 1\n", + " i += 1\n", + " if track > 2:\n", + " isPrime = False\n", + " return isPrime\n", + " else:\n", + " return isPrime\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 186, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 186, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# run the function\n", "primeTest(23)" @@ -270,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 183, "metadata": {}, "outputs": [], "source": [ @@ -280,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 184, "metadata": {}, "outputs": [], "source": [ @@ -289,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 185, "metadata": {}, "outputs": [], "source": [