From 97043cc94a121b9491611329a8601f58fdc5f157 Mon Sep 17 00:00:00 2001 From: chukseey Date: Wed, 4 May 2022 18:00:36 +0100 Subject: [PATCH] lab done --- your-code/main copy.ipynb | 475 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 475 insertions(+) create mode 100644 your-code/main copy.ipynb diff --git a/your-code/main copy.ipynb b/your-code/main copy.ipynb new file mode 100644 index 0000000..5f2c4c8 --- /dev/null +++ b/your-code/main copy.ipynb @@ -0,0 +1,475 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "Jr-PrebxIYn-" + }, + "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": { + "id": "0q_64AFXIYoA" + }, + "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": { + "id": "DzJreRQlIYoC" + }, + "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": 1, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6H7iPNa8IYoD", + "outputId": "da896482-fc7d-474b-af8b-c4cc19d501e3" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + ] + }, + "metadata": {}, + "execution_count": 1 + } + ], + "source": [ + "# your code here\n", + "# Step-1\n", + "lis = list(range(1, 11))\n", + "\n", + "# Step-2\n", + "def update(n):\n", + " return lambda x: x+n\n", + "\n", + "# Step-3\n", + "emp = []\n", + "\n", + "# Step-4\n", + "def new_func(emp):\n", + " return lambda x: emp.append(x)\n", + "\n", + "# Step-5\n", + "f = new_func(emp)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dM-1fFtnIYoF" + }, + "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": 2, + "metadata": { + "id": "Ym0UbNMSIYoG" + }, + "outputs": [], + "source": [ + "# Your code here:\n", + "def temp():\n", + " return lambda x: x+273.15\n", + "\n", + "f = temp()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "b0dWEXJIIYoI" + }, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "y2OSPh6RIYoI", + "outputId": "4de12084-0af9-4ef9-a405-44348458aa1c" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "\n", + "# Your code here:\n", + "[f(i) for i in temps]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EfMW6AiNIYoJ" + }, + "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": 7, + "metadata": { + "id": "32i_qaArIYoK" + }, + "outputs": [], + "source": [ + "# Your code here:\n", + "def mod(a,b):\n", + " return lambda a, b: 1 if (a%b == 0) else 0" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XOJbYbUTIYoL" + }, + "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": null, + "metadata": { + "id": "awoA8voKIYoM" + }, + "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", + " f1 = mod(a)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FerYeyMrIYoO" + }, + "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": null, + "metadata": { + "id": "lo5yZszmIYoO" + }, + "outputs": [], + "source": [ + "# Your code here:\n", + "divisible5 = divisor(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cyF-hGu4IYoO" + }, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "oIaH5dQqIYoO" + }, + "outputs": [], + "source": [ + "divisible5(10)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "rbOxyf4qIYoP" + }, + "outputs": [], + "source": [ + "divisible5(8)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N6iYs3BKIYoQ" + }, + "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": null, + "metadata": { + "id": "OwJbje24IYoQ", + "outputId": "ffe65fa2-457a-4a2f-9bfe-0f2bb31a7c96" + }, + "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": { + "id": "BGxSCeABIYoS" + }, + "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": 11, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "fgfNTSwBIYoS", + "outputId": "91e0061a-66bf-4b35-d8b2-d5d6a1076e55" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[(1, 2), (2, 3), (4, 3), (4, 5)]\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[.>,\n", + " .>,\n", + " .>,\n", + " .>]" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], + "source": [ + "list1 = [1,2,4,4]\n", + "list2 = [2,3,3,5]\n", + "## Zip the lists together \n", + "zipped_list = list(zip(list1, list2))\n", + "\n", + "## Print the zipped list \n", + "print(zipped_list)\n", + "\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "[lambda i: True if i[0]>i[1] else False for i in zipped_list]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YrTbbUyDIYoT" + }, + "source": [ + "Complete the parts of the code marked as \"###\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ShHaA1y1IYoT" + }, + "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": 16, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IJF_9GnYIYoU", + "outputId": "12ac2caf-4fd8-4c8d-d027-89b02817bc8c" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ], + "source": [ + "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "# Your code here:\n", + "sorted(zip(list1, list2), key = lambda x: x[1][0])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iuIdvvUjIYoV" + }, + "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": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "PITJDaNTIYoV", + "outputId": "3f469a27-5d7e-437a-d4dd-604a8bea2e86" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "{'Audi': 2001, 'BMW': 2005, 'Honda': 1997, 'Toyota': 1995}" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "\n", + "# Your code here:\n", + "dict(sorted(d.items(), key=lambda item: item[1]))" + ] + } + ], + "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" + }, + "colab": { + "name": "main.ipynb", + "provenance": [], + "collapsed_sections": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file