From 29ad493795cdc04e261b0a4357397db9844bfbfe Mon Sep 17 00:00:00 2001 From: JCMM1987 Date: Sat, 4 Mar 2023 14:10:09 +0000 Subject: [PATCH] [lab-lambda-functions] --- your-code/main.ipynb | 155 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 24 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..f1f2fda 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -10,6 +10,15 @@ "- Happy learning!" ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -34,11 +43,33 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[5625, 0, 2116, 784, 9025, 1, 729, 5776, 7225, 1521]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "list1=np.random.randint(100, size=10)\n", + "list2=[]\n", + "\n", + "square=(lambda x : x**2)\n", + "\n", + "def modify_list(any_list,function):\n", + " for i in any_list:\n", + " list2.append(function(i))\n", + " return list2\n", + "\n", + "modify_list(list1, square )" ] }, { @@ -52,11 +83,32 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[275.15, 283.15, 313.15, 288.15, 291.15, 279.15]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "temp_celsius=[2, 10, 40, 15, 18, 6]\n", + "\n", + "def celsius_to_kelvin(list_celsius):\n", + " temp_kelvin=[]\n", + " conversion_kelvin=lambda x : x+ 273.15\n", + " for i in list_celsius:\n", + " temp_kelvin.append(conversion_kelvin(i))\n", + " return temp_kelvin\n", + "\n", + "celsius_to_kelvin(temp_celsius)" ] }, { @@ -68,13 +120,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "celsius_to_kelvin(temps)" ] }, { @@ -92,7 +156,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod= lambda x,y : 1 if (y != 0) else 0" ] }, { @@ -106,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +180,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", + " x=mod(a,0)\n", + " return x\n" ] }, { @@ -127,11 +194,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "expression=lambda a : 1 if (a%5==0) else 0\n", + "def divisible5(a):\n", + " return expression(a)" ] }, { @@ -143,18 +213,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -209,17 +301,32 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List2 higher\n", + "List2 higher\n", + "List1 higher\n", + "List2 higher\n" + ] + } + ], "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", + "list(zipped)\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "list_check=(lambda n : \"List1 higher\" if (list1[n]>list2[n]) else \"List2 higher\")\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "for i in range(len(list1)):\n", + " print(list_check(i))" ] }, { @@ -282,7 +389,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +403,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.12" } }, "nbformat": 4,