From b4c10034857166bf27f54f493eaa9fcf4e91129e Mon Sep 17 00:00:00 2001 From: olaramoni Date: Fri, 6 Oct 2017 13:41:03 +0000 Subject: [PATCH] Write python looping functions --- cs-python-loops-challenge.ipynb | 104 ++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 26 deletions(-) diff --git a/cs-python-loops-challenge.ipynb b/cs-python-loops-challenge.ipynb index ed55ae9..bcfa8da 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": 18, "metadata": {}, "outputs": [], "source": [ @@ -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\u001b[0m in \u001b[0;36m\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\"" ] }, { @@ -110,23 +133,34 @@ }, { "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)" @@ -134,7 +168,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -151,23 +185,34 @@ }, { "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)" @@ -175,7 +220,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -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", " " ] @@ -216,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -225,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -243,7 +289,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -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