From 37324486f8d4a94f01e32592439071c3caa2ba0a Mon Sep 17 00:00:00 2001 From: JoseVelazquezR Date: Mon, 28 Mar 2022 00:37:10 -0600 Subject: [PATCH 1/2] laboratorio terminado --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 450 ++++++++++++++++++ your-code/main.ipynb | 212 +++++++-- 2 files changed, 613 insertions(+), 49 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..2502e3e --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,450 @@ +{ + "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": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def modify_list(lst, fudduLambda):\n", + " l = [1,2,3,4,5,6,7,8,9,10]\n", + " f = lambda x: x+2\n", + " b=[]\n", + " for x in l:\n", + " f(x)\n", + " b.append(f(x))\n", + "#Call modify_list(##,##)\n", + " return b\n", + "#print b\n", + "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": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "373.15" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "Celsius_to_Kelvin = lambda x: x + 273.15\n", + "Celsius_to_Kelvin(100)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Finally, convert the list of temperatures below from Celsius to Kelvin." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "temps = [12, 23, 38, -55, 24]\n", + "\n", + "# Your code here:\n", + "Celsius_to_Kelvin = lambda x: x + 273.15\n", + "[Celsius_to_Kelvin(x) for x in 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": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "mod = lambda x, y: 1 if x%y == 0 else 0\n", + "mod(10,3)\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": 44, + "metadata": {}, + "outputs": [], + "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:\n", + " if i%b == 0:\n", + " return lambda b: 1\n", + " else:\n", + " return" + ] + }, + { + "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": 50, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'i' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [50]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mdivisor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m divisible5(i)\n", + "Input \u001b[1;32mIn [44]\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \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 divisible by another number (to be passed later) and zero otherwise\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mi\u001b[49m\u001b[38;5;241m%\u001b[39mb \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", + "\u001b[1;31mNameError\u001b[0m: name 'i' is not defined" + ] + } + ], + "source": [ + "# Your code here:\n", + "divisor(5) == divisible5(i)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Test your function with the following test cases:" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'divisible5' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [51]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdivisible5\u001b[49m(\u001b[38;5;241m10\u001b[39m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'divisible5' is not defined" + ] + } + ], + "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": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 52, + "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": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = [1,2,3,4]\n", + "list2 = [2,3,4,5]\n", + "## Zip the lists together \n", + "## Print the zipped list \n", + "zipped = zip(list1, list2)\n", + "list(zipped)" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Complete the parts of the code marked as \"###\"" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "False\n", + "False\n" + ] + } + ], + "source": [ + "compare = lambda x, y: print(\"True\") if x == y else print(\"False\")\n", + "for x, y in zip(list1,list2):\n", + " compare(x, y)\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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m zipped \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mzip\u001b[39m(list1, list2)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28msorted\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x, y: y, x\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mzipped\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object is not callable" + ] + } + ], + "source": [ + "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", + "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", + "\n", + "# Your code here:\n", + "zipped = zip(list1, list2)\n", + "sorted = lambda x, y: y, x\n", + "sorted(zipped)\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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "\n", + "# Your code here:" + ] + }, + { + "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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 66a9984..2502e3e 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,18 +34,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + " l = [1,2,3,4,5,6,7,8,9,10]\n", + " f = lambda x: x+2\n", + " b=[]\n", + " for x in l:\n", + " f(x)\n", + " b.append(f(x))\n", "#Call modify_list(##,##)\n", - "#print b" + " return b\n", + "#print b\n", + "modify_list(l, f)" ] }, { @@ -59,12 +73,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "373.15" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Your code here:\n", - "\n" + "Celsius_to_Kelvin = lambda x: x + 273.15\n", + "Celsius_to_Kelvin(100)\n" ] }, { @@ -76,13 +102,26 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:" + "# Your code here:\n", + "Celsius_to_Kelvin = lambda x: x + 273.15\n", + "[Celsius_to_Kelvin(x) for x in temps]" ] }, { @@ -96,11 +135,24 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 38, + "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(10,3)\n" ] }, { @@ -114,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -124,7 +176,11 @@ " 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:" + " # Your code here:\n", + " if i%b == 0:\n", + " return lambda b: 1\n", + " else:\n", + " return" ] }, { @@ -136,11 +192,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'i' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [50]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mdivisor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m divisible5(i)\n", + "Input \u001b[1;32mIn [44]\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \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 divisible by another number (to be passed later) and zero otherwise\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mi\u001b[49m\u001b[38;5;241m%\u001b[39mb \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", + "\u001b[1;31mNameError\u001b[0m: name 'i' is not defined" + ] + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "divisor(5) == divisible5(i)" ] }, { @@ -152,9 +222,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'divisible5' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [51]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdivisible5\u001b[49m(\u001b[38;5;241m10\u001b[39m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'divisible5' is not defined" + ] + } + ], "source": [ "divisible5(10)" ] @@ -183,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -195,7 +277,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -218,14 +300,27 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,3,4]\n", "list2 = [2,3,4,5]\n", "## Zip the lists together \n", - "## Print the zipped list " + "## Print the zipped list \n", + "zipped = zip(list1, list2)\n", + "list(zipped)" ] }, { @@ -237,28 +332,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 55, "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", - "for ### in zip(list1,list2):\n", - " compare(###)\n", - " \n", - "''' " + "compare = lambda x, y: print(\"True\") if x == y else print(\"False\")\n", + "for x, y in zip(list1,list2):\n", + " compare(x, y)\n", + " " ] }, { @@ -274,14 +366,36 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m zipped \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mzip\u001b[39m(list1, list2)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28msorted\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x, y: y, x\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mzipped\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object is not callable" + ] + } + ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "zipped = zip(list1, list2)\n", + "sorted = lambda x, y: y, x\n", + "sorted(zipped)\n" ] }, { @@ -314,7 +428,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -328,7 +442,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4, From 0286bd58ee0f2e34bc675615f8bdc02f48a86e1b Mon Sep 17 00:00:00 2001 From: JoseVelazquezR Date: Tue, 29 Mar 2022 22:09:23 -0600 Subject: [PATCH 2/2] laboratorio corregido --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 148 ++++++++++++------ your-code/main.ipynb | 148 ++++++++++++------ 2 files changed, 202 insertions(+), 94 deletions(-) diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb index 2502e3e..efa2e72 100644 --- a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -166,9 +166,20 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + ".(x)>" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisor(b):\n", " \"\"\"\n", @@ -177,10 +188,9 @@ " \"\"\"\n", " \n", " # Your code here:\n", - " if i%b == 0:\n", - " return lambda b: 1\n", - " else:\n", - " return" + " return lambda x: 1 if x%b == 0 else 0\n", + "\n", + "divisor(5)" ] }, { @@ -192,25 +202,24 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 19, "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'i' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [50]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mdivisor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m divisible5(i)\n", - "Input \u001b[1;32mIn [44]\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \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 divisible by another number (to be passed later) and zero otherwise\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mi\u001b[49m\u001b[38;5;241m%\u001b[39mb \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", - "\u001b[1;31mNameError\u001b[0m: name 'i' is not defined" - ] + "data": { + "text/plain": [ + ".(x)>" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# Your code here:\n", - "divisor(5) == divisible5(i)" + "divisible5 = divisor(5)\n", + "divisible5" ] }, { @@ -222,19 +231,38 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 20, "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'divisible5' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [51]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdivisible5\u001b[49m(\u001b[38;5;241m10\u001b[39m)\n", - "\u001b[1;31mNameError\u001b[0m: name 'divisible5' is not defined" - ] + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -243,9 +271,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -373,19 +412,21 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 31, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "'tuple' object is not callable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m zipped \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mzip\u001b[39m(list1, list2)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28msorted\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x, y: y, x\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mzipped\u001b[49m\u001b[43m)\u001b[49m\n", - "\u001b[1;31mTypeError\u001b[0m: 'tuple' object is not callable" - ] + "data": { + "text/plain": [ + "[('Essay', 'Political Science'),\n", + " ('Homework', 'Computer Science'),\n", + " ('Lab', 'Engineering'),\n", + " ('Module', 'Mathematics')]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -393,9 +434,8 @@ "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", "# Your code here:\n", - "zipped = zip(list1, list2)\n", - "sorted = lambda x, y: y, x\n", - "sorted(zipped)\n" + "zipped = sorted(zip(list2, list1))\n", + "list(zipped)" ] }, { @@ -409,7 +449,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -420,10 +460,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "{1997: 'Honda', 1995: 'Toyota', 2001: 'Audi', 2005: 'BMW'}" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {value:key for key, value in d.items()}\n", + "d" + ] } ], "metadata": { diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 2502e3e..efa2e72 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -166,9 +166,20 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + ".(x)>" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def divisor(b):\n", " \"\"\"\n", @@ -177,10 +188,9 @@ " \"\"\"\n", " \n", " # Your code here:\n", - " if i%b == 0:\n", - " return lambda b: 1\n", - " else:\n", - " return" + " return lambda x: 1 if x%b == 0 else 0\n", + "\n", + "divisor(5)" ] }, { @@ -192,25 +202,24 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 19, "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'i' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [50]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mdivisor\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m divisible5(i)\n", - "Input \u001b[1;32mIn [44]\u001b[0m, in \u001b[0;36mdivisor\u001b[1;34m(b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \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 divisible by another number (to be passed later) and zero otherwise\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;66;03m# Your code here:\u001b[39;00m\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[43mi\u001b[49m\u001b[38;5;241m%\u001b[39mb \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", - "\u001b[1;31mNameError\u001b[0m: name 'i' is not defined" - ] + "data": { + "text/plain": [ + ".(x)>" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# Your code here:\n", - "divisor(5) == divisible5(i)" + "divisible5 = divisor(5)\n", + "divisible5" ] }, { @@ -222,19 +231,38 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 20, "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'divisible5' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [51]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdivisible5\u001b[49m(\u001b[38;5;241m10\u001b[39m)\n", - "\u001b[1;31mNameError\u001b[0m: name 'divisible5' is not defined" - ] + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -243,9 +271,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -373,19 +412,21 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 31, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "'tuple' object is not callable", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Input \u001b[1;32mIn [68]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m zipped \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mzip\u001b[39m(list1, list2)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28msorted\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x, y: y, x\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mzipped\u001b[49m\u001b[43m)\u001b[49m\n", - "\u001b[1;31mTypeError\u001b[0m: 'tuple' object is not callable" - ] + "data": { + "text/plain": [ + "[('Essay', 'Political Science'),\n", + " ('Homework', 'Computer Science'),\n", + " ('Lab', 'Engineering'),\n", + " ('Module', 'Mathematics')]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -393,9 +434,8 @@ "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", "# Your code here:\n", - "zipped = zip(list1, list2)\n", - "sorted = lambda x, y: y, x\n", - "sorted(zipped)\n" + "zipped = sorted(zip(list2, list1))\n", + "list(zipped)" ] }, { @@ -409,7 +449,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -420,10 +460,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "{1997: 'Honda', 1995: 'Toyota', 2001: 'Audi', 2005: 'BMW'}" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d = {value:key for key, value in d.items()}\n", + "d" + ] } ], "metadata": {