Skip to content
Open
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
104 changes: 78 additions & 26 deletions cs-python-loops-challenge.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -76,22 +76,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"20100"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the funtion\n",
"sumTo(100)"
"sumTo(200)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"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<ipython-input-27-e3881085ed86>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#test the function with an assert\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[0msumTo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m21100\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 function with an assert\n",
"assert sumTo(200) == 20100, \"error\""
"assert sumTo(200) == 21100, \"error\""
]
},
{
Expand All @@ -110,31 +133,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"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": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"338350"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the funtion\n",
"squaresTo(100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -151,31 +185,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def sumOfFactors(factor, num):\n",
" sumF=0\n",
" # add the loop here\n",
" \n",
" \n",
" for i in range(0,num+1,factor):\n",
" sumF+=i\n",
" return sumF"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1683"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#run the function\n",
"sumOfFactors(3, 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -198,8 +243,9 @@
"def sumOfMultiplesOfTwoFactors(f1,f2,num):\n",
" sum2F = 0\n",
" #add your code with loop here\n",
" \n",
"\n",
" for i in range(1,num):\n",
" if i%f1==0 and i%f2==0:\n",
" sum2F+=i\n",
" return sum2F\n",
" "
]
Expand All @@ -216,7 +262,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -225,7 +271,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -243,7 +289,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -254,7 +300,12 @@
" \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",
" i=2\n",
" while i<num:\n",
" if i%num==0:\n",
" isPrime=True\n",
" else:\n",
" isPrime=False\n",
" return isPrime\n"
]
},
Expand All @@ -265,7 +316,8 @@
"outputs": [],
"source": [
"# run the function\n",
"primeTest(23)"
"primeTest(103)\n",
"print(\"x\")"
]
},
{
Expand Down