diff --git a/.ipynb_checkpoints/Functional_Programing-checkpoint.ipynb b/.ipynb_checkpoints/Functional_Programing-checkpoint.ipynb new file mode 100644 index 0000000..eecf1ac --- /dev/null +++ b/.ipynb_checkpoints/Functional_Programing-checkpoint.ipynb @@ -0,0 +1,437 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f7473ffc", + "metadata": {}, + "source": [ + "# Programacion Funcional" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cc424f45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 9\n" + ] + } + ], + "source": [ + "#Programacion procedimental\n", + "\n", + "a=[1,2,3,4]\n", + "b=[2,4,6,9]\n", + "for i, j in zip(a,b):\n", + " if i%2==0 and j%2==1:\n", + " print(i,j)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c930404a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "LETRAS\n" + ] + } + ], + "source": [ + "def letras():\n", + " variable='letras'\n", + " count = 0\n", + "\n", + " while count< 20:\n", + " variable=variable.upper()\n", + " count=20\n", + " return variable\n", + "\n", + "print(variable)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1b279411", + "metadata": {}, + "outputs": [], + "source": [ + "def nombre_de_funcion():\n", + " print('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f2f4871a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "nombre_de_funcion()" + ] + }, + { + "cell_type": "raw", + "id": "ae346b81", + "metadata": {}, + "source": [ + "limpieza_de_bd(BD)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37aef843", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "045a9be8", + "metadata": {}, + "source": [ + "# Namespaces y scope\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0ff6a8f5", + "metadata": {}, + "outputs": [], + "source": [ + "esta_es_una_variable_global=10\n", + "\n", + "def funcion_prueba():\n", + " print(esta_es_una_variable_global)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "30f676d8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "funcion_prueba()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "8bc335b9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42 global scope\n", + "7\n" + ] + } + ], + "source": [ + "def local():\n", + " chuchu= 7\n", + " print(chuchu)\n", + "\n", + "chuchu=42\n", + "\n", + "print(chuchu,'global scope')\n", + "\n", + "local()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "adc72b79", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42\n" + ] + } + ], + "source": [ + "numero=42\n", + "\n", + "def funcion(x):\n", + " print(x)\n", + " \n", + "funcion(numero)\n" + ] + }, + { + "cell_type": "markdown", + "id": "72f4346e", + "metadata": {}, + "source": [ + "https://pythontutor.com/visualize" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "7e0de02a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 42]\n" + ] + } + ], + "source": [ + "#procura no hacer esto, modificar variables globales\n", + "lista=[1,2,3]\n", + "\n", + "def funcion(lista):\n", + " lista[2]=42\n", + " \n", + "funcion(lista)\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "c832ece7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 3 4\n", + "3 6 7\n" + ] + } + ], + "source": [ + "def funcion_posicional(a,b,c):\n", + " print(a,b,c)\n", + " \n", + "funcion_posicional(2,3,4)\n", + "funcion_posicional(3,6,7)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "6e0b2fc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 4 6\n", + "10 5 1\n" + ] + } + ], + "source": [ + "#Keyword arguments\n", + "\n", + "def funcion_keywords(a,b,c):\n", + " print(a,b,c)\n", + " \n", + "funcion_keywords(a=2,b=4,c=6)\n", + "\n", + "funcion_keywords(b=5,c=1,a=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "ae3b5581", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 42 30\n", + "7 3 1\n", + "4 42 1\n", + "112 44 99\n" + ] + } + ], + "source": [ + "def funcion_keywords_2(a, b=42, c=30):\n", + " print(a,b,c)\n", + " \n", + "funcion_keywords_2(1)\n", + "funcion_keywords_2(b=3,a=7,c=1)\n", + "funcion_keywords_2(4,c=1)\n", + "funcion_keywords_2(112,44,99)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1938de80", + "metadata": {}, + "outputs": [], + "source": [ + "#Argumentos variables\n", + "def funcion_args(*args):#para enviar un objeto iterable\n", + " print(args)\n", + " a,b,c,d=args\n", + " \n", + " print(a+b+c+d)\n", + " \n", + " \n", + "valores = (1, 2, 3, 5)\n", + "\n", + "funcion_args(*valores)\n", + "\n", + "#funcion_args(valores)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "f233dd30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 1, 'b': 2}\n", + "{'a': 1, 'b': 2, 'ache': ' '}\n" + ] + } + ], + "source": [ + "#Keyword-only Arguments\n", + "\n", + "def funcion_kwargs(**kwargs):#Dloble * es para desempacar diccionarios\n", + " print(kwargs)\n", + " \n", + "diccionario ={'a':1,'b':2}\n", + "\n", + "funcion_kwargs(**diccionario)\n", + "#funcion_kwargs(diccionario) esto marca error\n", + "\n", + "funcion_kwargs(a=1, b=2, ache=' ')\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "8ef38d7f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'llave': 4}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict(llave=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "bc1c7105", + "metadata": {}, + "outputs": [], + "source": [ + "def funcion(entero):\n", + " print(entero**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "6b3ac725", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n" + ] + } + ], + "source": [ + "funcion(5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d7013c6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Functional_Programing.ipynb b/Functional_Programing.ipynb new file mode 100644 index 0000000..eecf1ac --- /dev/null +++ b/Functional_Programing.ipynb @@ -0,0 +1,437 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f7473ffc", + "metadata": {}, + "source": [ + "# Programacion Funcional" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cc424f45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 9\n" + ] + } + ], + "source": [ + "#Programacion procedimental\n", + "\n", + "a=[1,2,3,4]\n", + "b=[2,4,6,9]\n", + "for i, j in zip(a,b):\n", + " if i%2==0 and j%2==1:\n", + " print(i,j)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c930404a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "LETRAS\n" + ] + } + ], + "source": [ + "def letras():\n", + " variable='letras'\n", + " count = 0\n", + "\n", + " while count< 20:\n", + " variable=variable.upper()\n", + " count=20\n", + " return variable\n", + "\n", + "print(variable)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1b279411", + "metadata": {}, + "outputs": [], + "source": [ + "def nombre_de_funcion():\n", + " print('Hello World')" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f2f4871a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n" + ] + } + ], + "source": [ + "nombre_de_funcion()" + ] + }, + { + "cell_type": "raw", + "id": "ae346b81", + "metadata": {}, + "source": [ + "limpieza_de_bd(BD)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "37aef843", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "045a9be8", + "metadata": {}, + "source": [ + "# Namespaces y scope\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0ff6a8f5", + "metadata": {}, + "outputs": [], + "source": [ + "esta_es_una_variable_global=10\n", + "\n", + "def funcion_prueba():\n", + " print(esta_es_una_variable_global)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "30f676d8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "funcion_prueba()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "8bc335b9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42 global scope\n", + "7\n" + ] + } + ], + "source": [ + "def local():\n", + " chuchu= 7\n", + " print(chuchu)\n", + "\n", + "chuchu=42\n", + "\n", + "print(chuchu,'global scope')\n", + "\n", + "local()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "adc72b79", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "42\n" + ] + } + ], + "source": [ + "numero=42\n", + "\n", + "def funcion(x):\n", + " print(x)\n", + " \n", + "funcion(numero)\n" + ] + }, + { + "cell_type": "markdown", + "id": "72f4346e", + "metadata": {}, + "source": [ + "https://pythontutor.com/visualize" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "7e0de02a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 42]\n" + ] + } + ], + "source": [ + "#procura no hacer esto, modificar variables globales\n", + "lista=[1,2,3]\n", + "\n", + "def funcion(lista):\n", + " lista[2]=42\n", + " \n", + "funcion(lista)\n", + "print(lista)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "c832ece7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 3 4\n", + "3 6 7\n" + ] + } + ], + "source": [ + "def funcion_posicional(a,b,c):\n", + " print(a,b,c)\n", + " \n", + "funcion_posicional(2,3,4)\n", + "funcion_posicional(3,6,7)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "6e0b2fc9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 4 6\n", + "10 5 1\n" + ] + } + ], + "source": [ + "#Keyword arguments\n", + "\n", + "def funcion_keywords(a,b,c):\n", + " print(a,b,c)\n", + " \n", + "funcion_keywords(a=2,b=4,c=6)\n", + "\n", + "funcion_keywords(b=5,c=1,a=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "ae3b5581", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 42 30\n", + "7 3 1\n", + "4 42 1\n", + "112 44 99\n" + ] + } + ], + "source": [ + "def funcion_keywords_2(a, b=42, c=30):\n", + " print(a,b,c)\n", + " \n", + "funcion_keywords_2(1)\n", + "funcion_keywords_2(b=3,a=7,c=1)\n", + "funcion_keywords_2(4,c=1)\n", + "funcion_keywords_2(112,44,99)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1938de80", + "metadata": {}, + "outputs": [], + "source": [ + "#Argumentos variables\n", + "def funcion_args(*args):#para enviar un objeto iterable\n", + " print(args)\n", + " a,b,c,d=args\n", + " \n", + " print(a+b+c+d)\n", + " \n", + " \n", + "valores = (1, 2, 3, 5)\n", + "\n", + "funcion_args(*valores)\n", + "\n", + "#funcion_args(valores)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "f233dd30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 1, 'b': 2}\n", + "{'a': 1, 'b': 2, 'ache': ' '}\n" + ] + } + ], + "source": [ + "#Keyword-only Arguments\n", + "\n", + "def funcion_kwargs(**kwargs):#Dloble * es para desempacar diccionarios\n", + " print(kwargs)\n", + " \n", + "diccionario ={'a':1,'b':2}\n", + "\n", + "funcion_kwargs(**diccionario)\n", + "#funcion_kwargs(diccionario) esto marca error\n", + "\n", + "funcion_kwargs(a=1, b=2, ache=' ')\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "8ef38d7f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'llave': 4}" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict(llave=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "bc1c7105", + "metadata": {}, + "outputs": [], + "source": [ + "def funcion(entero):\n", + " print(entero**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "6b3ac725", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "25\n" + ] + } + ], + "source": [ + "funcion(5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d7013c6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index d68aeb6..0ce6df3 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -30,23 +30,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "def summation(num):\n", + "def summation(result=0):\n", + " for i in range(num+1):\n", + " result= result+i\n", + " \n", + " return result\n", " # This function returns 1+2+3+....+num\n", " \n", - " # Your code here: " + " # Your code here: \n", + "suma=summation(num)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3002\n" + ] + } + ], "source": [ - "# print summation" + "# print summation\n", + "print(suma)" ] }, { @@ -60,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +83,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", + " a,b,c=args\n", + " return a+b+c\n", " # This function returns the sum of *args\n", " \n", - " # Your code here:" + " # Your code here:\n", + "suma=summation2(*array)\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "print(suma)" ] }, { @@ -97,23 +125,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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", + "\n", + " # Your code here:\n", + "suma=summation3(*array)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "print(suma)" ] }, { @@ -125,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +173,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", + " suma=0\n", + " for item in kwargs.keys():\n", + " if item!='red':\n", + " suma+=kwargs[item]\n", + " return suma\n", " \n", - " # Your code here:" + " # Your code here:\n", + "suma=summation4(**dictionary)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "print(suma)" ] }, { @@ -162,7 +216,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +229,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a,b,c,*d,**e):\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:" + " suma=0\n", + " for valor in e.values():\n", + " suma+=valor\n", + " for i in d:\n", + " suma+=i\n", + " suma+=a+b+c\n", + " return suma\n", + " \n", + " # Your code here:\n", + "total=summation5(a,b,c,*d,**e)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "769\n" + ] + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "print(total)" ] }, { @@ -205,9 +276,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,29 +299,57 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", - "print(next(iterator))" + "print(next(iterator))\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ - "print(next(iterator))" + "print(next(iterator))\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "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_3268/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,9 +358,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "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,16 +391,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "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", + " # Input: Iterable\n", + "def divisible2(iterator): # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " for i in iterator:\n", + " if i%2==0:\n", + " return i\n", + " return 0\n", " \n", " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", @@ -293,11 +411,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "print(divisible2(iterator))" ] }, { @@ -311,14 +438,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def firstn(n):\n", " number = 0\n", " while number < n:\n", - " yield number\n", + " yield number #construye iterador\n", " number = number + 1" ] }, @@ -331,9 +458,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 23, + "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,11 +489,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def even_iterator(n):\n", + " for number in range(n):\n", + " if number%2==0:\n", + " yield number #construye iterador\n", + " return number\n", " # This function produces an iterator containing all even numbers between 0 and n\n", " # Input: integer\n", " # Output: iterator\n", @@ -365,24 +508,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "# print even_iterator\n", + "iterator=even_iterator(5)\n", + "for i in iterator:\n", + " print(i)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +545,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..0ce6df3 100755 --- 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,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "def summation(num):\n", + "def summation(result=0):\n", + " for i in range(num+1):\n", + " result= result+i\n", + " \n", + " return result\n", " # This function returns 1+2+3+....+num\n", " \n", - " # Your code here: " + " # Your code here: \n", + "suma=summation(num)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3002\n" + ] + } + ], "source": [ - "# print summation" + "# print summation\n", + "print(suma)" ] }, { @@ -60,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -69,23 +83,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def summation2(*args):\n", + " a,b,c=args\n", + " return a+b+c\n", " # This function returns the sum of *args\n", " \n", - " # Your code here:" + " # Your code here:\n", + "suma=summation2(*array)\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation2" + "# print summation2\n", + "print(suma)" ] }, { @@ -97,23 +125,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "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", + "\n", + " # Your code here:\n", + "suma=summation3(*array)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "27\n" + ] + } + ], "source": [ - "# print summation3" + "# print summation3\n", + "print(suma)" ] }, { @@ -125,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -134,23 +173,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def summation4(**kwargs):\n", " # This function returns the sum of all M&Ms, except the red ones\n", + " suma=0\n", + " for item in kwargs.keys():\n", + " if item!='red':\n", + " suma+=kwargs[item]\n", + " return suma\n", " \n", - " # Your code here:" + " # Your code here:\n", + "suma=summation4(**dictionary)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "54\n" + ] + } + ], "source": [ - "# print summation4" + "# print summation4\n", + "print(suma)" ] }, { @@ -162,7 +216,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -175,23 +229,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "def summation5('define the function input'):\n", + "def summation5(a,b,c,*d,**e):\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:" + " suma=0\n", + " for valor in e.values():\n", + " suma+=valor\n", + " for i in d:\n", + " suma+=i\n", + " suma+=a+b+c\n", + " return suma\n", + " \n", + " # Your code here:\n", + "total=summation5(a,b,c,*d,**e)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "769\n" + ] + } + ], "source": [ - "# print summation5" + "# print summation5\n", + "print(total)" ] }, { @@ -205,9 +276,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "# We first define our iterator:\n", "\n", @@ -220,29 +299,57 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ "# We continue to iterate through the iterator.\n", "\n", - "print(next(iterator))" + "print(next(iterator))\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ - "print(next(iterator))" + "print(next(iterator))\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 18, + "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_3268/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,9 +358,19 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 19, + "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,16 +391,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], "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", + " # Input: Iterable\n", + "def divisible2(iterator): # This function takes an iterable and returns the first element that is divisible by 2 and zero otherwise\n", + " for i in iterator:\n", + " if i%2==0:\n", + " return i\n", + " return 0\n", " \n", " # Sample Input: iter([1,2,3])\n", " # Sample Output: 2\n", @@ -293,11 +411,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], "source": [ - "# print divisible2" + "# print divisible2\n", + "print(divisible2(iterator))" ] }, { @@ -311,14 +438,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def firstn(n):\n", " number = 0\n", " while number < n:\n", - " yield number\n", + " yield number #construye iterador\n", " number = number + 1" ] }, @@ -331,9 +458,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 23, + "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,11 +489,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "def even_iterator(n):\n", + " for number in range(n):\n", + " if number%2==0:\n", + " yield number #construye iterador\n", + " return number\n", " # This function produces an iterator containing all even numbers between 0 and n\n", " # Input: integer\n", " # Output: iterator\n", @@ -365,24 +508,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# print even_iterator" + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "2\n", + "4\n" + ] + } + ], + "source": [ + "# print even_iterator\n", + "iterator=even_iterator(5)\n", + "for i in iterator:\n", + " print(i)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -396,7 +545,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.9.6" } }, "nbformat": 4,