Commit 29b47f03 by Franck Thollard

### adding one DIY : find the missing element using set

parent f4b20daf
 ... ... @@ -561,6 +561,90 @@ "https://en.wikipedia.org/wiki/Hash_table" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "integer remove from the list: 5\n", "shuffled list: [4, 19, 14, 1, 8, 17, 15, 2, 3, 12, 0, 6, 16, 9, 11, 10, 7, 13, 18]\n" ] } ], "source": [ "from random import shuffle, randint\n", "\n", "n = 20\n", "i = randint(0, n-1)\n", "print('integer remove from the list:', i)\n", "l = list(range(n))\n", "l.remove(i)\n", "shuffle(l)\n", "print('shuffled list: ', l)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## DIY: back to the \"find the removed element\" problem\n", "\n", " - Could the problem be solved using set ? \n", " - What is the complexity of this solution ? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A possible solution : " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{5}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "full_set = set(range(n))\n", "changed_set = set(l)\n", "full_set - changed_set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complity :\n", "\n", " - line 1: n insertions --> O(n)\n", " - line 2 : n insertions --> O(n)\n", " - line 3: one traversal O(n), with one lookup at each time (O(1) -> O(n) \n", " \n", " -> Complixity of the whole algorithm : O(n)\n", " \n", "# Note " ] }, { "cell_type": "markdown", "metadata": { ... ... @@ -1085,7 +1169,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.2" "version": "3.7.2+" } }, "nbformat": 4, ... ...