From 4fc2984fc0d6ee3e18e7c756665145cf2da8fc6e Mon Sep 17 00:00:00 2001 From: Ekraj Pokhrel Date: Sat, 22 Apr 2023 12:37:58 +0200 Subject: [PATCH] all the problem solved along with the bonus challenges --- your-code/main.ipynb | 212 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 177 insertions(+), 35 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..19eede3 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,42 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 5, 4, 3, 6, 7, 9, 8, 2, 5]" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "\n", + "# create the list with 10 values\n", + "my_list = [2,3,2,1,4,5,7,6,0,3] \n", + "# define lambda function that updates values by 2 \n", + "my_lambda = lambda x: x +2 \n", + "# define empty list\n", + "modified_list = [] \n", + "\n", + "# create the function modify_list which takes two input a list and a lambda expression and append\n", + "# the result in predefined list\n", + "\n", + "def modify_list(my_input_list, lambda_expression):\n", + " \"\"\" \n", + " this function takes a list and lambda expression as input and append the result in modified_list\n", + " \"\"\"\n", + " for element in my_input_list:\n", + " modified_list.append(lambda_expression(element))\n", + "\n", + "modify_list(my_list, my_lambda)\n", + "modified_list" ] }, { @@ -52,11 +83,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "celsius_to_kelvin = lambda x : x + 273.15" ] }, { @@ -68,13 +100,27 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 103, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "285.15K\n", + "296.15K\n", + "311.15K\n", + "218.14999999999998K\n", + "297.15K\n" + ] + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "for temp in temps:\n", + " print(str((celsius_to_kelvin)(temp)) + \"K\")\n" ] }, { @@ -88,11 +134,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda x, y : 1 if x % y == 0 else 0" ] }, { @@ -106,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 43, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +162,8 @@ " 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", + " return lambda y: mod(y, a)\n" ] }, { @@ -127,11 +175,12 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "divisible5 = divisor(5)\n" ] }, { @@ -143,18 +192,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisible5(10)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -209,17 +280,36 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (4, 3), (4, 5)]\n" + ] + }, + { + "data": { + "text/plain": [ + "[False, False, True, False]" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,4,4]\n", "list2 = [2,3,3,5]\n", "## Zip the lists together \n", - "\n", + "zipped = list(zip(list1, list2))\n", "## Print the zipped list \n", - "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "print(zipped)\n", + "# Use a lambda expression to compare if: list1 element > list2 element\n", + "[(lambda x, y: x > y) (list1[i], list2[i]) for i in range(len(list1))]\n" ] }, { @@ -242,14 +332,39 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 59, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n" + ] + }, + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 59, + "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 = list(zip(list1, list2))\n", + "print(zipped)\n", + "sort_list = sorted(zipped, key = lambda x: x[1])\n", + "sort_list" ] }, { @@ -263,26 +378,53 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 74, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "sorted_d = sorted(d.items(), key = lambda x: x[1])\n", + "dict(sorted_d)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "{'BMW': 2005, 'Audi': 2001, 'Honda': 1997, 'Toyota': 1995}" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted_d_desc = sorted(d.items(), key = lambda x: x[1], reverse = True)\n", + "dict(sorted_d_desc)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +438,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.13" } }, "nbformat": 4,