From 66522d267ede2e8afd881651af4a513e4508e53a Mon Sep 17 00:00:00 2001 From: foux98 <156951108+foux98@users.noreply.github.com> Date: Sat, 8 Jun 2024 11:36:53 +0200 Subject: [PATCH 1/3] Add files via upload --- lab_lambda_functions-main/README.md | 26 + .../your-code/lambda_x.ipynb | 569 ++++++++++++++++++ 2 files changed, 595 insertions(+) create mode 100644 lab_lambda_functions-main/README.md create mode 100644 lab_lambda_functions-main/your-code/lambda_x.ipynb diff --git a/lab_lambda_functions-main/README.md b/lab_lambda_functions-main/README.md new file mode 100644 index 0000000..f428483 --- /dev/null +++ b/lab_lambda_functions-main/README.md @@ -0,0 +1,26 @@ +![Ironhack logo](https://i.imgur.com/1QgrNNw.png) + +# Lab | Lambda Functions + +## Introduction + +As a python developer, you need the right tool for the right job. In this lab, you will explore a number of scenarios where lambda functions are the best tool for the job. + +## Getting Started + +Open the `main.ipynb` file in the `your-code` directory. Follow the instructions and add your code and explanations as necessary. By the end of this lab, you will have learned about the different uses and advantages of lambda functions. + +## Deliverables + +- `main.ipynb` with your responses. + +## Submission + +Upon completion, add your deliverables to git. Then commit git and push your branch to the remote. + +## Resources + +[Lambda Expressions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) + +[The Python zip function](https://docs.python.org/3.3/library/functions.html#zip) + diff --git a/lab_lambda_functions-main/your-code/lambda_x.ipynb b/lab_lambda_functions-main/your-code/lambda_x.ipynb new file mode 100644 index 0000000..f0ea138 --- /dev/null +++ b/lab_lambda_functions-main/your-code/lambda_x.ipynb @@ -0,0 +1,569 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge - Passing a Lambda Expression to a Function\n", + "\n", + "In the next excercise you will create a function that returns a lambda expression. Create a function called `modify_list`. The function takes two arguments, a list and a lambda expression. The function iterates through the list and applies the lambda expression to every element in the list." + ] + }, + { + "cell_type": "code", + "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", + "#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", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now we will define a lambda expression that will transform the elements of the list. \n", + "\n", + "In the cell below, create a lambda expression that converts Celsius to Kelvin. Recall that 0°C + 273.15 = 273.15K" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "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", + "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))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### In this part, we will define a function that returns a lambda expression\n", + "\n", + "In the cell below, write a lambda expression that takes two numbers and returns 1 if one is divisible by the other and zero otherwise. Call the lambda expression `mod`." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "#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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now create a function that returns mod. The function only takes one argument - the first number in the `mod` lambda function. \n", + "\n", + "Note: the lambda function above took two arguments, the lambda function in the return statement only takes one argument but also uses the argument passed to the function." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "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\n", + " \n", + " return mod (a, b=5)\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisor(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisor(8)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Using Lambda Expressions in List Comprehensions\n", + "\n", + "In the following challenge, we will combine two lists using a lambda expression in a list comprehension. \n", + "\n", + "To do this, we will need to introduce the `zip` function. The `zip` function returns an iterator of tuples." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1,), (2,), (3,), (4,), (5,)]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Here is an example of passing one list to the zip function. \n", + "# Since the zip function returns an iterator, we need to evaluate the iterator by using a list comprehension.\n", + "\n", + "l = [1,2,3,4,5]\n", + "[x for x in zip(l)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using the `zip` function, let's iterate through two lists and add the elements by position." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "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\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]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Using Lambda Expressions as Arguments\n", + "\n", + "#### In this challenge, we will zip together two lists and sort by the resulting tuple.\n", + "\n", + "In the cell below, take the two lists provided, zip them together and sort by the first letter of the second element of each tuple. Do this using a lambda function." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [], + "source": [ + "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "# your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Sort a Dictionary by Values\n", + "\n", + "Given the dictionary below, sort it by values rather than by keys. Use a lambda function to specify the values as a sorting key." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "\n", + "# 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 (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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From fb0187cfba7112cf1986e8aeb6b910b803701a88 Mon Sep 17 00:00:00 2001 From: foux98 <156951108+foux98@users.noreply.github.com> Date: Sat, 8 Jun 2024 12:47:49 +0200 Subject: [PATCH 2/3] lab_lambda_functions done --- your-code/main.ipynb | 368 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 330 insertions(+), 38 deletions(-) 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, From 6ecd7b82d4f5c30f9dc5f02a83a759aaf4b95d76 Mon Sep 17 00:00:00 2001 From: foux98 <156951108+foux98@users.noreply.github.com> Date: Sat, 8 Jun 2024 12:48:54 +0200 Subject: [PATCH 3/3] Delete lab_lambda_functions-main directory --- lab_lambda_functions-main/README.md | 26 - .../your-code/lambda_x.ipynb | 569 ------------------ 2 files changed, 595 deletions(-) delete mode 100644 lab_lambda_functions-main/README.md delete mode 100644 lab_lambda_functions-main/your-code/lambda_x.ipynb diff --git a/lab_lambda_functions-main/README.md b/lab_lambda_functions-main/README.md deleted file mode 100644 index f428483..0000000 --- a/lab_lambda_functions-main/README.md +++ /dev/null @@ -1,26 +0,0 @@ -![Ironhack logo](https://i.imgur.com/1QgrNNw.png) - -# Lab | Lambda Functions - -## Introduction - -As a python developer, you need the right tool for the right job. In this lab, you will explore a number of scenarios where lambda functions are the best tool for the job. - -## Getting Started - -Open the `main.ipynb` file in the `your-code` directory. Follow the instructions and add your code and explanations as necessary. By the end of this lab, you will have learned about the different uses and advantages of lambda functions. - -## Deliverables - -- `main.ipynb` with your responses. - -## Submission - -Upon completion, add your deliverables to git. Then commit git and push your branch to the remote. - -## Resources - -[Lambda Expressions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) - -[The Python zip function](https://docs.python.org/3.3/library/functions.html#zip) - diff --git a/lab_lambda_functions-main/your-code/lambda_x.ipynb b/lab_lambda_functions-main/your-code/lambda_x.ipynb deleted file mode 100644 index f0ea138..0000000 --- a/lab_lambda_functions-main/your-code/lambda_x.ipynb +++ /dev/null @@ -1,569 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Before your start:\n", - "- Read the README.md file\n", - "- Comment as much as you can and use the resources in the README.md file\n", - "- Happy learning!" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Challenge - Passing a Lambda Expression to a Function\n", - "\n", - "In the next excercise you will create a function that returns a lambda expression. Create a function called `modify_list`. The function takes two arguments, a list and a lambda expression. The function iterates through the list and applies the lambda expression to every element in the list." - ] - }, - { - "cell_type": "code", - "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", - "#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", - " " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Now we will define a lambda expression that will transform the elements of the list. \n", - "\n", - "In the cell below, create a lambda expression that converts Celsius to Kelvin. Recall that 0°C + 273.15 = 273.15K" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "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)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Finally, convert the list of temperatures below from Celsius to Kelvin." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "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", - "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))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### In this part, we will define a function that returns a lambda expression\n", - "\n", - "In the cell below, write a lambda expression that takes two numbers and returns 1 if one is divisible by the other and zero otherwise. Call the lambda expression `mod`." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "#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)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Now create a function that returns mod. The function only takes one argument - the first number in the `mod` lambda function. \n", - "\n", - "Note: the lambda function above took two arguments, the lambda function in the return statement only takes one argument but also uses the argument passed to the function." - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": {}, - "outputs": [], - "source": [ - "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\n", - " \n", - " return mod (a, b=5)\n", - " \n", - " \n", - " " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "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": "markdown", - "metadata": {}, - "source": [ - "Test your function with the following test cases:" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "divisor(10)" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "divisor(8)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bonus Challenge - Using Lambda Expressions in List Comprehensions\n", - "\n", - "In the following challenge, we will combine two lists using a lambda expression in a list comprehension. \n", - "\n", - "To do this, we will need to introduce the `zip` function. The `zip` function returns an iterator of tuples." - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1,), (2,), (3,), (4,), (5,)]" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Here is an example of passing one list to the zip function. \n", - "# Since the zip function returns an iterator, we need to evaluate the iterator by using a list comprehension.\n", - "\n", - "l = [1,2,3,4,5]\n", - "[x for x in zip(l)]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Using the `zip` function, let's iterate through two lists and add the elements by position." - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": {}, - "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\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]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bonus Challenge - Using Lambda Expressions as Arguments\n", - "\n", - "#### In this challenge, we will zip together two lists and sort by the resulting tuple.\n", - "\n", - "In the cell below, take the two lists provided, zip them together and sort by the first letter of the second element of each tuple. Do this using a lambda function." - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [], - "source": [ - "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", - "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", - "\n", - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bonus Challenge - Sort a Dictionary by Values\n", - "\n", - "Given the dictionary below, sort it by values rather than by keys. Use a lambda function to specify the values as a sorting key." - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [], - "source": [ - "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", - "\n", - "# 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 (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.11.5" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}