From 20534abf29a4fbe9873f387457b1e79bc2514d74 Mon Sep 17 00:00:00 2001 From: Joao Carrasco Soares Date: Mon, 13 Mar 2023 00:32:54 +0000 Subject: [PATCH] done --- your-code/main.ipynb | 198 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 165 insertions(+), 33 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..50525e8 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,36 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" + ] + } + ], "source": [ - "# your code here\n" + "num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "\n", + "# Step 2\n", + "update_num = lambda x: x + 2\n", + "\n", + "# Step 3\n", + "new_list = []\n", + "\n", + "# Step 4\n", + "def modify_list(lst, func):\n", + " for num in lst:\n", + " new_list.append(func(num))\n", + "\n", + "# Step 5\n", + "modify_list(num_list, update_num)\n", + "\n", + "# Step 6\n", + "print(new_list)" ] }, { @@ -52,11 +77,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "celsius_to_kelvin = lambda celsius: celsius + 273.15" ] }, { @@ -68,13 +94,23 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n" + ] + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "kelvin_temps = list(map(celsius_to_kelvin, temps))\n", + "\n", + "print(kelvin_temps)" ] }, { @@ -88,11 +124,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda a, b: 1 if a % b == 0 or b % a == 0 else 0" ] }, { @@ -106,16 +143,33 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "1\n", + "0\n" + ] + } + ], "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" + " # Your code here:\n", + " return lambda b: 1 if a % b == 0 or b % a == 0 else 0\n", + "\n", + "a = 6\n", + "mod_function = divisor(a)\n", + "print(mod_function(2))\n", + "print(mod_function(3))\n", + "print(mod_function(4))" ] }, { @@ -127,11 +181,25 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "0\n", + "1\n" + ] + } + ], "source": [ - "# Your code here:\n" + "divisible5 = divisor(5)\n", + "\n", + "print(divisible5(10))\n", + "print(divisible5(7))\n", + "print(divisible5(25))" ] }, { @@ -143,18 +211,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -174,7 +264,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -186,7 +276,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -209,17 +299,32 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (4, 3), (4, 5)]\n", + "[False, False, True, False]\n" + ] + } + ], "source": [ "list1 = [1,2,4,4]\n", "list2 = [2,3,3,5]\n", "## Zip the lists together \n", + "zipped = zip(list1, list2)\n", "\n", "## Print the zipped list \n", + "print(list(zipped))\n", + "\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "greater_than = lambda x: x[0] > x[1]\n", + "results = [greater_than(x) for x in zip(list1, list2)]\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "print(results)" ] }, { @@ -242,14 +347,25 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Political Science', 'Essay'), ('Computer Science', 'Homework'), ('Engineering', 'Lab'), ('Mathematics', 'Module')]\n" + ] + } + ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "sorted_list = sorted(zip(list1, list2), key=lambda x: x[1][0])\n", + "\n", + "print(sorted_list)" ] }, { @@ -263,13 +379,24 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "sorted_dict = dict(sorted(d.items(), key=lambda x: x[1]))\n", + "\n", + "print(sorted_dict)" ] }, { @@ -282,7 +409,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3.10.7 64-bit", "language": "python", "name": "python3" }, @@ -296,7 +423,12 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.7" + }, + "vscode": { + "interpreter": { + "hash": "721db305ef1fd1fc91cdf20e400af694a949fe540ac5f48c160f31c7e384879d" + } } }, "nbformat": 4,