From 68f64b0cefdf1951f60830f63c971c7c317bf086 Mon Sep 17 00:00:00 2001 From: ArisGoulas Date: Sun, 23 Apr 2023 22:59:59 +0100 Subject: [PATCH 1/2] lab done --- your-code/main.ipynb | 180 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 149 insertions(+), 31 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..3e07da4 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,42 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 7, 8, 1, 12, 14, 166, 18, 3, 2]\n" + ] + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "\n", + "#define a list of any 10 numbers\n", + "init_list = [2, 5, 6, -1, 10, 12, 164, 16, 1, 0]\n", + "\n", + "#define a lambda expression that updates a number by adding 2 to the number\n", + "add_two = lambda x : x + 2\n", + "\n", + "#define an empty list\n", + "upd_list = []\n", + "\n", + "#define a function called modify_list, use the lambda expression to append the empty list\n", + "def modify_list(init_list, add_two):\n", + " '''\n", + " this function takes as input an initial list of numbers\n", + " and a lambda expression that adds +2 to each number of the list\n", + " and appends the result to a modified list\n", + " '''\n", + " for num in init_list:\n", + " upd_list.append(add_two(num))\n", + " \n", + "modify_list(init_list, add_two)\n", + " \n", + "print(upd_list)" ] }, { @@ -52,11 +83,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "C_to_K = lambda x : x + 273.15" ] }, { @@ -68,13 +100,27 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.15, 297.15]\n" + ] + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "K_temps = []\n", + "\n", + "for temp in temps:\n", + " K_temps.append(round(C_to_K(temp), 2))\n", + "\n", + "print(K_temps)" ] }, { @@ -88,11 +134,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 58, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda x, y : 1 if x % y == 0 or y % x == 0 else 0" ] }, { @@ -106,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +162,9 @@ " 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", + " mod = lambda x : 1 if a % x == 0 or x % a == 0 else 0\n", + " return mod" ] }, { @@ -127,11 +176,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 60, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "divisible5 = divisor(5)" ] }, { @@ -143,20 +193,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ - "divisible5(10)" + "divisible5(10)\n", + "\n", + "print(divisible5(10))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ - "divisible5(8)" + "divisible5(8)\n", + "\n", + "print(divisible5(8))" ] }, { @@ -209,17 +279,34 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 26, "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", + "\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", + "comparison = lambda x, y : x > y\n", + "\n", + "result = [comparison(x, y) for x, y in zip(list1, list2)]\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "print(result)" ] }, { @@ -242,14 +329,34 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', '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", + "\n", + "# Zip the lists together\n", + "zipped = list(zip(list1, list2))\n", + "\n", + "# Print the zipped list\n", + "print(list(zipped))\n", + "\n", + "# Sort the lists by using a lambda expression that calls upon the second element of each tuple\n", + "zipped_sorted = sorted(zipped, key=lambda x: x[1][0])\n", + "\n", + "print(zipped_sorted)" ] }, { @@ -263,13 +370,24 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 38, "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_d = dict(sorted(d.items(), key=lambda x: x[1]))\n", + "\n", + "print(sorted_d)" ] }, { @@ -282,7 +400,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +414,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.9" } }, "nbformat": 4, From 8e9a3567bd15f9399b41aa84415a6fe6d6a15f39 Mon Sep 17 00:00:00 2001 From: ArisGoulas Date: Wed, 5 Jul 2023 15:01:23 +0100 Subject: [PATCH 2/2] lab revised after bootcamp --- your-code/main.ipynb | 207 ++++++++++++++++++++++++++----------------- 1 file changed, 126 insertions(+), 81 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 3e07da4..fda08a7 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -46,30 +46,24 @@ } ], "source": [ - "# your code here\n", - "\n", - "#define a list of any 10 numbers\n", - "init_list = [2, 5, 6, -1, 10, 12, 164, 16, 1, 0]\n", + "initial_list = [2, 5, 6, -1, 10, 12, 164, 16, 1, 0]\n", "\n", - "#define a lambda expression that updates a number by adding 2 to the number\n", "add_two = lambda x : x + 2\n", "\n", - "#define an empty list\n", - "upd_list = []\n", + "updated_list = []\n", "\n", - "#define a function called modify_list, use the lambda expression to append the empty list\n", - "def modify_list(init_list, add_two):\n", + "def modify_list(initial_list, add_two):\n", " '''\n", - " this function takes as input an initial list of numbers\n", - " and a lambda expression that adds +2 to each number of the list\n", - " and appends the result to a modified list\n", + " this function takes as input an initial list of 10 numbers\n", + " and a lambda expression that adds 2 to each number of the list\n", + " it returns an updated list where the initial numbers are incremented by 2\n", " '''\n", - " for num in init_list:\n", - " upd_list.append(add_two(num))\n", + " for number in initial_list:\n", + " updated_list.append(add_two(number))\n", " \n", - "modify_list(init_list, add_two)\n", + "modify_list(initial_list, add_two)\n", " \n", - "print(upd_list)" + "print(updated_list)" ] }, { @@ -83,12 +77,11 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", - "C_to_K = lambda x : x + 273.15" + "C_to_K = lambda x : round((x + 273.15), 2)" ] }, { @@ -100,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -114,15 +107,44 @@ "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n", "K_temps = []\n", "\n", - "for temp in temps:\n", - " K_temps.append(round(C_to_K(temp), 2))\n", + "def modify_temps(temps, C_to_K):\n", + " '''\n", + " this function takes as input an initial list of temperatures in Celsius\n", + " and a lambda expression that converts a temperature from Celsius to Kelvin\n", + " it returns a list where the initial temperatures have been converted\n", + " '''\n", + " for temperature in temps:\n", + " K_temps.append(C_to_K(temperature))\n", + " \n", + "modify_temps(temps, C_to_K)\n", "\n", "print(K_temps)" ] }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.15, 297.15]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# with list comprehension\n", + "K_temps = [C_to_K(temperature) for temperature in temps]\n", + "K_temps" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -134,12 +156,11 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", - "mod = lambda x, y : 1 if x % y == 0 or y % x == 0 else 0" + "mod = lambda x, y : 1 if (x % y == 0 or y % x == 0) else 0" ] }, { @@ -153,7 +174,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -162,8 +183,7 @@ " 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", - " mod = lambda x : 1 if a % x == 0 or x % a == 0 else 0\n", + " mod = lambda x : 1 if (a % x == 0 or x % a == 0) else 0\n", " return mod" ] }, @@ -176,11 +196,10 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n", "divisible5 = divisor(5)" ] }, @@ -193,40 +212,42 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": 19, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n" - ] + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "divisible5(10)\n", - "\n", - "print(divisible5(10))" + "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 20, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n" - ] + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "divisible5(8)\n", - "\n", - "print(divisible5(8))" + "divisible5(8)" ] }, { @@ -244,7 +265,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -256,7 +277,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -264,7 +285,9 @@ "source": [ "list1 = ['Green', 'cheese', 'English', 'tomato']\n", "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", - "zipped = zip(list1,list2)\n", + "\n", + "zipped = zip(list1, list2)\n", + "\n", "list(zipped)" ] }, @@ -279,34 +302,42 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[(1, 2), (2, 3), (4, 3), (4, 5)]\n", - "[False, False, True, False]\n" + "[(1, 2), (2, 3), (4, 3), (4, 5)]\n" ] + }, + { + "data": { + "text/plain": [ + "[0, 0, 1, 0]" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "list1 = [1,2,4,4]\n", - "list2 = [2,3,3,5]\n", + "list1 = [1, 2, 4, 4]\n", + "list2 = [2, 3, 3, 5]\n", "\n", "## Zip the lists together \n", "zipped = zip(list1, list2)\n", "\n", "## Print the zipped list \n", - "print(list(zipped))\n", + "print(list(zipped)) # zipped is consumed here, so need to re-make it down\n", "\n", "## Use a lambda expression to compare if: list1 element > list2 element\n", - "comparison = lambda x, y : x > y\n", + "compare = lambda x, y : 1 if x > y else 0\n", "\n", - "result = [comparison(x, y) for x, y in zip(list1, list2)]\n", - "\n", - "print(result)" + "result = [compare(x, y) for x, y in zip(list1, list2)]\n", + "result" ] }, { @@ -329,34 +360,45 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n", - "[('Political Science', 'Essay'), ('Computer Science', 'Homework'), ('Engineering', 'Lab'), ('Mathematics', 'Module')]\n" + "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n" ] + }, + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# Your code here:\n", - "\n", "# Zip the lists together\n", - "zipped = list(zip(list1, list2))\n", + "zipped = zip(list1, list2)\n", "\n", "# Print the zipped list\n", "print(list(zipped))\n", "\n", "# Sort the lists by using a lambda expression that calls upon the second element of each tuple\n", - "zipped_sorted = sorted(zipped, key=lambda x: x[1][0])\n", + "sort = lambda x : x[1][0]\n", "\n", - "print(zipped_sorted)" + "result = sorted(zip(list1, list2), key=sort)\n", + "result" ] }, { @@ -370,24 +412,27 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 42, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" - ] + "data": { + "text/plain": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n", - "sorted_d = dict(sorted(d.items(), key=lambda x: x[1]))\n", + "sort = lambda x : x[1]\n", "\n", - "print(sorted_d)" + "result = sorted(d.items(), key=sort)\n", + "result" ] }, { @@ -414,7 +459,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.10.10" } }, "nbformat": 4,