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
384 changes: 384 additions & 0 deletions .ipynb_checkpoints/cs-python-loops-challenge-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -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 = <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": 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",
"<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": 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<ipython-input-6-c3510b256019>\u001b[0m in \u001b[0;36m<module>\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
}
Loading