diff --git a/your-code/main.ipynb b/your-code/.ipynb_checkpoints/Lab_lambda functions-checkpoint.ipynb similarity index 63% rename from your-code/main.ipynb rename to your-code/.ipynb_checkpoints/Lab_lambda functions-checkpoint.ipynb index 66a9984..b9a9c22 100644 --- a/your-code/main.ipynb +++ b/your-code/.ipynb_checkpoints/Lab_lambda functions-checkpoint.ipynb @@ -34,18 +34,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]\n" + ] + } + ], "source": [ - "l = [######]\n", - "f = lambda x: #define the lambda expression\n", - "b = []\n", - "def modify_list(lst, fudduLambda):\n", - " for x in ####:\n", - " b.append(#####(x))\n", - "#Call modify_list(##,##)\n", - "#print b" + "import random \n", + "l = [1,2,3,4,5,6,7,8,9,10]\n", + "f = lambda x: x+100\n", + "\n", + "def modify_list(l,f):\n", + " lista= [f(valor) for valor in l]\n", + " return lista\n", + " \n", + "print(modify_list(l,f))" ] }, { @@ -59,12 +68,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "273.15" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n", - "\n" + "kel= (lambda x: x+273.15)\n", + "kel(0)\n" ] }, { @@ -76,13 +96,21 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n" + ] + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", - "\n", - "# Your code here:" + "keltemp=[(kel(i)) for i in temps]\n", + "print(keltemp)" ] }, { @@ -96,11 +124,24 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod= (lambda x,y: '1' if x%y==0 else '0')\n", + "mod(2,2)" ] }, { @@ -114,17 +155,30 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'0'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisor(b):\n", " \"\"\"\n", " input: a number\n", " output: a function that returns 1 if the number is divisible by another number (to be passed later) and zero otherwise\n", " \"\"\"\n", - " \n", - " # Your code here:" + " #other_num= int(input())\n", + " return mod(2,b)\n", + "\n", + "divisor(5)" ] }, { @@ -136,11 +190,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 65, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "def divisible5(x): \n", + " return (mod(x,5))\n", + "\n", + "divisible5(30)\n" ] }, { @@ -152,18 +220,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'0'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -218,14 +308,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 86, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]\n", + "['False', 'False', 'False', 'False']\n" + ] + } + ], "source": [ "list1 = [1,2,3,4]\n", "list2 = [2,3,4,5]\n", - "## Zip the lists together \n", - "## Print the zipped list " + "\n", + "print(list(zip(list1,list2)))\n", + "print(['true' if elemento_lista_1>elemento_lista_2 else 'False' for elemento_lista_1,elemento_lista_2 in zip(list1,list2)])\n" ] }, { @@ -237,28 +337,36 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 107, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'\\n\\ncompare = lambda ###: print(\"True\") if ### else print(\"False\")\\nfor ### in zip(list1,list2):\\n compare(###)\\n \\n'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "False\n", + "False\n" + ] } ], "source": [ "'''\n", "\n", "compare = lambda ###: print(\"True\") if ### else print(\"False\")\n", + "\n", + "\n", "for ### in zip(list1,list2):\n", " compare(###)\n", " \n", - "''' " + "''' \n", + "\n", + "\n", + "compare = lambda i,j: print(\"True\") if i>j else print(\"False\")\n", + "for i,j in zip(list1,list2):\n", + " compare(i,j)\n", + " \n" ] }, { @@ -274,14 +382,29 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 116, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['L', 'H', 'E', 'M']" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# Your code here:\n" + "sorting= lambda i,j: j[0]\n", + "a= [sorting(i,j) for i,j in zip(list1,list2)] \n", + "\n", + "a \n", + " " ] }, { @@ -295,26 +418,55 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:" + "#resolucion 1\n", + "acomodar= sorted(d.items(), key=lambda x: x[1])\n", + "print(acomodar)\n", + "\n", + " \n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function sorted in module builtins:\n", + "\n", + "sorted(iterable, /, *, key=None, reverse=False)\n", + " Return a new list containing all items from the iterable in ascending order.\n", + " \n", + " A custom key function can be supplied to customize the sort order, and the\n", + " reverse flag can be set to request the result in descending order.\n", + "\n" + ] + } + ], + "source": [ + "help (sorted)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -328,7 +480,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/Lab_lambda functions.ipynb b/your-code/Lab_lambda functions.ipynb new file mode 100644 index 0000000..b9a9c22 --- /dev/null +++ b/your-code/Lab_lambda functions.ipynb @@ -0,0 +1,488 @@ +{ + "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 1 - 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": "raw", + "metadata": {}, + "source": [ + "Follow the steps as stated below:\n", + " 1. Define a list of any 10 numbers\n", + " 2. Define a simple lambda expression for eg that updates a number by 2\n", + " 3. Define an empty list\n", + " 4. Define the function -> use the lambda function to append the empty list\n", + " 5. Call the function with list and lambda expression\n", + " 6. print the updated list " + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]\n" + ] + } + ], + "source": [ + "import random \n", + "l = [1,2,3,4,5,6,7,8,9,10]\n", + "f = lambda x: x+100\n", + "\n", + "def modify_list(l,f):\n", + " lista= [f(valor) for valor in l]\n", + " return lista\n", + " \n", + "print(modify_list(l,f))" + ] + }, + { + "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": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "273.15" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "kel= (lambda x: x+273.15)\n", + "kel(0)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n" + ] + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "keltemp=[(kel(i)) for i in temps]\n", + "print(keltemp)" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "mod= (lambda x,y: '1' if x%y==0 else '0')\n", + "mod(2,2)" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divisor(b):\n", + " \"\"\"\n", + " input: a number\n", + " output: a function that returns 1 if the number is divisible by another number (to be passed later) and zero otherwise\n", + " \"\"\"\n", + " #other_num= int(input())\n", + " return mod(2,b)\n", + "\n", + "divisor(5)" + ] + }, + { + "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": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divisible5(x): \n", + " return (mod(x,5))\n", + "\n", + "divisible5(30)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1'" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'0'" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(8)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - 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.\n", + "\n", + "The way zip function works with list has been shown below:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = ['Green', 'cheese', 'English', 'tomato']\n", + "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", + "zipped = zip(list1,list2)\n", + "list(zipped)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this exercise we will try to compare the elements on the same index in the two lists. \n", + "We want to zip the two lists and then use a lambda expression to compare if:\n", + "list1 element > list2 element " + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]\n", + "['False', 'False', 'False', 'False']\n" + ] + } + ], + "source": [ + "list1 = [1,2,3,4]\n", + "list2 = [2,3,4,5]\n", + "\n", + "print(list(zip(list1,list2)))\n", + "print(['true' if elemento_lista_1>elemento_lista_2 else 'False' for elemento_lista_1,elemento_lista_2 in zip(list1,list2)])\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Complete the parts of the code marked as \"###\"" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "False\n", + "False\n" + ] + } + ], + "source": [ + "'''\n", + "\n", + "compare = lambda ###: print(\"True\") if ### else print(\"False\")\n", + "\n", + "\n", + "for ### in zip(list1,list2):\n", + " compare(###)\n", + " \n", + "''' \n", + "\n", + "\n", + "compare = lambda i,j: print(\"True\") if i>j else print(\"False\")\n", + "for i,j in zip(list1,list2):\n", + " compare(i,j)\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - 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": 116, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['L', 'H', 'E', 'M']" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "sorting= lambda i,j: j[0]\n", + "a= [sorting(i,j) for i,j in zip(list1,list2)] \n", + "\n", + "a \n", + " " + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]\n" + ] + } + ], + "source": [ + "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "\n", + "#resolucion 1\n", + "acomodar= sorted(d.items(), key=lambda x: x[1])\n", + "print(acomodar)\n", + "\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function sorted in module builtins:\n", + "\n", + "sorted(iterable, /, *, key=None, reverse=False)\n", + " Return a new list containing all items from the iterable in ascending order.\n", + " \n", + " A custom key function can be supplied to customize the sort order, and the\n", + " reverse flag can be set to request the result in descending order.\n", + "\n" + ] + } + ], + "source": [ + "help (sorted)" + ] + } + ], + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}