From b1eb2d358d3841bdc67053b3897e77ea17372726 Mon Sep 17 00:00:00 2001 From: PedroFariaBlanc <82642165+PedroFariaBlanc@users.noreply.github.com> Date: Sat, 29 Apr 2023 17:55:09 +0100 Subject: [PATCH] [lab-lambda-functions] Pedro Faria Blanc] --- your-code/main.ipynb | 198 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 167 insertions(+), 31 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..3bf423c 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,67 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# your code here\n" + "# your code here\n", + "\n", + "def modify_list(num_list, func):\n", + " updated_list = []\n", + " for num in num_list:\n", + " updated_list.append(func(num))\n", + " return updated_list" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" + ] + } + ], + "source": [ + "# 1\n", + "num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "\n", + "# 2\n", + "func = lambda x: x + 2\n", + "\n", + "# 3 *\n", + "\n", + "# 4\n", + "updated_list = modify_list(num_list, func)\n", + "\n", + "# 5\n", + "print(updated_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[5, 6, 7, 6, 5, 4, 3, 5, 9]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list2 = modify_list([3,4,5,4,3,2,1,3,7], func)\n", + "list2 " ] }, { @@ -52,11 +108,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "a = lambda x: x + 273.15" ] }, { @@ -68,13 +126,27 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", + "temps_kelvin = []\n", + "\n", + "# Your code here:\n", "\n", - "# Your code here:\n" + "list(map(a, temps))" ] }, { @@ -88,11 +160,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "mod = lambda a, x: 1 if x%a==0 else 0 " ] }, { @@ -106,16 +180,18 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ - "def divisor(a):\n", + "def divisor(a,x):\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", + " \n", + " return mod(a,x)\n" ] }, { @@ -127,11 +203,14 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "def divisible5(x):\n", + " return divisor(5,x)\n" ] }, { @@ -143,18 +222,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)" ] @@ -174,7 +275,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -186,7 +287,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -209,17 +310,34 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[False, False, True, False]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,4,4]\n", "list2 = [2,3,3,5]\n", - "## Zip the lists together \n", "\n", - "## Print the zipped list \n", + "## Zip the lists together\n", + "zipped2 = zip(list1,list2)\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "## Print the zipped list\n", + "zipped2 = list(zipped2)\n", + "\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "compare = lambda x, y: x > y\n", + "compared = [compare(x, y) for x, y in zipped2 ]\n", + "compared" ] }, { @@ -242,14 +360,32 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 25, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 25, + "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", + "letter1 = lambda x: x[1][0]\n", + "result = sorted(zip(list1, list2), key=lambda x: letter1(x))\n", + "result" ] }, { @@ -263,13 +399,13 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here:" ] }, { @@ -296,7 +432,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.3" } }, "nbformat": 4,