From fffc590ffea7bfccda8c33afd3b4f1b10642caad Mon Sep 17 00:00:00 2001 From: Benjamin Kenyery Date: Fri, 27 Aug 2021 23:02:00 -0400 Subject: [PATCH] lab_lambda_functions[Benjamin Kenyery] --- your-code/Learning.ipynb | 4 +- your-code/main.ipynb | 224 ++++++++++++++++++++++++++++----------- 2 files changed, 167 insertions(+), 61 deletions(-) diff --git a/your-code/Learning.ipynb b/your-code/Learning.ipynb index 93dd25f..26df047 100755 --- a/your-code/Learning.ipynb +++ b/your-code/Learning.ipynb @@ -269,7 +269,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -283,7 +283,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 66a9984..16e047d 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,18 +34,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 7, 9, 10, 8, 6, 9, 4, 5, 16]\n" + ] + } + ], "source": [ - "l = [######]\n", - "f = lambda x: #define the lambda expression\n", + "l = [1,5,7,8,6,4,7,2,3,14]\n", + "f = lambda x: x + 2 #define the lambda expression\n", "b = []\n", "def modify_list(lst, fudduLambda):\n", - " for x in ####:\n", - " b.append(#####(x))\n", + " for x in lst:\n", + " b.append(fudduLambda(x))\n", "#Call modify_list(##,##)\n", - "#print b" + "modify_list(l,f)\n", + "#print b\n", + "print(b)" ] }, { @@ -59,11 +69,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Your code here:\n", + "C_K = lambda c : c + 273.15\n", "\n" ] }, @@ -76,13 +87,27 @@ }, { "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:" + "# Your code here:\n", + "\n", + "temps_kelvin = [C_K(i) for i in temps]\n", + "temps_kelvin" ] }, { @@ -96,11 +121,27 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "0\n" + ] + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda a, b : '1' if a % b == 0 else '0'\n", + "def function_divisible(a, b):\n", + " return mod(a,b)\n", + "print(function_divisible(2, 4))\n", + "print(function_divisible(6, 2))\n", + "print(function_divisible(5, 10))\n" ] }, { @@ -114,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -124,7 +165,8 @@ " 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", + " return lambda a : mod(a,b) " ] }, { @@ -136,11 +178,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "divisible5 = divisor(5)" ] }, { @@ -152,18 +195,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)" ] @@ -183,28 +248,22 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "[('Green', 'eggs'),\n", - " ('cheese', 'cheese'),\n", - " ('English', 'cucumber'),\n", - " ('tomato', 'tomato')]" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Green', 'eggs'), ('cheese', 'cheese'), ('English', 'cucumber'), ('tomato', 'tomato')]\n" + ] } ], "source": [ "list1 = ['Green', 'cheese', 'English', 'tomato']\n", "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", "zipped = zip(list1,list2)\n", - "list(zipped)" + "print(list(zipped))" ] }, { @@ -218,14 +277,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]\n" + ] + } + ], "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", + "print(list(zipped))" ] }, { @@ -237,28 +306,28 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 12, "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", + "compare = lambda a, b: print(\"True\") if a > b else print(\"False\")\n", + "for x, y in zip(list1,list2):\n", + " compare(x, y)\n", " \n", - "''' " + "\n", + " \n", + " " ] }, { @@ -274,14 +343,31 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 19, + "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" + "# Your code here:\n", + "zipped = zip(list1, list2)\n", + "sorted_lam = lambda x : x[1]\n", + "sorted(zipped, key= lambda x : x[1][0] )" ] }, { @@ -295,7 +381,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -304,6 +390,26 @@ "# Your code here:" ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(d.items(), key= lambda x : x[1])" + ] + }, { "cell_type": "code", "execution_count": null, @@ -314,7 +420,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -328,7 +434,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.6" } }, "nbformat": 4,