diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index d68aeb6..4a6dcd5 100755 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", + " '''Esta funcion retorna la suma de los numeros del 1 al argumento num'''\n", + " num_list = [i for i in range(1,num+1)]\n", + " return sum(num_list)\n", " \n", " # Your code here: " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Esta funcion retorna la suma de los numeros del 1 al argumento num\n", + "2926\n" + ] + } + ], "source": [ - "# print summation" + "# print summation\n", + "print(summation.__doc__)\n", + "print( summation(num))" ] }, { @@ -60,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +83,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", - " \n", - " # Your code here:" + " return sum(args)\n", + " # Your code here:\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "print(summation2(*array))" ] }, { @@ -97,23 +120,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", - " \n", - " # Your code here:" + " return a + b + c\n", + " # Your code here:\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "print(summation3(array[0],array[1], array[2]))" ] }, { @@ -125,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -134,14 +166,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", - " \n", - " # Your code here:" + " x = 0\n", + " for i,j in kwargs.items() :\n", + " if not i == 'red': x = x + j\n", + " # Your code here:\n", + " return x" ] }, { @@ -150,7 +185,8 @@ "metadata": {}, "outputs": [], "source": [ - "# print summation4" + "# print summation4\n", + "print(summation4(**dictionary))" ] }, { @@ -162,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +211,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a, b, c, *arg, **kwarg):\n", " # This function returns the sum of a,b,c,d and e\n", " # You need to define the function input\n", - " # Your code here:" + " # Your code here:\n", + " int_sum = a + b + c\n", + " lst_sum = sum(arg)\n", + " dict_sum = sum(kwarg.values())\n", + " return int_sum + lst_sum + dict_sum\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "769\n" + ] + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "print(summation5(a,b,c,*d,**e))" ] }, { @@ -205,9 +254,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +277,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +296,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_19548/3052031144.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,14 +336,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "1\n", + "2\n", + "3\n" + ] + } + ], "source": [ "# We can also iterate through an iterator using a for loop like this:\n", "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", "# This is why we are redefining the iterator below\n", - "\n", + "print(iterator)\n", "iterator = iter([1,2,3])\n", "\n", "for i in iterator:\n", @@ -274,7 +370,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, "outputs": [], "source": [ @@ -288,16 +384,31 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for i in iterator:\n", + " if i % 2 == 0 :\n", + " return i\n", + " break\n", + " \n", + " \n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "print(divisible2(iterator))" ] }, { @@ -311,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 96, "metadata": {}, "outputs": [], "source": [ @@ -331,9 +442,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], "source": [ "iterator = firstn(5)\n", "\n", @@ -350,7 +473,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "metadata": {}, "outputs": [], "source": [ @@ -360,12 +483,16 @@ " # Output: iterator\n", " \n", " # Sample Input: 5\n", - " # Sample Output: iter([0, 2, 4])" + " # Sample Output: iter([0, 2, 4])\n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 2 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "metadata": {}, "outputs": [], "source": [ @@ -374,15 +501,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "iterator = even_iterator(5)\n", + "for i in iterator:\n", + " print(i)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +537,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index d68aeb6..ad61ab9 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", + " '''Esta funcion retorna la suma de los numeros del 1 al argumento num'''\n", + " num_list = [i for i in range(1,num+1)]\n", + " return sum(num_list)\n", " \n", " # Your code here: " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Esta funcion retorna la suma de los numeros del 1 al argumento num\n", + "2926\n" + ] + } + ], "source": [ - "# print summation" + "# print summation\n", + "print(summation.__doc__)\n", + "print( summation(num))" ] }, { @@ -60,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +83,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", - " \n", - " # Your code here:" + " return sum(args)\n", + " # Your code here:\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "print(summation2(*array))" ] }, { @@ -97,23 +120,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", - " \n", - " # Your code here:" + " return a + b + c\n", + " # Your code here:\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "print(summation3(*array))" ] }, { @@ -125,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +166,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", - " \n", - " # Your code here:" + " x = 0\n", + " for i,j in kwargs.items() :\n", + " if not i == 'red': x = x + j\n", + " # Your code here:\n", + " return x" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "print(summation4(**dictionary))" ] }, { @@ -162,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +219,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a, b, c, *arg, **kwarg):\n", " # This function returns the sum of a,b,c,d and e\n", " # You need to define the function input\n", - " # Your code here:" + " # Your code here:\n", + " int_sum = a + b + c\n", + " lst_sum = sum(arg)\n", + " dict_sum = sum(kwarg.values())\n", + " return int_sum + lst_sum + dict_sum\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "769\n" + ] + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "print(summation5(a,b,c,*d,**e))" ] }, { @@ -205,9 +262,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +285,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +304,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_26916/3052031144.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# After we have iterated through all elements, we will get a StopIteration Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,14 +344,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "1\n", + "2\n", + "3\n" + ] + } + ], "source": [ "# We can also iterate through an iterator using a for loop like this:\n", "# Note: we cannot go back directly in an iterator once we have traversed through the elements. \n", "# This is why we are redefining the iterator below\n", - "\n", + "print(iterator)\n", "iterator = iter([1,2,3])\n", "\n", "for i in iterator:\n", @@ -274,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -288,16 +392,31 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for i in iterator:\n", + " if i % 2 == 0 :\n", + " return i\n", + " break\n", + " \n", + " \n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "print(divisible2(iterator))" ] }, { @@ -311,7 +430,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -331,9 +450,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], "source": [ "iterator = firstn(5)\n", "\n", @@ -350,7 +481,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -360,18 +491,54 @@ " # Output: iterator\n", " \n", " # Sample Input: 5\n", - " # Sample Output: iter([0, 2, 4])" + " # Sample Output: iter([0, 2, 4])\n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number = number + 2 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# print even_iterator" ] }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n", + "6\n", + "8\n", + "10\n", + "12\n", + "14\n", + "16\n", + "18\n", + "20\n", + "22\n", + "24\n", + "26\n" + ] + } + ], + "source": [ + "iterator = even_iterator(27)\n", + "for i in iterator:\n", + " print(i)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -382,7 +549,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +563,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.9.6" } }, "nbformat": 4,