diff --git a/Lambda.ipynb b/Lambda.ipynb new file mode 100644 index 0000000..56f6671 --- /dev/null +++ b/Lambda.ipynb @@ -0,0 +1,416 @@ +{ + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "map(lambda x: x+273.15, temps)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def modify_list(lst, lmbda):\n", + " \"\"\"\n", + " Input: list and lambda expression\n", + " Output: the transformed list\n", + " \"\"\"\n", + " \n", + " # your code here\n", + " lambda x: x+273.15\n", + " res = ()\n", + " \n", + " for e in lst:\n", + " res = lst.append()\n", + " \n", + " for e in lmbda:\n", + " res = lmbda.append()" + ] + }, + { + "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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here\n", + "lamb = map(lambda x: x+273.15, temps)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "\n", + "# your code here\n", + "modify_list(temps, lamb)" + ] + }, + { + "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": 16, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here\n", + "mod = lambda x,y: 1 if x/y==0 or y/x==0 else 0" + ] + }, + { + "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": 25, + "metadata": {}, + "outputs": [], + "source": [ + "def mod(a):\n", + " return mod\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "def divisor(b):\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", + " mod = lambda a,b : 1 if a%b==0 or b%a==0 else 0\n", + " return mod(a, b=5)\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": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here\n", + "divisible5 = divisor" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for %: 'type' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[39], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m divisible5(\u001b[38;5;241m10\u001b[39m)\n", + "Cell \u001b[1;32mIn[34], line 10\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# your code here\u001b[39;00m\n\u001b[0;32m 9\u001b[0m mod \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m a,b : \u001b[38;5;241m1\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m%\u001b[39mb\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m b\u001b[38;5;241m%\u001b[39ma\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m---> 10\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m mod(a\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m)\n", + "Cell \u001b[1;32mIn[34], line 9\u001b[0m, in \u001b[0;36mdivisor..\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124;03mInput: a number\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;124;03mOutput: a function that returns 1 if the number is \u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03mdivisible by another number (to be passed later) and zero otherwise.\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# your code here\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m mod \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m a,b : \u001b[38;5;241m1\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m%\u001b[39mb\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m b\u001b[38;5;241m%\u001b[39ma\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m mod(a\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m)\n", + "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for %: 'type' and 'int'" + ] + } + ], + "source": [ + "divisible5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for %: 'type' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[40], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m divisible5(\u001b[38;5;241m8\u001b[39m)\n", + "Cell \u001b[1;32mIn[34], line 10\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# your code here\u001b[39;00m\n\u001b[0;32m 9\u001b[0m mod \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m a,b : \u001b[38;5;241m1\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m%\u001b[39mb\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m b\u001b[38;5;241m%\u001b[39ma\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m---> 10\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m mod(a\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m)\n", + "Cell \u001b[1;32mIn[34], line 9\u001b[0m, in \u001b[0;36mdivisor..\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124;03mInput: a number\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;124;03mOutput: a function that returns 1 if the number is \u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03mdivisible by another number (to be passed later) and zero otherwise.\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# your code here\u001b[39;00m\n\u001b[1;32m----> 9\u001b[0m mod \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m a,b : \u001b[38;5;241m1\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m%\u001b[39mb\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m b\u001b[38;5;241m%\u001b[39ma\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m mod(a\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mint\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m)\n", + "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for %: 'type' and 'int'" + ] + } + ], + "source": [ + "divisible5(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": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(list1, list2)>" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lambda list1, list2: [(res for i in range(len(list1)))]" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1,), (2,), (3,), (4,), (5,)]" + ] + }, + "execution_count": 42, + "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": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = ['Green', 'cheese', 'English', 'tomato']\n", + "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", + "\n", + "# your code here\n", + "zipeo = [x for x in zip(list1,list2)]\n", + "zipeo" + ] + }, + { + "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": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 52, + "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", + "\n", + "zipped = [x for x in zip(list1,list2)]\n", + "ordenado = sorted(zipped, key=lambda zipped: zipped[1])\n", + "ordenado" + ] + }, + { + "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": 6, + "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", + "# your code here\n", + "d2 = sorted((d.items()), key = lambda x: x[1])\n", + "print(d2)" + ] + }, + { + "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 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb deleted file mode 100644 index 50c3c11..0000000 --- a/your-code/main.ipynb +++ /dev/null @@ -1,277 +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": 1, - "metadata": {}, - "outputs": [], - "source": [ - "def modify_list(lst, lmbda):\n", - " \"\"\"\n", - " Input: list and lambda expression\n", - " Output: the transformed list\n", - " \"\"\"\n", - " \n", - " # your code here\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": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Finally, convert the list of temperatures below from Celsius to Kelvin." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "temps = [12, 23, 38, -55, 24]\n", - "\n", - "# your code here" - ] - }, - { - "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": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "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": 6, - "metadata": {}, - "outputs": [], - "source": [ - "def divisor(b):\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" - ] - }, - { - "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": 7, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Test your function with the following test cases:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "divisible5(10)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "divisible5(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": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[(1,), (2,), (3,), (4,), (5,)]" - ] - }, - "execution_count": 10, - "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": 11, - "metadata": {}, - "outputs": [], - "source": [ - "list1 = ['Green', 'cheese', 'English', 'tomato']\n", - "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", - "\n", - "# your code here" - ] - }, - { - "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": 1, - "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": 13, - "metadata": {}, - "outputs": [], - "source": [ - "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", - "\n", - "# your code here" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}