From 38e1c89907e2c9506b8b98821b024ed8411e2f47 Mon Sep 17 00:00:00 2001 From: Francisco Barreto Date: Sat, 2 Apr 2022 22:16:03 +0100 Subject: [PATCH] lab-lambda-functions --- your-code/main.ipynb | 171 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 30 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..28f0d0d 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,31 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n" + ] + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "\n", + "lst = [i for i in range(10)]\n", + "expr = lambda x: x + 2\n", + "\n", + "lst2 = []\n", + "\n", + "def modify_list(x, expr):\n", + " return [expr(i) for i in x]\n", + "\n", + "lst2 = modify_list(lst, expr)\n", + "\n", + "print(lst2)\n" ] }, { @@ -52,11 +72,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "c_to_k = lambda x: x + 273.15" ] }, { @@ -68,13 +89,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" + "# Your code here:\n", + "temps_k = [c_to_k(x) for x in temps]\n", + "print(temps_k)" ] }, { @@ -88,11 +119,25 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda x, y: 1 if y % x == 0 else 0\n", + "\n", + "mod(2, 6)" ] }, { @@ -106,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +160,8 @@ " 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 y: mod(a, y)" ] }, { @@ -127,11 +173,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "divisible5 = divisor(5)" ] }, { @@ -143,18 +190,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -209,17 +278,30 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 16, "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", + "list3 = list(zip(list1, list2))\n", "\n", "## Print the zipped list \n", - "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "print(list3)\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "compare = lambda x, y: x > y\n", + "comparison = [compare(x[0], x[1]) for x in list3]\n", + "print(comparison)" ] }, { @@ -242,14 +324,25 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 17, "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", + "list3 = list(zip(list1, list2))\n", + "sorting = sorted(list3, key = lambda x: x[1][0])\n", + "print(sorting)" ] }, { @@ -263,13 +356,31 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "var = {k : v for k, v in sorted(d.items(), key = lambda x: x[1])}" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], + "source": [ + "print(var)" ] }, { @@ -282,7 +393,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +407,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.7" } }, "nbformat": 4,