From 1e6178aa85f1c8cb8746f00bc4861fbf6604c672 Mon Sep 17 00:00:00 2001 From: Alexandre Garanhao Date: Mon, 1 May 2023 14:14:52 +0100 Subject: [PATCH] lab done --- your-code/main.ipynb | 216 ++++++++++++++++++++++++++++++++----------- 1 file changed, 163 insertions(+), 53 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..cf66ca6 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,31 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" + ] + } + ], "source": [ - "# your code here\n" + "list = [1,2,3,4,5,6,7,8,9,10]\n", + "simple_lambda = lambda x : x + 2\n", + "list4 =[]\n", + "\n", + "def new_list (list, fun):\n", + " for x in list:\n", + " list4.append(fun(x))\n", + "\n", + "new_list(list,simple_lambda)\n", + "\n", + "print(list4)\n", + " \n", + " " ] }, { @@ -52,11 +72,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "\n", + "celsius_to_kelvin = lambda x :x + 273,15\n" ] }, { @@ -68,13 +89,38 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n" + ] + } + ], "source": [ + "celsius_to_kelvin = lambda x: x + 273.15\n", "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "new_list = [celsius_to_kelvin(x) for x in temps]\n", + "print(new_list)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [], + "source": [ + "#(lambda x: x+273,15)\n", + "\n", + "#[x for x in temps if (lambda x : x *273,15)]\n", + "\n", + "#temps(lambda x : x+273,15 [12,23,38,-55,24])\n" ] }, { @@ -88,11 +134,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "mod = lambda x,y: 1 if x%y == 0 or y%x ==0 else 0\n" ] }, { @@ -106,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 68, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +161,10 @@ " 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" + "return_mod = lambda x : 1 if x % a == 0 else 0 \n", + "\n", + "\n", + "\n" ] }, { @@ -127,11 +176,30 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 80, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ". at 0x000001F394B6FB00>\n", + "1\n" + ] + } + ], "source": [ - "# Your code here:\n" + "def divisor(a):\n", + " return lambda x: 1 if x % a == 0 else 0\n", + "\n", + "\n", + "print(divisor(5))\n", + "\n", + "\n", + "divisible5 = divisor(5)\n", + "\n", + "\n", + "print(divisible5(5)) \n" ] }, { @@ -143,20 +211,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisible5(10) # sim é divisivel " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(8)" + "divisible5(8) #nao nem é" ] }, { @@ -174,28 +264,24 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 89, "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)" + "\n", + "new_list = [(lambda x, y : x + \" \" + y)(x, y) for x, y in zip(list1, list2)]\n", + "\n", + "print (new_list)" ] }, { @@ -209,17 +295,28 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 92, "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", - "## Print the zipped list \n", + "together = [x > y for x, y in zip(list1, list2)]\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "## Print the zipped list \n", + "print (together)\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "#???" ] }, { @@ -242,14 +339,25 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 57, "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" + "zipped = zip(list1, list2)\n", + "\n", + "sort = sorted(zipped, key=lambda x: x[1][0])\n", + "print(sort)" ] }, { @@ -263,21 +371,23 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 60, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "new_sort = dict(sorted(d.items(), key=lambda x: x[1])) # o lambda x = 1 é para acessar o segundo valor da chave.\n", + "print(new_sort)\n" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -296,7 +406,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.2" } }, "nbformat": 4,