From 228e59ef59625569c0052bb72549f76799bbfe42 Mon Sep 17 00:00:00 2001 From: Aliya Janmohamed Date: Mon, 24 Apr 2023 14:07:45 +0100 Subject: [PATCH] Lab Done --- your-code/main.ipynb | 150 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 120 insertions(+), 30 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..36566ed 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,33 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 6, 7, 9, 10, 10, 11, 14, 22]\n" + ] + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "list_1 = [1,2,4,5,7,8,8,9,12,20]\n", + "\n", + "lambda_1 = lambda x: x+2\n", + "\n", + "list_2=[]\n", + "\n", + "def modify_list(list_1, lambda_1, list_2):\n", + " for x in list_1:\n", + " list_1 = lambda_1(x)\n", + " list_2.append(list_1)\n", + "\n", + "modify_list(list_1, lambda_1, list_2)\n", + "\n", + "print(list_2)" ] }, { @@ -52,11 +74,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "lambda_trans = lambda x: x+273.15" ] }, { @@ -68,13 +91,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 37, "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" + "list_3=[]\n", + "# Your code here:\n", + "modify_list(temps, lambda_trans, list_3)\n", + "\n", + "print(list_3)" ] }, { @@ -88,11 +123,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda a,b: 1 if a % b == 0 else 0" ] }, { @@ -106,16 +142,18 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "def divisor(a):\n", + "def divisor(b):\n", " \"\"\"\n", " 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 a: 1 if a % b == 0 else 0\n", + " " ] }, { @@ -127,11 +165,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,18 +182,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -174,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 63, "metadata": {}, "outputs": [ { @@ -186,7 +247,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } @@ -209,17 +270,30 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[False, False, True, False]\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", + "list(zipped)\n", "## Print the zipped list \n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "compare=lambda a,b: a>b\n", + "\n", + "list3 = list(map(compare, list1, list2))\n", + "print(list3)" ] }, { @@ -242,14 +316,30 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 70, + "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", + "sorted_list = lambda a: a[1]\n", + "sorted(zip(list1, list2), key=sorted_list)" ] }, { @@ -282,7 +372,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +386,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.13" } }, "nbformat": 4,