From 4f4be5e74698b13f9899dd22e800b9f9064183c3 Mon Sep 17 00:00:00 2001 From: Giorgio Giao Date: Sun, 3 Apr 2022 22:06:50 +0100 Subject: [PATCH] lab comleted --- your-code/main.ipynb | 182 +++++++++++++++++++++++++++++++++---------- 1 file changed, 142 insertions(+), 40 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..ec2aff6 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,28 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n" + ] + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "lt = [0,1,2,3,4,5,6,7,8,9]\n", + "a = (lambda x: x*2)\n", + "lt1 = []\n", + "def modify_list(nb_list):\n", + " for number in nb_list:\n", + " lt1.append(a(number))\n", + "\n", + "modify_list(lt)\n", + "print(lt1)" ] }, { @@ -52,11 +69,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "lbd_exp = (lambda x: x + 273.15)" ] }, { @@ -68,13 +86,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "[lbd_exp(number) for number in temps]" ] }, { @@ -88,11 +118,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = (lambda x,y: 1 if x%y == 0 else 0)" ] }, { @@ -106,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +146,11 @@ " 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", + " if a%'number (to be passed later)' ==0:\n", + " return 1\n", + " else:\n", + " return 0" ] }, { @@ -127,11 +162,16 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "def divisible5(a):\n", + " if a%5 == 0:\n", + " return 1\n", + " else:\n", + " return 0" ] }, { @@ -143,18 +183,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -174,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -186,7 +248,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -209,17 +271,37 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (4, 3), (4, 5)]\n" + ] + }, + { + "data": { + "text/plain": [ + "[False, False, True, False]" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,4,4]\n", "list2 = [2,3,3,5]\n", "## Zip the lists together \n", - "\n", + "zipped = zip(list1,list2)\n", "## Print the zipped list \n", - "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "l_zipped =list(zipped)\n", + "print(l_zipped)\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "list(map(lambda x: x[0] > x[1],l_zipped))\n" ] }, { @@ -242,14 +324,31 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n", + "['Essay', 'Homework', 'Lab', 'Module']\n", + "[('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", + "zipped = zip(list1,list2)\n", + "l_zipped = list(zipped)\n", + "print(l_zipped)\n", + "\n", + "print(sorted(list2, key = lambda x: x))\n", + "\n", + "print(sorted(l_zipped, key = lambda x: x[1]))" ] }, { @@ -263,26 +362,29 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Toyota', 'Honda', 'Audi', 'BMW']\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here: \n", + "\n", + "print (sorted(d,key = lambda i: d[i]))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +398,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.7" } }, "nbformat": 4,