From eda5dd95a9db71bc82ef3d3c342b1e263a7693cd Mon Sep 17 00:00:00 2001 From: lauratll Date: Sat, 5 Jun 2021 18:30:12 +0200 Subject: [PATCH 1/2] half way done --- your-code/main.ipynb | 411 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 384 insertions(+), 27 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 01257d4..9187a8c 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,18 +21,37 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "def modify_list(lst, lmbda):\n", + "def modify_list(lst,lmbda):\n", " \"\"\"\n", " Input: list and lambda expression\n", " Output: the transformed list\n", " \"\"\"\n", - " \n", - " # your code here\n", - " " + " transformed_list = list(map(lmbda,lst))\n", + " return transformed_list\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[273.15, 274.15, 275.15]" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "modify_list([0,1,2],(lambda celsius : celsius + 273.15))" ] }, { @@ -46,11 +65,31 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "f_kelvin= lambda celsius : celsius + 273.15" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "281.15" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f_kelvin(celsius=8)" ] }, { @@ -62,13 +101,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" + "modify_list(temps,f_kelvin)\n", + "\n" ] }, { @@ -82,11 +133,53 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "#we can use if else statements in lambda expressions\n", + "\n", + "mod = lambda a,b : 1 if (a%b==0) else 0" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(4,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(9,2)" ] }, { @@ -100,7 +193,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 57, "metadata": {}, "outputs": [], "source": [ @@ -110,8 +203,10 @@ " Output: a function that returns 1 if the number is \n", " divisible by another number (to be passed later) and zero otherwise.\n", " \"\"\"\n", - " \n", - " # your code here" + " mod = lambda a : 1 if (a%b==0) else 0\n", + " return mod\n", + "\n", + "#This lambda functions inside the divisor takes just one argument(a)" ] }, { @@ -123,11 +218,32 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + ".(a)>" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisor(5)\n", + "#this is still a function" + ] + }, + { + "cell_type": "code", + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "divisible5 =divisor(5)" ] }, { @@ -139,18 +255,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisible5(10)\n", + "#now we pass a = 10" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -199,14 +338,138 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "list1 = ['Green', 'cheese', 'English', 'tomato']\n", "list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n", "\n", - "# your code here" + "list3 = [elements for elements in zip(list1, list2)]" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Green', 'eggs'), ('cheese', 'cheese'), ('English', 'cucumber'), ('tomato', 'tomato')]\n" + ] + } + ], + "source": [ + "print(list3)\n", + "#We have a list of tuples" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [], + "source": [ + "list3 = [elements[i] for elements in zip(list1, list2)\n", + " for i in range(len(elements))]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(('Green', 'eggs'))" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(0, 2)" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(len(('Green', 'eggs')))\n", + "#the function range creates the sequence and\n", + "#already stops at the number you pass - 1" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1]" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(range(len(('Green', 'eggs'))))" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8]\n" + ] + } + ], + "source": [ + "print(list(range(0,9)))" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Green', 'eggs', 'cheese', 'cheese', 'English', 'cucumber', 'tomato', 'tomato']\n" + ] + } + ], + "source": [ + "print(list3)" ] }, { @@ -222,14 +485,108 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# your code here" + "list3 = [element for element in zip(list1,list2)]" + ] + }, + { + "cell_type": "code", + "execution_count": 150, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n" + ] + } + ], + "source": [ + "print(list3)" + ] + }, + { + "cell_type": "code", + "execution_count": 152, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 153, + "metadata": {}, + "outputs": [], + "source": [ + "#f = lambda x, y, z: x + y + z# arguments :output\n", + "lmba = lambda x: x[1][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "metadata": {}, + "outputs": [], + "source": [ + "second_element= list(map(lmba,list3))" + ] + }, + { + "cell_type": "code", + "execution_count": 156, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['E', 'H', 'L', 'M']\n" + ] + } + ], + "source": [ + "print(second_element)" + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "metadata": {}, + "outputs": [], + "source": [ + "list4 = list3.sort(key=lmba)" + ] + }, + { + "cell_type": "code", + "execution_count": 158, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "print(list4)" ] }, { @@ -269,7 +626,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, From 5ee0196db5b02eb005baffa1ed011b799d195e8c Mon Sep 17 00:00:00 2001 From: lauratll Date: Sun, 6 Jun 2021 13:15:12 +0200 Subject: [PATCH 2/2] lab finished --- your-code/main.ipynb | 137 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 25 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 9187a8c..749b559 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -485,7 +485,7 @@ }, { "cell_type": "code", - "execution_count": 149, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -497,7 +497,7 @@ }, { "cell_type": "code", - "execution_count": 150, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -514,99 +514,186 @@ }, { "cell_type": "code", - "execution_count": 152, + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "#f = lambda x, y, z: x + y + z# arguments :output\n", + "lmba = lambda x:x[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "#I can assign it to a variable... it just changes the original list\n", + "list3.sort(key=lmba)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "[('Political Science', 'Essay'), ('Computer Science', 'Homework'), ('Engineering', 'Lab'), ('Mathematics', 'Module')]\n" ] } ], - "source": [] + "source": [ + "print(list3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Sort a Dictionary by Values\n", + "\n", + "Given the dictionary below, sort it by values rather than by keys. Use a lambda function to specify the values as a sorting key." + ] }, { "cell_type": "code", - "execution_count": 153, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "#f = lambda x, y, z: x + y + z# arguments :output\n", - "lmba = lambda x: x[1][0]" + "# sort() function will mpdify the list it is called on, this one\n", + "#can not be used in a dictionary\n", + "# sorted() function will create a new list containing a sorted \n", + "#version of the list" ] }, { "cell_type": "code", - "execution_count": 155, + "execution_count": 66, "metadata": {}, "outputs": [], "source": [ - "second_element= list(map(lmba,list3))" + "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}" ] }, { "cell_type": "code", - "execution_count": 156, + "execution_count": 67, + "metadata": {}, + "outputs": [], + "source": [ + "d_sorted = sorted(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "['E', 'H', 'L', 'M']\n" + "['Audi', 'BMW', 'Honda', 'Toyota']\n" ] } ], "source": [ - "print(second_element)" + "print(d_sorted)\n", + "#sorts by key" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": {}, + "outputs": [], + "source": [ + "lmbda2= lambda x:x[1]" ] }, { "cell_type": "code", - "execution_count": 157, + "execution_count": 81, "metadata": {}, "outputs": [], "source": [ - "list4 = list3.sort(key=lmba)" + "d_sorted = sorted(d.items(),key=lmbda2)" ] }, { "cell_type": "code", - "execution_count": 158, + "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]\n" ] } ], "source": [ - "print(list4)" + "print(d_sorted)\n", + "#it return a list, I should convert in a dictionary" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 83, "metadata": {}, + "outputs": [], "source": [ - "# Bonus Challenge - Sort a Dictionary by Values\n", - "\n", - "Given the dictionary below, sort it by values rather than by keys. Use a lambda function to specify the values as a sorting key." + "d_sorted = dict(sorted(d.items(),key=lmbda2))" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], + "source": [ + "print(d_sorted)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, "metadata": {}, "outputs": [], "source": [ - "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", + "#trial in a dic comprehension\n", "\n", - "# your code here" + "d ={key:value for key,value in sorted(d.items(),key=lmbda2)}" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], + "source": [ + "print(d)" ] } ],