diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index d68aeb6..6c9e1ec 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -21,32 +21,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "num=76" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma del 1 hasta tu numero 76 es 2926\n" + ] + } + ], "source": [ "def summation(num):\n", - " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation" + " '''This function returns 1+2+3+....+num'''\n", + " suma= 0\n", + " for i in range(0,num+1):\n", + " suma=suma+i\n", + " return suma\n", + "\n", + "num=76\n", + "print(\"la suma del 1 hasta tu numero\", num, 'es', summation(num))" ] }, { @@ -60,32 +55,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "array=[3, 8, 16]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de los numeros contenidos en tu array es: 27\n" + ] + } + ], + "source": [ + "array=[3, 8, 16]\n", "def summation2(*args):\n", - " # This function returns the sum of *args\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation2" + " '''This function returns the sum of *args'''\n", + " suma=sum(args)\n", + " return suma\n", + "\n", + "print(\"la suma de los numeros contenidos en tu array es: \", summation2(*array))\n", + "\n", + " " ] }, { @@ -97,23 +87,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma resultante es: 18\n" + ] + } + ], "source": [ "def summation3(a, b, c):\n", - " # This function returns a+b+c\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation3" + " '''This function returns a+b+c'''\n", + " suma= a+b+c\n", + " return suma\n", + "\n", + "print(\"la suma resultante es:\", summation3(5,4,9))" ] }, { @@ -125,32 +116,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de los numeros del diccionario que no son rojos es: 54\n" + ] + } + ], + "source": [ + "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}\n", "def summation4(**kwargs):\n", - " # This function returns the sum of all M&Ms, except the red ones\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation4" + " '''This function returns the sum of all M&Ms, except the red ones'''\n", + " suma=0 \n", + " for i,j in kwargs.items():\n", + " if i!='red':\n", + " suma+=j\n", + " return suma\n", + "\n", + "print(\"la suma de los numeros del diccionario que no son rojos es: \", summation4(**dictionary))\n", + "\n" ] }, { @@ -162,36 +150,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de las variables es: 769\n" + ] + } + ], "source": [ "a=4\n", "b=6\n", "c=15\n", "d=[48, 465, 23, 96]\n", - "e={'A':16, 'B':32, 'C':64}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation5('define the function input'):\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:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation5" + "e={'A':16, 'B':32, 'C':64}\n", + "\n", + "def summation5(*args,**kwargs):\n", + " '''This function returns the sum of a,b,c,d and e\n", + " You need to define the function input'''\n", + " values= sum(kwargs.values())\n", + " suma_argumentos= sum(args)\n", + " suma= suma_argumentos+values\n", + " \n", + " return suma\n", + " \n", + "print(\"la suma de las variables es: \", summation5(a,b,c,*d,**e))\n", + " " ] }, { @@ -205,9 +192,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +215,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +234,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "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 [64]\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 +274,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 59, + "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", @@ -269,35 +302,40 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In the cell below, write a function that takes an iterator and returns the first element in the iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." + "In the cell below, write a function that takes an iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numero divisible de la lista: 0\n" + ] + } + ], "source": [ "iterator=iter([1,2,3])\n", "\n", "def divisible2(iterator):\n", - " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", - " # Input: Iterable\n", - " # Output: Integer\n", - " \n", - " # Sample Input: iter([1,2,3])\n", - " # Sample Output: 2\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print divisible2" + " '''\n", + " This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " Input: Iterable\n", + " Output: Integer\n", + " Sample Input: iter([1,2,3])\n", + " Sample Output: 2\n", + " '''\n", + " for i in iterator:\n", + " if i%2==0:\n", + " return i\n", + " else:\n", + " return 0\n", + "\n", + "print(\"numero divisible de la lista:\", divisible2(iterator))" ] }, { @@ -311,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ @@ -322,6 +360,44 @@ " number = number + 1" ] }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + }, + { + "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 [127]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(lis_fir)))\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(lis_fir)))\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43miter\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mlis_fir\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "lis_fir=firstn(5)\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir))) #Error. Se acabaron los numeros contenidos en el objeto lis:fir\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -331,9 +407,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 108, + "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,28 +438,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 138, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[0, 2, 4]]" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def even_iterator(n):\n", - " # This function produces an iterator containing all even numbers between 0 and n\n", - " # Input: integer\n", - " # Output: iterator\n", + " '''\n", + " This function produces an iterator containing all even numbers between 0 and n\n", + " Input: integer\n", + " Output: iterator \n", + " \n", + " '''\n", + " even=[i for i in range(0, n) if i%2==0]\n", + " yield (even)\n", + " \n", + "\n", + " \n", + "even5= even_iterator(5)\n", + "[i for i in even5]\n", + " \n", " \n", " # Sample Input: 5\n", " # Sample Output: iter([0, 2, 4])" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" - ] - }, { "cell_type": "code", "execution_count": null, @@ -382,7 +483,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +497,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..6c9e1ec 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,32 +21,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "num=76" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma del 1 hasta tu numero 76 es 2926\n" + ] + } + ], "source": [ "def summation(num):\n", - " # This function returns 1+2+3+....+num\n", - " \n", - " # Your code here: " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation" + " '''This function returns 1+2+3+....+num'''\n", + " suma= 0\n", + " for i in range(0,num+1):\n", + " suma=suma+i\n", + " return suma\n", + "\n", + "num=76\n", + "print(\"la suma del 1 hasta tu numero\", num, 'es', summation(num))" ] }, { @@ -60,32 +55,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "array=[3, 8, 16]" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de los numeros contenidos en tu array es: 27\n" + ] + } + ], + "source": [ + "array=[3, 8, 16]\n", "def summation2(*args):\n", - " # This function returns the sum of *args\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation2" + " '''This function returns the sum of *args'''\n", + " suma=sum(args)\n", + " return suma\n", + "\n", + "print(\"la suma de los numeros contenidos en tu array es: \", summation2(*array))\n", + "\n", + " " ] }, { @@ -97,23 +87,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma resultante es: 18\n" + ] + } + ], "source": [ "def summation3(a, b, c):\n", - " # This function returns a+b+c\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation3" + " '''This function returns a+b+c'''\n", + " suma= a+b+c\n", + " return suma\n", + "\n", + "print(\"la suma resultante es:\", summation3(5,4,9))" ] }, { @@ -125,32 +116,29 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de los numeros del diccionario que no son rojos es: 54\n" + ] + } + ], + "source": [ + "dictionary={'red':3, 'blue':8, 'green':24, 'orange':12, 'purple':10}\n", "def summation4(**kwargs):\n", - " # This function returns the sum of all M&Ms, except the red ones\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation4" + " '''This function returns the sum of all M&Ms, except the red ones'''\n", + " suma=0 \n", + " for i,j in kwargs.items():\n", + " if i!='red':\n", + " suma+=j\n", + " return suma\n", + "\n", + "print(\"la suma de los numeros del diccionario que no son rojos es: \", summation4(**dictionary))\n", + "\n" ] }, { @@ -162,36 +150,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "la suma de las variables es: 769\n" + ] + } + ], "source": [ "a=4\n", "b=6\n", "c=15\n", "d=[48, 465, 23, 96]\n", - "e={'A':16, 'B':32, 'C':64}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def summation5('define the function input'):\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:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print summation5" + "e={'A':16, 'B':32, 'C':64}\n", + "\n", + "def summation5(*args,**kwargs):\n", + " '''This function returns the sum of a,b,c,d and e\n", + " You need to define the function input'''\n", + " values= sum(kwargs.values())\n", + " suma_argumentos= sum(args)\n", + " suma= suma_argumentos+values\n", + " \n", + " return suma\n", + " \n", + "print(\"la suma de las variables es: \", summation5(a,b,c,*d,**e))\n", + " " ] }, { @@ -205,9 +192,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,9 +215,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", @@ -231,18 +234,38 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "print(next(iterator))" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 64, + "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 [64]\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 +274,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 59, + "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", @@ -269,35 +302,40 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In the cell below, write a function that takes an iterator and returns the first element in the iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." + "In the cell below, write a function that takes an iterator and returns the first element in the iterator that is divisible by 2. Assume that all iterators contain only numeric data. If we have not found a single element that is divisible by 2, return zero." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numero divisible de la lista: 0\n" + ] + } + ], "source": [ "iterator=iter([1,2,3])\n", "\n", "def divisible2(iterator):\n", - " # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", - " # Input: Iterable\n", - " # Output: Integer\n", - " \n", - " # Sample Input: iter([1,2,3])\n", - " # Sample Output: 2\n", - " \n", - " # Your code here:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print divisible2" + " '''\n", + " This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " Input: Iterable\n", + " Output: Integer\n", + " Sample Input: iter([1,2,3])\n", + " Sample Output: 2\n", + " '''\n", + " for i in iterator:\n", + " if i%2==0:\n", + " return i\n", + " else:\n", + " return 0\n", + "\n", + "print(\"numero divisible de la lista:\", divisible2(iterator))" ] }, { @@ -311,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [], "source": [ @@ -322,6 +360,44 @@ " number = number + 1" ] }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + }, + { + "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 [127]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(lis_fir)))\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(lis_fir)))\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43miter\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mlis_fir\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[1;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "lis_fir=firstn(5)\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir)))\n", + "print(next(iter(lis_fir))) #Error. Se acabaron los numeros contenidos en el objeto lis:fir\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -331,9 +407,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 108, + "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,28 +438,41 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 138, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[0, 2, 4]]" + ] + }, + "execution_count": 138, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def even_iterator(n):\n", - " # This function produces an iterator containing all even numbers between 0 and n\n", - " # Input: integer\n", - " # Output: iterator\n", + " '''\n", + " This function produces an iterator containing all even numbers between 0 and n\n", + " Input: integer\n", + " Output: iterator \n", + " \n", + " '''\n", + " even=[i for i in range(0, n) if i%2==0]\n", + " yield (even)\n", + " \n", + "\n", + " \n", + "even5= even_iterator(5)\n", + "[i for i in even5]\n", + " \n", " \n", " # Sample Input: 5\n", " # Sample Output: iter([0, 2, 4])" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" - ] - }, { "cell_type": "code", "execution_count": null, @@ -382,7 +483,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +497,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.10.3" } }, "nbformat": 4,