diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..1303bfe 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -38,7 +38,44 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here\n" + "# your code here\n", + "list3 = [1,2,3,4,5,6,7,8,9,10]\n", + "lambda_function = lambda x: x * 2" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def modify_list(a,b):\n", + " empty_list = []\n", + " for item in a:\n", + " item2 = b(item)\n", + " empty_list.append(item2)\n", + " return empty_list" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 8, 10, 8, 6, 4, 2, 6, 14]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list2 = modify_list([3,4,5,4,3,2,1,3,7], lambda_function)\n", + "list2 " ] }, { @@ -52,11 +89,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "a = lambda x: x + 273.15" ] }, { @@ -68,13 +106,25 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", - "\n", - "# Your code here:\n" + "temps_kelvin = []\n", + "# Your code here:\n", + "list(map(a, temps))" ] }, { @@ -88,11 +138,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "mod = lambda a, x: 1 if x%a==0 else 0 " ] }, { @@ -106,16 +157,17 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "def divisor(a):\n", + "def divisor(a,x):\n", " \"\"\"\n", " 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 mod(a,x)\n" ] }, { @@ -127,11 +179,13 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "def divisible5(x):\n", + " return divisor(5,x)\n" ] }, { @@ -143,18 +197,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "divisible5(8)" ] @@ -209,17 +285,32 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 153, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[False, False, True, False]" + ] + }, + "execution_count": 153, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "list1 = [1,2,4,4]\n", "list2 = [2,3,3,5]\n", - "## Zip the lists together \n", - "\n", - "## Print the zipped list \n", - "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "## Zip the lists together\n", + "zipped2 = zip(list1,list2)\n", + "## Print the zipped list\n", + "zipped2 = list(zipped2)\n", + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "compare = lambda x, y: x > y\n", + "compared = [compare(x, y) for x, y in zipped2 ]\n", + "compared\n", + "\n" ] }, { @@ -242,14 +333,31 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 192, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 192, + "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 = zip(list1,list2)\n", + "letter1 = lambda x: x[1][0]\n", + "result = sorted(zip(list1, list2), key=lambda x: letter1(x))\n", + "result" ] }, { @@ -263,13 +371,42 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 235, "metadata": {}, "outputs": [], + "source": [ + "def Convert(tup, di):\n", + " for a, b in tup:\n", + " di.setdefault(a, []).append(b)\n", + " return di" + ] + }, + { + "cell_type": "code", + "execution_count": 236, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Toyota': [1995], 'Honda': [1997], 'Audi': [2001], 'BMW': [2005]}" + ] + }, + "execution_count": 236, + "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", + "dict_result ={}\n", + "\n", + "letter1 = lambda x: x[1]\n", + "result = sorted(list(d.items()), key=lambda x: letter1(x))\n", + "#dict = dict(sorted(d.items(), key=lambda x: x[1]))\n", + "Convert(result,dict_result)" ] }, { @@ -296,7 +433,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.11.2" } }, "nbformat": 4,