Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
391 changes: 391 additions & 0 deletions .ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -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 = <value>\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",
"<p>Also, see the line:</p>\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
}
Loading