From bdb41cbdef610ef3c1b3e9e2e8582472603ccd9c Mon Sep 17 00:00:00 2001 From: raulcastr Date: Thu, 19 Nov 2020 18:05:43 +0100 Subject: [PATCH] adding solutions --- your-code/main.ipynb | 181 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 143 insertions(+), 38 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 01257d4..55ecc9a 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,17 +21,22 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ + "\n", + "\n", + "\"\"\"\n", + "Input: list and lambda expression\n", + "Output: the transformed list\n", + "\"\"\"\n", + " \n", + "# your code here\n", "def modify_list(lst, lmbda):\n", - " \"\"\"\n", - " Input: list and lambda expression\n", - " Output: the transformed list\n", - " \"\"\"\n", + " return list(map(lmbda,lst))\n", + "\n", " \n", - " # your code here\n", " " ] }, @@ -46,11 +51,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "c_to_k=lambda x: x+273.15\n" ] }, { @@ -62,13 +68,25 @@ }, { "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", "\n", - "# your code here" + "# your code here\n", + "modify_list(temps,c_to_k)" ] }, { @@ -82,11 +100,25 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "mod = lambda x, y : 1 if x % y == 0 else 0\n", + "\n", + "mod(8,3)" ] }, { @@ -100,18 +132,20 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ - "def divisor(b):\n", - " \"\"\"\n", - " Input: a number\n", - " 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", + "\"\"\"\n", + "Input: a number\n", + "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" + "# your code here\n", + "def divisor(b):\n", + " return lambda a: mod(a, b)" ] }, { @@ -123,11 +157,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "divisible5 = divisor(5)" ] }, { @@ -139,18 +174,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -199,14 +256,26 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 89, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Green eggs', 'cheese cheese', 'English cucumber', 'tomato tomato']" + ] + }, + "execution_count": 89, + "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", + "[w[0]+ \" \"+ w[1] for w in zip(list1,list2)]" ] }, { @@ -222,14 +291,29 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 88, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Mathematics Module',\n", + " 'Engineering Lab',\n", + " 'Computer Science Homework',\n", + " 'Political Science Essay']" + ] + }, + "execution_count": 88, + "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", + "sorted([w[0]+\" \"+w[1] for w in zip(list1,list2)],key= lambda w: w[1])" ] }, { @@ -243,14 +327,35 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 86, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# your code here" + "# your code here\n", + "#{key: value for key, value in iterable}\n", + "dict(sorted(d.items(),key=lambda w: w[1]))\n", + "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -269,7 +374,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.3" } }, "nbformat": 4,