diff --git a/labs/02-recursion-exercises.ipynb b/labs/02-recursion-exercises.ipynb index 4c3ae444fda3964e7c9ffcdcf6f1e157ad2988de..b1cb714465394a51645766107f98a8b0fde51c24 100644 --- a/labs/02-recursion-exercises.ipynb +++ b/labs/02-recursion-exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "386bb47e", + "id": "61ded096", "metadata": {}, "source": [ "NAME:" @@ -43,7 +43,32 @@ "For each of the following questions:\n", "- In the `# YOUR CODE HERE` cell, remove `raise NotImplementedError()` to write your code\n", "- Write an example of use of your code or make sure the given examples and tests pass\n", - "- Add extra tests in the `#Tests` cell" + "- Add extra tests in the `#Tests` cell\n", + "\n", + "Recall on recursion:\n", + "\n", + "- Find a base case to stop the recursion\n", + "- Fin a decomposition of the problem to reach it\n", + "- Don't reach the Python recursion limits or infinite loops\n", + "\n", + "To compare iterative and recursive functions you may use the following time comparison functions:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6c23281-9c3d-488a-acb9-069964faff4b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import time\n", + "t1 = time.time()\n", + "# YOUR CODE\n", + "time.sleep(1)\n", + "dt = time.time() - t1\n", + "print(f\"Elapsed time: {dt:.4f} seconds\")" ] }, { @@ -683,7 +708,7 @@ "editable": false, "nbgrader": { "cell_type": "code", - "checksum": "6af4501cee439c4fc21ff5b23ac801fe", + "checksum": "34e9dc5de2d87132b4fccc04281e1faf", "grade": true, "grade_id": "correct_annagram_rec", "locked": true, @@ -701,7 +726,8 @@ "assert palindrom_rec(\"AA\")\n", "assert not palindrom_rec(\"ABC\")\n", "assert palindrom_rec(\"ABA\")\n", - "assert palindrom_rec(\"LAVAL\")" + "assert palindrom_rec(\"LAVAL\")\n", + "assert palindrom_rec(\"toto\") == False" ] }, { @@ -1023,14 +1049,14 @@ "tags": [] }, "source": [ - "## Exercise 11: 2D Binary search\n", + "### Exercise 11: 2D Binary search\n", "\n", "We want to search for a target value in a 2D matrix where:\n", "\n", "- Each row is sorted in ascending order.\n", "- The first element of each row is greater than the last element of the previous row.\n", "\n", - "Implement a recursive function adn" + "Implement a recursive function to solve this problem (hint: use a similar approach to binary search in a 1D array)." ] }, { @@ -1101,14 +1127,6 @@ "assert binary_search_2D(m, 11) == True\n", "assert binary_search_2D(m, 15) == False" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "34fea954-1cf6-4721-9d83-4504ca6e9901", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {