From ce22158e35870643b03e3ad6bf46a51920a700e0 Mon Sep 17 00:00:00 2001 From: JoseVelazquezR Date: Sun, 27 Mar 2022 21:54:20 -0600 Subject: [PATCH] =?UTF-8?q?Laboratorio=20terminado=20exit=20cd=20{p'090895?= =?UTF-8?q?7868760hb=C3=B1b}f{?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 437 +++++++++++++++--- your-code/main.ipynb | 437 +++++++++++++++--- 2 files changed, 742 insertions(+), 132 deletions(-) diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index d68aeb6..429ff28 100644 --- 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,134 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "20\n", + "21\n", + "22\n", + "23\n", + "24\n", + "25\n", + "26\n", + "27\n", + "28\n", + "29\n", + "30\n", + "31\n", + "32\n", + "33\n", + "34\n", + "35\n", + "36\n", + "37\n", + "38\n", + "39\n", + "40\n", + "41\n", + "42\n", + "43\n", + "44\n", + "45\n", + "46\n", + "47\n", + "48\n", + "49\n", + "50\n", + "51\n", + "52\n", + "53\n", + "54\n", + "55\n", + "56\n", + "57\n", + "58\n", + "59\n", + "60\n", + "61\n", + "62\n", + "63\n", + "64\n", + "65\n", + "66\n", + "67\n", + "68\n", + "69\n", + "70\n", + "71\n", + "72\n", + "73\n", + "74\n", + "75\n", + "76\n" + ] + } + ], + "source": [ + "for i in range(0,num):\n", + " i += 1\n", + " i\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " + " # Your code here: \n", + " summ = sum(range(0,num+1))\n", + " return summ\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2926" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation" + "# print summation\n", + "summation(76)" ] }, { @@ -60,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +180,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", - " \n", - " # Your code here:" + " # Your code here:\n", + " return sum(args)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "summation2(*array) # Importante mantener la anotación. \"*\" antes de \"array\"" ] }, { @@ -97,23 +220,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", - " \n", - " # Your code here:" + " # Your code here:\n", + " return a + b + c" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "summation3(3,8,16)" ] }, { @@ -125,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +269,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "summ = 0\n", + "for k,v in dictionary.items():\n", + " if k == \"red\":\n", + " pass\n", + " else:\n", + " summ += v\n", + "summ" + ] + }, + { + "cell_type": "code", + "execution_count": 80, "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:" + " # Your code here:\n", + " summ = 0\n", + " for k,v in kwargs.items():\n", + " if k == \"red\":\n", + " pass\n", + " else:\n", + " summ += v\n", + " return summ" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "summation4(**dictionary)" ] }, { @@ -162,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +354,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a,b,c,*args,**kwargs):\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", + " def sum1(*args):\n", + " suma1 = 0\n", + " for x in args:\n", + " suma1 += x\n", + " return suma1\n", + " \n", + " def sum2(**kwargs):\n", + " suma2 = 0\n", + " for v in kwargs.values():\n", + " suma2 += v\n", + " return suma2\n", + " \n", + " return a+b+c+sum1(*args)+sum2(**kwargs)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "summation5(a,b,c,*d,**e)" ] }, { @@ -205,9 +409,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +432,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +451,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [115]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# After we have iterated through all elements, we will get a StopIteration Error\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,9 +491,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", @@ -274,7 +524,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 135, "metadata": {}, "outputs": [], "source": [ @@ -288,16 +538,42 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for item in iterator:\n", + " if item % 2 == 0:\n", + " yield item\n", + " return 0" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "metadata": {}, "outputs": [], "source": [ - "# print divisible2" + "# print divisible2\n", + "a = divisible2(iterator)" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "0", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [138]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__next__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[1;31mStopIteration\u001b[0m: 0" + ] + } + ], + "source": [ + "a.__next__()" ] }, { @@ -311,7 +587,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "metadata": {}, "outputs": [], "source": [ @@ -331,9 +607,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 140, + "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 +638,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 154, "metadata": {}, "outputs": [], "source": [ @@ -358,18 +646,35 @@ " # This function produces an iterator containing all even numbers between 0 and n\n", " # Input: integer\n", " # Output: iterator\n", - " \n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number += 2\n", " # Sample Input: 5\n", " # Sample Output: iter([0, 2, 4])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" + "execution_count": 155, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "# print even_iterator\n", + "iterator = even_iterator(5)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -382,7 +687,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +701,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index d68aeb6..429ff28 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,134 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "20\n", + "21\n", + "22\n", + "23\n", + "24\n", + "25\n", + "26\n", + "27\n", + "28\n", + "29\n", + "30\n", + "31\n", + "32\n", + "33\n", + "34\n", + "35\n", + "36\n", + "37\n", + "38\n", + "39\n", + "40\n", + "41\n", + "42\n", + "43\n", + "44\n", + "45\n", + "46\n", + "47\n", + "48\n", + "49\n", + "50\n", + "51\n", + "52\n", + "53\n", + "54\n", + "55\n", + "56\n", + "57\n", + "58\n", + "59\n", + "60\n", + "61\n", + "62\n", + "63\n", + "64\n", + "65\n", + "66\n", + "67\n", + "68\n", + "69\n", + "70\n", + "71\n", + "72\n", + "73\n", + "74\n", + "75\n", + "76\n" + ] + } + ], + "source": [ + "for i in range(0,num):\n", + " i += 1\n", + " i\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "def summation(num):\n", " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " + " # Your code here: \n", + " summ = sum(range(0,num+1))\n", + " return summ\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2926" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation" + "# print summation\n", + "summation(76)" ] }, { @@ -60,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +180,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", " # This function returns the sum of *args\n", - " \n", - " # Your code here:" + " # Your code here:\n", + " return sum(args)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "summation2(*array) # Importante mantener la anotación. \"*\" antes de \"array\"" ] }, { @@ -97,23 +220,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "def summation3(a, b, c):\n", " # This function returns a+b+c\n", - " \n", - " # Your code here:" + " # Your code here:\n", + " return a + b + c" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "summation3(3,8,16)" ] }, { @@ -125,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +269,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "summ = 0\n", + "for k,v in dictionary.items():\n", + " if k == \"red\":\n", + " pass\n", + " else:\n", + " summ += v\n", + "summ" + ] + }, + { + "cell_type": "code", + "execution_count": 80, "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:" + " # Your code here:\n", + " summ = 0\n", + " for k,v in kwargs.items():\n", + " if k == \"red\":\n", + " pass\n", + " else:\n", + " summ += v\n", + " return summ" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "54" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "summation4(**dictionary)" ] }, { @@ -162,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +354,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a,b,c,*args,**kwargs):\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", + " def sum1(*args):\n", + " suma1 = 0\n", + " for x in args:\n", + " suma1 += x\n", + " return suma1\n", + " \n", + " def sum2(**kwargs):\n", + " suma2 = 0\n", + " for v in kwargs.values():\n", + " suma2 += v\n", + " return suma2\n", + " \n", + " return a+b+c+sum1(*args)+sum2(**kwargs)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "769" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "summation5(a,b,c,*d,**e)" ] }, { @@ -205,9 +409,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +432,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +451,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [115]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# After we have iterated through all elements, we will get a StopIteration Error\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "# After we have iterated through all elements, we will get a StopIteration Error\n", "\n", @@ -251,9 +491,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", @@ -274,7 +524,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 135, "metadata": {}, "outputs": [], "source": [ @@ -288,16 +538,42 @@ " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", " \n", - " # Your code here:" + " # Your code here:\n", + " for item in iterator:\n", + " if item % 2 == 0:\n", + " yield item\n", + " return 0" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "metadata": {}, "outputs": [], "source": [ - "# print divisible2" + "# print divisible2\n", + "a = divisible2(iterator)" + ] + }, + { + "cell_type": "code", + "execution_count": 138, + "metadata": {}, + "outputs": [ + { + "ename": "StopIteration", + "evalue": "0", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [138]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__next__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[1;31mStopIteration\u001b[0m: 0" + ] + } + ], + "source": [ + "a.__next__()" ] }, { @@ -311,7 +587,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 139, "metadata": {}, "outputs": [], "source": [ @@ -331,9 +607,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 140, + "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 +638,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 154, "metadata": {}, "outputs": [], "source": [ @@ -358,18 +646,35 @@ " # This function produces an iterator containing all even numbers between 0 and n\n", " # Input: integer\n", " # Output: iterator\n", - " \n", + " number = 0\n", + " while number < n:\n", + " yield number\n", + " number += 2\n", " # Sample Input: 5\n", " # Sample Output: iter([0, 2, 4])" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" + "execution_count": 155, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "# print even_iterator\n", + "iterator = even_iterator(5)\n", + "\n", + "for i in iterator:\n", + " print(i)" ] }, { @@ -382,7 +687,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +701,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.3" } }, "nbformat": 4,