diff --git a/02-recursion-exercices.ipynb b/02-recursion-exercices.ipynb index 167b8ab969bb0475dc0c03a5583318d726e319bc..5173c4a16bb209c77e0e1d6f6cc742823ac7599b 100644 --- a/02-recursion-exercices.ipynb +++ b/02-recursion-exercices.ipynb @@ -1,5 +1,44 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "dadeb4a2", + "metadata": {}, + "source": [ + "# UE5 Fundamentals of Algorithms\n", + "## Exercices\n", + "### Ecole Centrale de Lyon, Bachelor of Science in Data Science for Responsible Business\n", + "#### [Romain Vuillemot](https://romain.vuillemot.net/)\n", + "\n", + "Before you turn this problem in:\n", + "- make sure everything runs as expected. \n", + " - first, **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart) \n", + " - then **run all cells** (in the menubar, select Cell$\\rightarrow$Run All).\n", + "- make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\"\n", + "- remove `raise NotImplementedError()` to get started with your answer\n", + "- bonus points (at the end of this notebook) are optionals\n", + "- write your name (and collaborators as a list if any) below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7c45a2a5", + "metadata": {}, + "outputs": [], + "source": [ + "ID = \"\"\n", + "COLLABORATORS_ID = []" + ] + }, + { + "cell_type": "markdown", + "id": "f14bc602", + "metadata": {}, + "source": [ + "---" + ] + }, { "cell_type": "markdown", "id": "a4e4fad3", @@ -40,10 +79,13 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": null, "id": "431fe8c1-91d1-40f3-a7a4-f4a3770a4a01", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "8b23d599da9ca2bdba48b9bcdd6e1165", "grade": false, "grade_id": "find_maximum_iterative", "locked": false, @@ -56,47 +98,32 @@ "outputs": [], "source": [ "def find_maximum_iterative(L):\n", - " ### BEGIN SOLUTION \n", - " if len(L) == 0:\n", - " raise ValueError(\"The list is empty.\")\n", - "\n", - " max_val = L[0]\n", - " for num in L:\n", - " if num > max_val:\n", - " max_val = num\n", - " return max_val\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 63, + "execution_count": null, "id": "f6baae3c-3660-4add-ab4b-48016cba3030", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "find_maximum_iterative([1, 3, 5, 7, 9])" ] }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "id": "e68b3e9a-418f-4950-9f27-18bb0fe90794", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "9cb3441fa27dd28ce74e368375de8080", "grade": true, "grade_id": "correct_find_maximum_iterative", "locked": true, @@ -128,10 +155,13 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": null, "id": "07668fd8", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "fcd4348ffdf05fa800f15e7dba033d8e", "grade": false, "grade_id": "find_maximum_recursive", "locked": false, @@ -144,43 +174,32 @@ "outputs": [], "source": [ "def find_maximum_recursive(L):\n", - " ### BEGIN SOLUTION\n", - " if len(L) == 1:\n", - " return L[0]\n", - " else:\n", - " return max(L[0], find_maximum_recursive(L[1:]))\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 135, + "execution_count": null, "id": "f9784710-4b2b-434c-bc47-da46fa410749", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 135, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "find_maximum_recursive([1, 3, 5, 7, 9])" ] }, { "cell_type": "code", - "execution_count": 90, + "execution_count": null, "id": "9b0161f8-0539-4e5e-921c-1886e61c0783", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "1aae7ba6a868f7afd015deedcdf48aa4", "grade": true, "grade_id": "correct_find_maximum_recursive", "locked": true, @@ -206,10 +225,13 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "id": "de424156-0b9b-41d0-8e38-ce335f35cec0", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "3872f4f922847e273bc5a45cd425c458", "grade": false, "grade_id": "sum_of_digits", "locked": false, @@ -222,43 +244,32 @@ "outputs": [], "source": [ "def sum_of_digits(n):\n", - " ### BEGIN SOLUTION\n", - " if n < 10:\n", - " return n\n", - " else:\n", - " return n % 10 + sum_of_digits(n // 10)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "id": "cec0caca-cb2c-4e4d-b004-27b3cf2ff611", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 66, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "sum_of_digits(10)" ] }, { "cell_type": "code", - "execution_count": 67, + "execution_count": null, "id": "bf276ca2-48ed-4e87-80f2-776f54b7062b", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "57a72fe3b7f20a42f9306fdde4b142f6", "grade": true, "grade_id": "correct_sum_of_digits", "locked": true, @@ -286,10 +297,13 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "abca03a0-7bcd-4ee6-b109-ff2f2da52bb6", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "c8c385784b4dd53e96c693511e7a2e80", "grade": false, "grade_id": "power", "locked": false, @@ -302,43 +316,32 @@ "outputs": [], "source": [ "def power(base, exponent):\n", - " ### BEGIN SOLUTION\n", - " if exponent == 0:\n", - " return 1\n", - " else:\n", - " return base * power(base, exponent - 1)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "abddd3b1-f75f-4cb6-a09e-54eed489c5b0", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "1024" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "power(2, 10)" ] }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "id": "8a6605fe-4f6f-45de-84a3-7601e2e2e6f6", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "c08d387ba43f0468538bfde5b39407d6", "grade": true, "grade_id": "correct_power", "locked": true, @@ -366,10 +369,13 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "ddc9826a-0863-4777-a08d-81b66652b5a5", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "762d91a747e5399677a26e44c5d8a041", "grade": false, "grade_id": "reverse_string", "locked": false, @@ -382,43 +388,32 @@ "outputs": [], "source": [ "def reverse_string(s):\n", - " ### BEGIN SOLUTION\n", - " if len(s) == 0:\n", - " return s\n", - " else:\n", - " return reverse_string(s[1:]) + s[0]\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "13acad7e-d03c-4ea6-ad86-baf02e0910eb", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "'niamoR'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "reverse_string(\"Romain\")" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "453c8e04-6cd9-4581-a206-dd479b6115cd", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "d42cbfb82df47fb4eb82e1530fa8f3cb", "grade": true, "grade_id": "correct_reverse_string", "locked": true, @@ -466,7 +461,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "219add7f", "metadata": { "slideshow": { @@ -486,30 +481,25 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "id": "0787df24-8234-4286-b910-5f9e456dd279", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The result is 15\n" - ] - } - ], + "outputs": [], "source": [ "print(\"The result is {}\".format(sequence(3)))" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "9c17cf1b-6d05-4589-af2b-c05cfcc33202", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "2137e4ccdef7bccaa7b7ed6b52f305a8", "grade": false, "grade_id": "sequence_recursive_tail", "locked": false, @@ -522,39 +512,31 @@ "outputs": [], "source": [ "def sequence_recursive_tail(n):\n", - " ### BEGIN SOLUTION\n", - " if n == 0:\n", - " return 1\n", - " return 2 * sequence_recursive_tail(n - 1) + 1\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "43507c43-de61-414d-b389-c0a771ad9e0c", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The result is still 15\n" - ] - } - ], + "outputs": [], "source": [ "print(\"The result is still {}\".format(sequence_recursive_tail(3)))" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "921939d8", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "0a1a74cd37dac7bf2b7e0090f1a418d1", "grade": false, "grade_id": "sequence_recursive_non_tail", "locked": false, @@ -567,39 +549,32 @@ "outputs": [], "source": [ "def sequence_recursive_non_tail(n, acc=1):\n", - " ### BEGIN SOLUTION\n", - " if n == 0:\n", - " return acc\n", - " return sequence_recursive_non_tail(n - 1, 2 * acc + 1)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "b754bf67-3d8d-42d9-a7ca-c43e2995837d", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The result is still 15\n" - ] - } - ], + "outputs": [], "source": [ "print(\"The result is still {}\".format(sequence_recursive_non_tail(3)))" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "dd923b7c-0cab-4678-8dc3-aad2ab9b25f9", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "357a18b503c50448c4d736ab9159a64c", "grade": true, "grade_id": "correct_sequence_recursive_non_tail", "locked": true, @@ -634,10 +609,13 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "id": "51bb3d08", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "c25b0fb5ce11b015f40949352274cfdf", "grade": false, "grade_id": "annagram_rec", "locked": false, @@ -650,42 +628,32 @@ "outputs": [], "source": [ "def annagram_rec(mot):\n", - " ### BEGIN SOLUTION\n", - " if len(mot) < 2: \n", - " return True\n", - " return mot[0] == mot [len(mot) - 1] and palindrome_rec(mot[1:len(mot)-1])\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "0c279628-9b96-4687-8e20-a954ab646e0f", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "annagram_rec(\"laval\")" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "id": "cf6fa61a-7c6f-4a32-96c2-b9fd50deacc6", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "94bbbbee3a4c7c005e8f9552a29ed556", "grade": true, "grade_id": "correct_annagram_rec", "locked": true, @@ -714,7 +682,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "48c65c83-be0c-41c4-8c04-1d29ac4415cb", "metadata": { "tags": [] @@ -729,10 +697,13 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": null, "id": "bf6e1a76", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "45353cb0fdd92b57e6f6f1739ed430b3", "grade": false, "grade_id": "recursive_gcd", "locked": false, @@ -745,41 +716,30 @@ "outputs": [], "source": [ "def recursive_gcd(a, b):\n", - " ### BEGIN SOLUTION\n", - " if b == 0:\n", - " return a\n", - " else:\n", - " return recursive_gcd(b, a % b)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "id": "4f1feace", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "recursive_gcd(10, 2)" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": null, "id": "e6ff1765-ff4b-49a5-80d3-684d2627e961", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "ed3ae90c2a4be5ff93a5d68b688f0f96", "grade": true, "grade_id": "correct_recursive_gcd", "locked": true, @@ -805,10 +765,13 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": null, "id": "1661dfb5-88f2-411f-8fe2-63bbaa29ce72", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "ccdab0e0b54c6ff4e1835b4a57a4ac72", "grade": false, "grade_id": "calculate_average_recursive", "locked": false, @@ -821,46 +784,30 @@ "outputs": [], "source": [ "def calculate_average_recursive(lst, index=0):\n", - " ### BEGIN SOLUTION\n", - " if not lst:\n", - " return None\n", - "\n", - " if index == len(lst):\n", - " return 0\n", - "\n", - " sum_rest = calculate_average_recursive(lst, index + 1)\n", - "\n", - " return (lst[index] + sum_rest) / (index + 1)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()\n" ] }, { "cell_type": "code", - "execution_count": 55, + "execution_count": null, "id": "0c5c72c6-3237-4f98-ab96-50a1838b833f", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2.5" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "calculate_average_recursive([1, 2, 3])" ] }, { "cell_type": "code", - "execution_count": 56, + "execution_count": null, "id": "9f18b6b7-d980-4a72-a2a8-3aa201003d21", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "27294a77eaee1e794183e6a31f145be1", "grade": true, "grade_id": "correct_calculate_average_recursive", "locked": true, @@ -886,10 +833,13 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": null, "id": "ac374a08-11c9-47e6-ba11-419549911266", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "58fec5e697f40d4e1934bdce3ae87647", "grade": false, "grade_id": "is_prime_recursive", "locked": false, @@ -902,54 +852,30 @@ "outputs": [], "source": [ "def is_prime_recursive(n, divisor=2):\n", - " ### BEGIN SOLUTION\n", - " if n < 2:\n", - " return False\n", - " \n", - " # Base case: If n is 2, it's a prime number\n", - " if n == 2:\n", - " return True\n", - " \n", - " # Base case: If n is divisible by the current divisor, it's not prime\n", - " if n % divisor == 0:\n", - " return False\n", - " \n", - " # Base case: If the divisor squared is greater than n, it's prime\n", - " if divisor * divisor > n:\n", - " return True\n", - " \n", - " # Recursive case: Check with the next divisor\n", - " return is_prime_recursive(n, divisor + 1)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "id": "996fc91f", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "is_prime_recursive(3)" ] }, { "cell_type": "code", - "execution_count": 43, + "execution_count": null, "id": "5be1051c-3b60-4810-b855-6f8575ad6380", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "f571ff3e3a5bc53ffbc828b255bfb6cb", "grade": true, "grade_id": "correct_is_prime_recursive", "locked": true, @@ -975,10 +901,13 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": null, "id": "cfeb0ad0-ed82-499e-9af3-64923291a0e7", "metadata": { + "deletable": false, "nbgrader": { + "cell_type": "code", + "checksum": "01c4970fffe483944b93049b5d8404a0", "grade": false, "grade_id": "count_occurrences", "locked": false, @@ -991,48 +920,32 @@ "outputs": [], "source": [ "def count_occurrences(arr, target, index=0):\n", - " ### BEGIN SOLUTION\n", - " if index == len(arr):\n", - " return 0\n", - " \n", - " # Check if the current element is equal to the target element.\n", - " # If it is, increment the count by 1.\n", - " count = (1 if arr[index] == target else 0)\n", - " \n", - " # Recursively call the function on the rest of the list (from the next index).\n", - " return count + count_occurrences(arr, target, index + 1)\n", - " ### END SOLUTION" + " # YOUR CODE HERE\n", + " raise NotImplementedError()" ] }, { "cell_type": "code", - "execution_count": 59, + "execution_count": null, "id": "49daec07-00b3-412e-ad44-5bafadbd9f36", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "4" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "count_occurrences([1, 2, 3, 4, 2, 2, 5, 6, 2], 2)" ] }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "id": "14a2eb85-1126-4506-93bb-4bf624d046b6", "metadata": { + "deletable": false, + "editable": false, "nbgrader": { + "cell_type": "code", + "checksum": "5df4afd6ed2f7dfd102b8c21f8ced2e7", "grade": true, "grade_id": "correct_count_occurrences", "locked": true, @@ -1068,7 +981,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": null, "id": "f9334f32-f760-46c0-a649-c82f267aba5b", "metadata": { "tags": [] @@ -1094,7 +1007,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": null, "id": "775b7e32-a5ae-431b-9987-fcd3671fd022", "metadata": { "tags": [] @@ -1117,7 +1030,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": null, "id": "f76ed657-3288-4b36-9175-21168d2eb761", "metadata": { "tags": [] @@ -1137,7 +1050,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "id": "b8bc657e-d491-4851-aa8c-ac2f2eac0841", "metadata": { "tags": [] @@ -1158,7 +1071,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": null, "id": "1c73ad0e-ddc9-483d-85a1-f2a65d04f30b", "metadata": { "tags": [] @@ -1171,7 +1084,7 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": null, "id": "8ce960f5-08da-449e-9e6f-dae215bc1b6a", "metadata": { "tags": [] @@ -1209,7 +1122,6 @@ } ], "metadata": { - "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python",