From 0e99c0a9dd6c38d317b6bd5af61b8626887cb5de Mon Sep 17 00:00:00 2001 From: Anna Fonte Date: Sun, 25 Oct 2020 19:36:29 +0100 Subject: [PATCH] Adding files to the repo --- your-code/main.ipynb | 257 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 226 insertions(+), 31 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 01257d4..443e572 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -21,7 +21,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ @@ -30,9 +30,41 @@ " Input: list and lambda expression\n", " Output: the transformed list\n", " \"\"\"\n", + " i = 0\n", " \n", " # your code here\n", - " " + " while i < len(lst):\n", + " lst[i] = lmbda(lst[i])\n", + " i += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "x = [1,2,3]\n", + "modify_list(x, lambda x: x*3)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 6, 9]\n" + ] + } + ], + "source": [ + "print(x)" ] }, { @@ -46,11 +78,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "celsius_to_kelvin = lambda x: x + 273.15" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "modify_list(temps, celsius_to_kelvin)" ] }, { @@ -62,13 +106,21 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 39, "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" + "modify_list(temps, celsius_to_kelvin)\n", + "print(temps)" ] }, { @@ -82,11 +134,32 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "mod = lambda x, y: 1 if x % y == 0 or y % x == 0 else 0" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mod(4,2)" ] }, { @@ -100,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 59, "metadata": {}, "outputs": [], "source": [ @@ -110,10 +183,35 @@ " 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", + " for i in range(2,b):\n", + " if mod(i,b) == 1:\n", + " return 1\n", + " \n", + " return 0\n", + "\n", " # your code here" ] }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisor(10)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -123,11 +221,21 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 64, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "divisible5 = divisor(5)\n", + "print(divisible5)" ] }, { @@ -139,20 +247,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(10)" + "divisor(10)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "divisible5(8)" + "divisor(8)" ] }, { @@ -199,14 +329,29 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 70, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Green', 'eggs'),\n", + " ('cheese', 'cheese'),\n", + " ('English', 'cucumber'),\n", + " ('tomato', 'tomato')]" + ] + }, + "execution_count": 70, + "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", + "[x for x in zip(list1, list2)]" ] }, { @@ -222,14 +367,52 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 71, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Engineering', 'Lab'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Political Science', 'Essay'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 71, + "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", + "[x for x in zip(list1, list2)]" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Political Science', 'Essay'),\n", + " ('Computer Science', 'Homework'),\n", + " ('Engineering', 'Lab'),\n", + " ('Mathematics', 'Module')]" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted([x for x in zip(list1, list2)], key = lambda x: x[1])" ] }, { @@ -243,13 +426,25 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n", "\n", - "# your code here" + "# your code here\n", + "sorted(d.items(), key = lambda x: x[1])" ] } ], @@ -269,7 +464,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.6" } }, "nbformat": 4,