From 612febe95dfd6269365e4781891dfb0a4bba713c Mon Sep 17 00:00:00 2001 From: fedecavanagh Date: Thu, 27 Oct 2022 21:34:40 +0200 Subject: [PATCH] FedericoCavanagh --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 438 +++++++++++ your-code/main.ipynb | 742 +++++++++++------- 2 files changed, 876 insertions(+), 304 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/main-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..275d94a --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,438 @@ +{ + "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": "markdown", + "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": 10, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[95, 80, 70, 97, 26, 64, 15, 8, 2, 4]\n" + ] + }, + { + "data": { + "text/plain": [ + "[97, 82, 72, 99, 28, 66, 17, 10, 4, 6]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "\n", + "list_a = random.sample(range(0, 100), 10)\n", + "function = (lambda x: x+2)\n", + "list_b = []\n", + "\n", + "def modify_list(xxx):\n", + " print(list_a)\n", + " for number in xxx:\n", + " list_b.append(function(number))\n", + " return list_b\n", + "\n", + "modify_list(list_a)" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "function2= lambda x : x+273.15" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[12, 23, 38, -55, 24]\n" + ] + }, + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.15, 297.15]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "temps_k = []\n", + "\n", + "# Your code here:\n", + "\n", + "def c_to_k(list):\n", + " print(temps)\n", + " for temperature in list:\n", + " temps_k.append(round(function2(temperature),2))\n", + " return temps_k\n", + "\n", + "c_to_k(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": [ + "# Your code here:\n", + "\n", + "mod = lambda a,b : 1 if a%b == 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": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def divisor(a):\n", + " return (lambda b : 1 if b%a == 0 else 0)\n", + "\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", + " \"\"\"" + ] + }, + { + "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": 47, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "divisible5 = divisor(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 49, + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Green', 'eggs'), ('cheese', 'cheese'), ('English', 'cucumber'), ('tomato', 'tomato')]\n" + ] + } + ], + "source": [ + "list1 = ['Green', 'cheese', 'English', 'tomato']\n", + "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", + "\n", + "zipped = zip(list1,list2)\n", + "zipped = list(zipped)\n", + "print(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": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zipped list: [(1, 2), (2, 3), (4, 3), (4, 5)]\n", + "greater element from list: [2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "list3 = [1,2,4,4]\n", + "list4 = [2,3,3,5]\n", + "\n", + "## Zip the lists together \n", + "zipped2 = list(zip(list3, list4))\n", + "\n", + "## Print the zipped list\n", + "print(\"zipped list:\", zipped2)\n", + "\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "print(\"greater element from list:\", [(lambda x,y: x if x>y else y)(x,y) for x,y in zipped2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Complete the parts of the code marked as \"###\"" + ] + }, + { + "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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zipped list: {'Engineering': 'Lab', 'Computer Science': 'Homework', 'Political Science': 'Essay', 'Mathematics': 'Module'}\n", + "Sorted: {'Political Science': 'Essay', 'Computer Science': 'Homework', 'Engineering': 'Lab', 'Mathematics': 'Module'}\n" + ] + } + ], + "source": [ + "list5 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list6 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "# Your code here:\n", + "\n", + "new_list = dict(zip(list5, list6))\n", + "print(\"zipped list:\", new_list)\n", + "\n", + "sort_list= dict(sorted(new_list.items(), key=lambda item: item[1])) \n", + "print(\"Sorted: \", sort_list)" + ] + }, + { + "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": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original Dictionary: {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "Year Sorted Dictionary: {'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", + "\n", + "print(\"Original Dictionary: \", d)\n", + "sort_dict= dict(sorted(d.items(), key=lambda item: item[1])) \n", + "print(\"Year Sorted Dictionary: \", sort_dict)" + ] + }, + { + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 38b51a7..275d94a 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -1,304 +1,438 @@ -{ - "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": "markdown", - "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": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# 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:\n" - ] - }, - { - "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:\n" - ] - }, - { - "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:\n" - ] - }, - { - "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(a):\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", - " # Your code here:\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": 7, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" - ] - }, - { - "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": [ - "# 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": 9, - "metadata": {}, - "outputs": [], - "source": [ - "list1 = [1,2,4,4]\n", - "list2 = [2,3,3,5]\n", - "## Zip the lists together \n", - "\n", - "## Print the zipped list \n", - "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Complete the parts of the code marked as \"###\"" - ] - }, - { - "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": 10, - "metadata": {}, - "outputs": [], - "source": [ - "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", - "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", - "\n", - "# Your code here:\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": 11, - "metadata": {}, - "outputs": [], - "source": [ - "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", - "\n", - "# Your code here:\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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.3" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} +{ + "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": "markdown", + "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": 10, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import random" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[95, 80, 70, 97, 26, 64, 15, 8, 2, 4]\n" + ] + }, + { + "data": { + "text/plain": [ + "[97, 82, 72, 99, 28, 66, 17, 10, 4, 6]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "\n", + "list_a = random.sample(range(0, 100), 10)\n", + "function = (lambda x: x+2)\n", + "list_b = []\n", + "\n", + "def modify_list(xxx):\n", + " print(list_a)\n", + " for number in xxx:\n", + " list_b.append(function(number))\n", + " return list_b\n", + "\n", + "modify_list(list_a)" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "function2= lambda x : x+273.15" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[12, 23, 38, -55, 24]\n" + ] + }, + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.15, 297.15]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "temps_k = []\n", + "\n", + "# Your code here:\n", + "\n", + "def c_to_k(list):\n", + " print(temps)\n", + " for temperature in list:\n", + " temps_k.append(round(function2(temperature),2))\n", + " return temps_k\n", + "\n", + "c_to_k(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": [ + "# Your code here:\n", + "\n", + "mod = lambda a,b : 1 if a%b == 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": 46, + "metadata": {}, + "outputs": [], + "source": [ + "def divisor(a):\n", + " return (lambda b : 1 if b%a == 0 else 0)\n", + "\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", + " \"\"\"" + ] + }, + { + "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": 47, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "divisible5 = divisor(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 49, + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Green', 'eggs'), ('cheese', 'cheese'), ('English', 'cucumber'), ('tomato', 'tomato')]\n" + ] + } + ], + "source": [ + "list1 = ['Green', 'cheese', 'English', 'tomato']\n", + "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", + "\n", + "zipped = zip(list1,list2)\n", + "zipped = list(zipped)\n", + "print(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": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zipped list: [(1, 2), (2, 3), (4, 3), (4, 5)]\n", + "greater element from list: [2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "list3 = [1,2,4,4]\n", + "list4 = [2,3,3,5]\n", + "\n", + "## Zip the lists together \n", + "zipped2 = list(zip(list3, list4))\n", + "\n", + "## Print the zipped list\n", + "print(\"zipped list:\", zipped2)\n", + "\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "print(\"greater element from list:\", [(lambda x,y: x if x>y else y)(x,y) for x,y in zipped2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Complete the parts of the code marked as \"###\"" + ] + }, + { + "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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zipped list: {'Engineering': 'Lab', 'Computer Science': 'Homework', 'Political Science': 'Essay', 'Mathematics': 'Module'}\n", + "Sorted: {'Political Science': 'Essay', 'Computer Science': 'Homework', 'Engineering': 'Lab', 'Mathematics': 'Module'}\n" + ] + } + ], + "source": [ + "list5 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list6 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "# Your code here:\n", + "\n", + "new_list = dict(zip(list5, list6))\n", + "print(\"zipped list:\", new_list)\n", + "\n", + "sort_list= dict(sorted(new_list.items(), key=lambda item: item[1])) \n", + "print(\"Sorted: \", sort_list)" + ] + }, + { + "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": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original Dictionary: {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "Year Sorted Dictionary: {'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", + "\n", + "print(\"Original Dictionary: \", d)\n", + "sort_dict= dict(sorted(d.items(), key=lambda item: item[1])) \n", + "print(\"Year Sorted Dictionary: \", sort_dict)" + ] + }, + { + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}