diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 66a9984..bb64ed7 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,18 +34,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n" + ] + } + ], "source": [ - "l = [######]\n", - "f = lambda x: #define the lambda expression\n", + "l = [i for i in range(0, 10)]\n", + "f = lambda x: x*2\n", "b = []\n", "def modify_list(lst, fudduLambda):\n", - " for x in ####:\n", - " b.append(#####(x))\n", - "#Call modify_list(##,##)\n", - "#print b" + " for x in lst:\n", + " b.append(fudduLambda(x))\n", + "#Call \n", + "modify_list(l, f)\n", + "#print b\n", + "print(b)" ] }, { @@ -64,7 +74,8 @@ "outputs": [], "source": [ "# Your code here:\n", - "\n" + "\n", + "C_to_K = lambda x: x+273.15" ] }, { @@ -76,13 +87,26 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:" + "# Your code here:\n", + "temps_in_K = [C_to_K(t) for t in temps]\n", + "temps_in_K" ] }, { @@ -96,11 +120,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "mod = lambda x, b: 1 if b%x == 0 else 0" ] }, { @@ -114,7 +140,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -124,7 +150,8 @@ " output: a function that returns 1 if the number is divisible by another number (to be passed later) and zero otherwise\n", " \"\"\"\n", " \n", - " # Your code here:" + " # Your code here:\n", + " return lambda x: 1 if x%b == 0 else 0" ] }, { @@ -136,11 +163,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "divisible5 = divisor(5)" ] }, { @@ -152,22 +181,84 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(500)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible5(2)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -218,14 +309,27 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[(1, 2), (2, 3), (3, 4), (4, 5)]" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,3,4]\n", "list2 = [2,3,4,5]\n", "## Zip the lists together \n", - "## Print the zipped list " + "zipped = zip(list1, list2)\n", + "## Print the zipped list \n", + "list(zipped)" ] }, { @@ -261,6 +365,28 @@ "''' " ] }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "False\n", + "False\n" + ] + } + ], + "source": [ + "compare = lambda num: print(\"True\") if num[0]>num[1] else print(\"False\")\n", + "for num in zip(list1,list2):\n", + " compare(num)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -274,14 +400,32 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 45, + "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", + " \n", + "zipped_list = [word for word in zip(list1,list2)]\n", + "sorted_list = sorted(zipped_list, key=lambda x: x[1])\n", + "sorted_list" ] }, { @@ -295,13 +439,22 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Toyota', 'Honda', 'Audi', 'BMW']\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:" + "# Your code here:\n", + "print(sorted(d, key=lambda x: d[x]))" ] }, { @@ -328,7 +481,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4,