diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 50c3c11..f0ea138 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,17 +21,38 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ + "#la función modify_list toma dos argumentos: una lista (lst) y una expresión lambda (lmbda\n", "def modify_list(lst, lmbda):\n", " \"\"\"\n", " Input: list and lambda expression\n", " Output: the transformed list\n", " \"\"\"\n", + "#res = []: Se inicializa una lista vacía res que se usará para almacenar los resultados transformados.\n", + "\n", + " res = []\n", " \n", - " # your code here\n", + "#for e in lst:: Se itera sobre cada elemento e en la lista lst.\n", + "\n", + " for e in lst:\n", + "\n", + "#n_temp = lmbda(e): La expresión lambda lmbda se aplica al elemento e. Esto significa que la función definida por la expresión lambda se evalúa con e como argumento, y el resultado se almacena en n_temp.\n", + "\n", + " n_temp = lmbda(e)\n", + " \n", + "#Redondeo del resultado: (Se redondea dos decimales, usando round)\n", + "\n", + " n_temp = round(n_temp, 2)\n", + " \n", + "#Agregar el resultado a la lista\n", + "\n", + " res.append(n_temp)\n", + " \n", + " \n", + " return res\n", " " ] }, @@ -46,11 +67,32 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "temps = [12, 23, 38, -55, 24]" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "296.15" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = lambda x: x + 273.15\n", + "l(23)" ] }, { @@ -62,13 +104,41 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.15, 297.15]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", - "\n", - "# your code here" + "modify_list(temps, l)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "map(lambda x : x+273.15, temps)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "list(map(lambda x : round(x+273.15, 2), temps))" ] }, { @@ -82,11 +152,52 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "#a % b == 0: Verifica si a es divisible por b. El operador % es el operador de módulo y devuelve el resto de la división de a por b. Si el resto es 0, significa que a es divisible por b.\n", + "mod = lambda a, b : 1 if a%b==0 or b%a==0 else 0" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(2, 20)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(20, 3)" ] }, { @@ -100,18 +211,23 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ - "def divisor(b):\n", + "def divisor(a):\n", " \"\"\"\n", " Input: a number\n", " Output: a function that returns 1 if the number is \n", " divisible by another number (to be passed later) and zero otherwise.\n", " \"\"\"\n", " \n", - " # your code here" + " # your code here\n", + " \n", + " return mod (a, b=5)\n", + " \n", + " \n", + " " ] }, { @@ -121,15 +237,6 @@ "Finally, pass the number 5 to `divisor`. Now the function will check whether a number is divisble by 5. Assign this function to `divisible5`" ] }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -139,20 +246,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisor(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(8)" + "divisor(8)" ] }, { @@ -168,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 45, "metadata": {}, "outputs": [ { @@ -177,7 +306,7 @@ "[(1,), (2,), (3,), (4,), (5,)]" ] }, - "execution_count": 10, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" } @@ -199,14 +328,108 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs', 'Green'),\n", + " ('cheese', 'cheese', 'cheese'),\n", + " ('English', 'cucumber', 'English'),\n", + " ('tomato', 'tomato', 'tomato')]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = ['Green', 'cheese', 'English', 'tomato']\n", "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", "\n", - "# your code here" + "# your code here\n", + "\n", + "zip (list1, list2)\n", + "\n", + "list(zip(list1, list2))\n", + "\n", + "list(zip(list1,list2,list1))" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "zipea = lambda lst1, lst2: [(lst1[i], lst2[i]) for i in range(len(lst1))] " + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "zipea(list1, list2)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Green_eggs', 'cheese_cheese', 'English_cucumber', 'tomato_tomato']" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "zipea2 = lambda lst1, lst2: [lst1[i] + '_' + lst2[i] for i in range(len(lst1))] \n", + "\n", + "zipea2(list1, list2)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Green_eggs'" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1[0] + '_' + list2[0]" ] }, { @@ -222,7 +445,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ @@ -243,19 +466,88 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# your code here" + "# your code here\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Audi', 2001), ('BMW', 2005), ('Honda', 1997), ('Toyota', 1995)]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(d.items())" ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Audi': 2001, 'BMW': 2005, 'Honda': 1997, 'Toyota': 1995}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict(sorted(d.items()))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted (d.items(), key=lambda x: x[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -269,7 +561,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.11.5" } }, "nbformat": 4,