diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1ecb24d..29f3514 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -34,11 +34,31 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n" + ] + } + ], "source": [ - "# your code here\n" + "# your code here\n", + "\n", + "def modify_list(lst, func):\n", + " result = []\n", + " for num in lst:\n", + " result.append(func(num))\n", + " return result\n", + "\n", + "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "add_two = lambda x: x + 2\n", + "new_numbers = modify_list(numbers, add_two)\n", + "print(new_numbers)\n", + "\n" ] }, { @@ -52,11 +72,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "celsius_to_kelvin = lambda x: x + 273.15\n" ] }, { @@ -68,13 +89,23 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n" + ] + } + ], "source": [ "temps = [12, 23, 38, -55, 24]\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "kelvin_temps = modify_list(temps, celsius_to_kelvin)\n", + "print(kelvin_temps)" ] }, { @@ -88,11 +119,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "func = lambda x, y: 1 if x % y == 0 or y % x == 0 else 0" ] }, { @@ -106,7 +138,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -115,7 +147,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" + "\n", + "\n" ] }, { @@ -174,7 +207,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -186,7 +219,7 @@ " ('tomato', 'tomato')]" ] }, - "execution_count": 1, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -211,15 +244,36 @@ "cell_type": "code", "execution_count": 9, "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": 9, + "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 = zip(list1, list2)\n", "## Print the zipped list \n", + "print(list(zipped))\n", "\n", - "## Use a lambda expression to compare if: list1 element > list2 element\n" + "## Use a lambda expression to compare if: list1 element > list2 element\n", + "compare = lambda list1, list2: [x > y for x, y in zip(list1, list2)]\n", + "compare(list1,list2)" ] }, { @@ -244,12 +298,23 @@ "cell_type": "code", "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Political Science', 'Essay'), ('Computer Science', 'Homework'), ('Engineering', 'Lab'), ('Mathematics', 'Module')]\n" + ] + } + ], "source": [ "list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n", "list2 = ['Lab', 'Homework', 'Essay', 'Module']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "tuples = sorted(zip(list1, list2), key=lambda x: x[1][0])\n", + "\n", + "print(tuples)" ] }, { @@ -265,11 +330,22 @@ "cell_type": "code", "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Toyota': 1995, 'Honda': 1997, 'Audi': 2001, 'BMW': 2005}\n" + ] + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "sorted_dict = dict(sorted(d.items(), key=lambda x: x[1]))\n", + "\n", + "print(sorted_dict)" ] }, { @@ -282,7 +358,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -296,7 +372,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.13" } }, "nbformat": 4,