From 553ae4d21c9c076156500b64653d98b297d43652 Mon Sep 17 00:00:00 2001 From: Andre Santa Clara Date: Sun, 25 Oct 2020 19:36:56 +0100 Subject: [PATCH] lambdafunc --- your-code/main.ipynb | 234 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 204 insertions(+), 30 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 50c3c11..3020fd9 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -32,7 +32,40 @@ " \"\"\"\n", " \n", " # your code here\n", - " " + " \n", + " i = 0\n", + " \n", + " while i < len(lst):\n", + " lst[i] = lmbda(lst[i])\n", + " i += 1 " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "x = [1,2,3]\n", + "\n", + "modify_list(x, lambda x: x*3)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 6, 9]\n" + ] + } + ], + "source": [ + "print(x)" ] }, { @@ -46,11 +79,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "converter_c_k = lambda x: x + 273.15" ] }, { @@ -62,13 +97,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 21, "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" + "# your code here\n", + "\n", + "modify_list(temps,converter_c_k)\n", + "\n", + "print(temps)" ] }, { @@ -82,11 +129,33 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "mod = lambda x,y: 1 if x % y == 0 or y % x == 0 else 0" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(4,4)" ] }, { @@ -100,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -111,7 +180,13 @@ " divisible by another number (to be passed later) and zero otherwise.\n", " \"\"\"\n", " \n", - " # your code here" + " # your code here\n", + " \n", + " for i in range(2,b):\n", + " if mod(i,b) == 1: \n", + " return 1\n", + " \n", + " return 0" ] }, { @@ -123,11 +198,20 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ - "# your code here" + "divisible5 = divisor(5)\n", + "print(divisible5)" ] }, { @@ -139,20 +223,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisor(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(8)" + "divisor(8)" ] }, { @@ -199,14 +305,30 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = ['Green', 'cheese', 'English', 'tomato']\n", "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", "\n", - "# your code here" + "# your code here\n", + "\n", + "[x for x in zip(list1,list2)]" ] }, { @@ -222,14 +344,53 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Engineering', 'Lab'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Political Science', 'Essay'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# your code here" + "# your code here\n", + "\n", + "[x for x in zip(list1,list2)]" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted([x for x in zip(list1,list2)], key = lambda t: t[1])" ] }, { @@ -243,13 +404,26 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# your code here" + "# your code here\n", + "\n", + "sorted(d.items(), key = lambda t: t[1])" ] } ], @@ -269,7 +443,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.3" } }, "nbformat": 4,