diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb index eac9c379..3a92c544 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Anagram Check .ipynb @@ -205,7 +205,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Largest Continuous Sum .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Largest Continuous Sum .ipynb index be1f654d..231b035a 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Largest Continuous Sum .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - PRACTICE/Largest Continuous Sum .ipynb @@ -120,7 +120,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb index 91e7d3d2..0b69412e 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Anagram Check - SOLUTION.ipynb @@ -302,7 +302,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb index 2419f818..9be6a473 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Array Pair Sum - SOLUTION.ipynb @@ -69,9 +69,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -98,9 +96,7 @@ { "cell_type": "code", "execution_count": 16, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -140,23 +136,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb index 0b216746..0b1f1a95 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Find the Missing Element - SOLUTION.ipynb @@ -55,9 +55,7 @@ { "cell_type": "code", "execution_count": 46, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -86,9 +84,7 @@ { "cell_type": "code", "execution_count": 44, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "import collections\n", @@ -114,9 +110,7 @@ { "cell_type": "code", "execution_count": 47, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -167,9 +161,7 @@ { "cell_type": "code", "execution_count": 39, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -209,9 +201,7 @@ { "cell_type": "code", "execution_count": 43, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -250,23 +240,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb index f5fd1e30..0324bf68 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Largest Continuous Sum - SOLUTION.ipynb @@ -132,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb index 0e6577a7..07f4715a 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Sentence Reversal - SOLUTION.ipynb @@ -249,7 +249,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb index 68411f20..730067d4 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/String Compression -SOLUTION.ipynb @@ -154,7 +154,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb index 4b18c4c4..fa0f9f56 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions - SOLUTIONS/Unique Characters in String - SOLUTION.ipynb @@ -114,7 +114,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Unique Characters in String-checkpoint.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Unique Characters in String-checkpoint.ipynb index 9659d2bf..3b3253e0 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Unique Characters in String-checkpoint.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Unique Characters in String-checkpoint.ipynb @@ -20,14 +20,17 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def uni_char(s):\n", - " pass" + " chars = set()\n", + " for ch in s:\n", + " if ch in chars:\n", + " return False\n", + " chars.add(ch)\n", + " return True" ] }, { @@ -39,10 +42,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -65,7 +66,7 @@ " assert_equal(sol(''), True)\n", " assert_equal(sol('goo'), False)\n", " assert_equal(sol('abcdefg'), True)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "t = TestUnique()\n", @@ -82,23 +83,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb index fd6f0824..2cd3f5c9 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Anagram Check .ipynb @@ -25,23 +25,30 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 11, + "metadata": {}, "outputs": [], "source": [ - "def anagram(s1,s2):\n", - " \n", - " pass" + "def anagram(s1, s2):\n", + " count = [0 for _ in range(256)]\n", + " for c in s1:\n", + " c = c.lower()\n", + " if c.isalnum():\n", + " count[ord(c)] += 1\n", + " for c in s2:\n", + " c = c.lower()\n", + " if ord('a') <= ord(c) <= ord('z'):\n", + " count[ord(c)] -= 1\n", + " for c in count:\n", + " if c != 0:\n", + " return False\n", + " return True" ] }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "data": { @@ -49,7 +56,7 @@ "True" ] }, - "execution_count": 27, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -60,10 +67,8 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "data": { @@ -71,7 +76,7 @@ "True" ] }, - "execution_count": 34, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -82,10 +87,8 @@ }, { "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -93,7 +96,7 @@ "False" ] }, - "execution_count": 35, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -112,10 +115,8 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, + "execution_count": 15, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -139,7 +140,7 @@ " assert_equal(sol('hi man','hi man'),True)\n", " assert_equal(sol('aabbcc','aabbc'),False)\n", " assert_equal(sol('123','1 2'),False)\n", - " print \"ALL TEST CASES PASSED\"\n", + " print(\"ALL TEST CASES PASSED\")\n", "\n", "# Run Tests\n", "t = AnagramTest()\n", @@ -148,16 +149,18 @@ }, { "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" + "ename": "NameError", + "evalue": "name 'anagram2' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0managram2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'anagram2' is not defined" ] } ], @@ -175,23 +178,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb index 50ee6bfa..32d670a1 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Array Pair Sum .ipynb @@ -28,23 +28,29 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ - "def pair_sum(arr,k):\n", + "from collections import defaultdict\n", + "\n", + "def pair_sum(arr, k):\n", + " complements = set()\n", + " pairs = set()\n", " \n", - " pass" + " for el in arr:\n", + " if k - el in complements:\n", + " complement = k - el\n", + " pairs.add((min(el, complement), max(el, complement)))\n", + " complements.add(el)\n", + " \n", + " return len(pairs)" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -52,7 +58,7 @@ "2" ] }, - "execution_count": 14, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -70,10 +76,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -95,7 +99,7 @@ " assert_equal(sol([1,9,2,8,3,7,4,6,5,5,13,14,11,13,-1],10),6)\n", " assert_equal(sol([1,2,3,1],3),1)\n", " assert_equal(sol([1,3,2,2],4),2)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "#Run tests\n", "t = TestPair()\n", @@ -113,23 +117,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb index fe1e12d7..4d4adcfb 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Find the Missing Element .ipynb @@ -27,23 +27,34 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ - "def finder(arr1,arr2):\n", - " \n", - " pass" + "def finder(arr1, arr2):\n", + " xor = 0\n", + " for el in arr1:\n", + " xor ^= el\n", + " for el in arr2:\n", + " xor ^= el\n", + " return xor\n", + "\n", + "from collections import defaultdict\n", + "def finder2(arr1, arr2):\n", + " count = defaultdict(int)\n", + " for el in arr1:\n", + " count[el] += 1\n", + " for el in arr2:\n", + " count[el] -= 1\n", + " for k, v in count.items():\n", + " if v != 0:\n", + " return k" ] }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -51,7 +62,7 @@ "5" ] }, - "execution_count": 46, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -64,10 +75,8 @@ }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -75,7 +84,7 @@ "5" ] }, - "execution_count": 47, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -89,9 +98,7 @@ }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "_____" ] @@ -105,10 +112,8 @@ }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -130,7 +135,7 @@ " assert_equal(sol([5,5,7,7],[5,7,7]),5)\n", " assert_equal(sol([1,2,3,4,5,6,7],[3,7,2,1,4,6]),5)\n", " assert_equal(sol([9,8,7,6,5,4,3,2,1],[9,8,7,5,4,3,2,1]),6)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", "\n", "# Run test\n", "t = TestFinder()\n", @@ -143,27 +148,34 @@ "source": [ "## Good Job!" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb index a4220bb0..20d67f93 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Largest Continuous Sum .ipynb @@ -16,22 +16,22 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ - "def large_cont_sum(arr): \n", - " pass" + "def large_cont_sum(arr):\n", + " max_sum = curr_sum = arr[0]\n", + " for el in arr[1:]:\n", + " curr_sum = max(curr_sum + el, el)\n", + " max_sum = max(curr_sum, max_sum)\n", + " return max_sum" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -39,7 +39,7 @@ "29" ] }, - "execution_count": 25, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -52,7 +52,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "____\n", + "\n", "Many times in an interview setting the question also requires you to report back the start and end points of the sum. Keep this in mind and see if you can solve that problem, we'll see it in the mock interview section of the course!" ] }, @@ -65,10 +65,8 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -85,8 +83,10 @@ " def test(self,sol):\n", " assert_equal(sol([1,2,-1,3,4,-1]),9)\n", " assert_equal(sol([1,2,-1,3,4,10,10,-10,-1]),29)\n", + " assert_equal(sol([-1, -2, -3, 1, 8, -10, 50]),50)\n", + " assert_equal(sol([-1, -2, -3, -4]),-1)\n", " assert_equal(sol([-1,1]),1)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "#Run Test\n", "t = LargeContTest()\n", @@ -103,23 +103,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb index e6cf296a..b59d7a60 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Sentence Reversal.ipynb @@ -33,22 +33,34 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 129, + "metadata": {}, "outputs": [], "source": [ + "def rev_word2(s):\n", + " return ' '.join(reversed(s.split()))\n", + "\n", "def rev_word(s):\n", - " pass" + " words = []\n", + " i = len(s) - 1\n", + " while i >= 0:\n", + " while i >= 0 and s[i] == ' ':\n", + " i -= 1\n", + " word_end = i + 1\n", + " \n", + " while i >= 0 and s[i] != ' ':\n", + " i -= 1\n", + " word_start = i + 1\n", + " \n", + " if word_start != word_end:\n", + " words.append(s[word_start: word_end])\n", + " return ' '.join(words)" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 130, + "metadata": {}, "outputs": [ { "data": { @@ -56,7 +68,7 @@ "'go? to ready you are John, Hi'" ] }, - "execution_count": 4, + "execution_count": 130, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +79,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 131, + "metadata": {}, "outputs": [ { "data": { @@ -78,7 +88,7 @@ "'before space'" ] }, - "execution_count": 20, + "execution_count": 131, "metadata": {}, "output_type": "execute_result" } @@ -103,10 +113,8 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 132, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -130,7 +138,7 @@ " assert_equal(sol('space after '),'after space')\n", " assert_equal(sol(' Hello John how are you '),'you are how John Hello')\n", " assert_equal(sol('1'),'1')\n", - " print \"ALL TEST CASES PASSED\"\n", + " print(\"ALL TEST CASES PASSED\")\n", " \n", "# Run and test\n", "t = ReversalTest()\n", @@ -143,27 +151,34 @@ "source": [ "## Good Job!" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb index e8c7d5f4..1db3ecb5 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/String Compression .ipynb @@ -23,36 +23,51 @@ }, { "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": true - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ + "def sub_compress(char, n):\n", + " return list(f'{char}{n}')\n", + "\n", "def compress(s):\n", - " pass" + " if len(s) == 0:\n", + " return ''\n", + " \n", + " compressed = [] \n", + " curr_count = 1\n", + " curr_ch = s[0]\n", + " \n", + " for ch in s[1:]:\n", + " if ch == curr_ch:\n", + " curr_count += 1\n", + " else:\n", + " compressed += sub_compress(curr_ch, curr_count)\n", + " curr_ch = ch\n", + " curr_count = 1\n", + " compressed += sub_compress(curr_ch, curr_count)\n", + " \n", + " return ''.join(compressed)" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, + "execution_count": 16, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'A5B4C4'" + "'A1'" ] }, - "execution_count": 66, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "compress('AAAAABBBBCCCC')" + "compress('A')" ] }, { @@ -64,10 +79,8 @@ }, { "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -89,7 +102,7 @@ " assert_equal(sol(''), '')\n", " assert_equal(sol('AABBCC'), 'A2B2C2')\n", " assert_equal(sol('AAABCCDDDDD'), 'A3B1C2D5')\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", "\n", "# Run Tests\n", "t = TestCompress()\n", @@ -106,23 +119,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb index 9659d2bf..3b3253e0 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique Characters in String.ipynb @@ -20,14 +20,17 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def uni_char(s):\n", - " pass" + " chars = set()\n", + " for ch in s:\n", + " if ch in chars:\n", + " return False\n", + " chars.add(ch)\n", + " return True" ] }, { @@ -39,10 +42,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -65,7 +66,7 @@ " assert_equal(sol(''), True)\n", " assert_equal(sol('goo'), False)\n", " assert_equal(sol('abcdefg'), True)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "t = TestUnique()\n", @@ -82,23 +83,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement Depth First Search Algorithm-checkpoint.ipynb b/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement Depth First Search Algorithm-checkpoint.ipynb index 94da601e..26a60853 100644 --- a/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement Depth First Search Algorithm-checkpoint.ipynb +++ b/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement Depth First Search Algorithm-checkpoint.ipynb @@ -16,10 +16,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "graph = {'A': set(['B', 'C']),\n", @@ -36,27 +34,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def dfs(graph, start):\n", + " visited = set()\n", + " stack = [start]\n", + " \n", + " while stack:\n", + " node = stack.pop()\n", + " adjacent = graph[node]\n", + " \n", + " visited.add(node)\n", + " stack.extend(adjacent - visited)\n", + " return visited" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'A', 'B', 'C', 'D', 'E', 'F'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dfs(graph, 'A')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement a Graph-checkpoint.ipynb b/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement a Graph-checkpoint.ipynb index b6362791..479a8c7f 100644 --- a/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement a Graph-checkpoint.ipynb +++ b/Graphs/Graph Interview Questions/.ipynb_checkpoints/Implement a Graph-checkpoint.ipynb @@ -19,27 +19,92 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, key):\n", + " self.key = key\n", + " self.adjacent = {}\n", + "\n", + " def __str__(self):\n", + " return f'{self.key}, connected to: {self.adjacent}'\n", + " \n", + "class Graph:\n", + " def __init__(self):\n", + " self.nodes = {}\n", + " \n", + " def add_node(self, key):\n", + " node = Node(key)\n", + " self.nodes[key] = node\n", + " return node\n", + " \n", + " def add_edge(self, src, dest, weight=0):\n", + " if src not in self.nodes:\n", + " self.nodes[src] = Node(src)\n", + " if dest not in self.nodes:\n", + " self.nodes[dest] = Node(dest)\n", + " self.nodes[src].adjacent[dest] = weight" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0: <__main__.Node object at 0x7f76a10d6220>, 1: <__main__.Node object at 0x7f76a11f0cd0>, 2: <__main__.Node object at 0x7f76b04cdb80>, 3: <__main__.Node object at 0x7f76a11f0fd0>}\n", + "\n", + "1, connected to: {2: 0, 3: 0}\n" + ] + } + ], + "source": [ + "g = Graph()\n", + "g.add_edge(0, 1)\n", + "g.add_edge(2, 3)\n", + "g.add_edge(1, 2)\n", + "g.add_edge(1, 3)\n", + "\n", + "print(g.nodes)\n", + "print()\n", + "print(g.nodes[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb b/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb index 55bd824b..27c051a5 100644 --- a/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb +++ b/Graphs/Graph Interview Questions/Implement Breadth First Search Algorithm.ipynb @@ -16,10 +16,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "graph = {'A': set(['B', 'C']),\n", @@ -36,6 +34,55 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import deque\n", + "\n", + "def bfs(graph, start):\n", + " visited = set()\n", + " queue = deque([start])\n", + " \n", + " while queue:\n", + " node = queue.popleft()\n", + " adjacent = graph[node]\n", + " \n", + " visited.add(node)\n", + " queue.extend(adjacent - visited)\n", + " \n", + " return visited" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'A', 'B', 'C', 'D', 'E', 'F'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bfs(graph, 'A')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -54,7 +101,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb b/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb index 94da601e..26a60853 100644 --- a/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb +++ b/Graphs/Graph Interview Questions/Implement Depth First Search Algorithm.ipynb @@ -16,10 +16,8 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "graph = {'A': set(['B', 'C']),\n", @@ -36,27 +34,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def dfs(graph, start):\n", + " visited = set()\n", + " stack = [start]\n", + " \n", + " while stack:\n", + " node = stack.pop()\n", + " adjacent = graph[node]\n", + " \n", + " visited.add(node)\n", + " stack.extend(adjacent - visited)\n", + " return visited" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'A', 'B', 'C', 'D', 'E', 'F'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dfs(graph, 'A')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Graphs/Graph Interview Questions/Implement a Graph.ipynb b/Graphs/Graph Interview Questions/Implement a Graph.ipynb index b6362791..479a8c7f 100644 --- a/Graphs/Graph Interview Questions/Implement a Graph.ipynb +++ b/Graphs/Graph Interview Questions/Implement a Graph.ipynb @@ -19,27 +19,92 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, key):\n", + " self.key = key\n", + " self.adjacent = {}\n", + "\n", + " def __str__(self):\n", + " return f'{self.key}, connected to: {self.adjacent}'\n", + " \n", + "class Graph:\n", + " def __init__(self):\n", + " self.nodes = {}\n", + " \n", + " def add_node(self, key):\n", + " node = Node(key)\n", + " self.nodes[key] = node\n", + " return node\n", + " \n", + " def add_edge(self, src, dest, weight=0):\n", + " if src not in self.nodes:\n", + " self.nodes[src] = Node(src)\n", + " if dest not in self.nodes:\n", + " self.nodes[dest] = Node(dest)\n", + " self.nodes[src].adjacent[dest] = weight" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{0: <__main__.Node object at 0x7f76a10d6220>, 1: <__main__.Node object at 0x7f76a11f0cd0>, 2: <__main__.Node object at 0x7f76b04cdb80>, 3: <__main__.Node object at 0x7f76a11f0fd0>}\n", + "\n", + "1, connected to: {2: 0, 3: 0}\n" + ] + } + ], + "source": [ + "g = Graph()\n", + "g.add_edge(0, 1)\n", + "g.add_edge(2, 3)\n", + "g.add_edge(1, 2)\n", + "g.add_edge(1, 3)\n", + "\n", + "print(g.nodes)\n", + "print()\n", + "print(g.nodes[1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Graphs/Implementation of Breadth First Search.ipynb b/Graphs/Implementation of Breadth First Search.ipynb index 185b0183..7da9fcc3 100644 --- a/Graphs/Implementation of Breadth First Search.ipynb +++ b/Graphs/Implementation of Breadth First Search.ipynb @@ -168,7 +168,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Graphs/Implementation of Depth First Search.ipynb b/Graphs/Implementation of Depth First Search.ipynb index c111e89d..8b107268 100644 --- a/Graphs/Implementation of Depth First Search.ipynb +++ b/Graphs/Implementation of Depth First Search.ipynb @@ -170,7 +170,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Graphs/Implementation of Graph Overview.ipynb b/Graphs/Implementation of Graph Overview.ipynb index 3363f9a8..6cdb10df 100644 --- a/Graphs/Implementation of Graph Overview.ipynb +++ b/Graphs/Implementation of Graph Overview.ipynb @@ -148,7 +148,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Doubly Linked List-checkpoint.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Doubly Linked List-checkpoint.ipynb index 047d0421..bb719e2b 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Doubly Linked List-checkpoint.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Doubly Linked List-checkpoint.ipynb @@ -17,8 +17,11 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " pass" + "class Node:\n", + " def __init__(self, value):\n", + " self.value = value\n", + " self.prev = None\n", + " self.next = None" ] }, { @@ -29,7 +32,15 @@ }, "outputs": [], "source": [ - "# Create a Doubly Linked List here" + "a = Node(1)\n", + "b = Node(2)\n", + "c = Node(3)\n", + "\n", + "a.next = b\n", + "b.prev = a\n", + "\n", + "b.next = c\n", + "c.prev = b" ] }, { @@ -45,23 +56,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Singly Linked List-checkpoint.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Singly Linked List-checkpoint.ipynb index 92b84693..43f6705e 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Singly Linked List-checkpoint.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /.ipynb_checkpoints/Implement a Singly Linked List-checkpoint.ipynb @@ -17,8 +17,10 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " pass" + "class Node:\n", + " def __init__(self, value):\n", + " self.value = value\n", + " self.next = None" ] }, { @@ -29,7 +31,12 @@ }, "outputs": [], "source": [ - "# Create a Singly Linked List here" + "a = Node(1)\n", + "b = Node(2)\n", + "c = Node(3)\n", + "\n", + "a.next = b\n", + "b.next = c" ] }, { @@ -46,23 +53,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb index 047d0421..bb719e2b 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Doubly Linked List.ipynb @@ -17,8 +17,11 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " pass" + "class Node:\n", + " def __init__(self, value):\n", + " self.value = value\n", + " self.prev = None\n", + " self.next = None" ] }, { @@ -29,7 +32,15 @@ }, "outputs": [], "source": [ - "# Create a Doubly Linked List here" + "a = Node(1)\n", + "b = Node(2)\n", + "c = Node(3)\n", + "\n", + "a.next = b\n", + "b.prev = a\n", + "\n", + "b.next = c\n", + "c.prev = b" ] }, { @@ -45,23 +56,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb index 92b84693..43f6705e 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Implement a Singly Linked List.ipynb @@ -17,8 +17,10 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " pass" + "class Node:\n", + " def __init__(self, value):\n", + " self.value = value\n", + " self.next = None" ] }, { @@ -29,7 +31,12 @@ }, "outputs": [], "source": [ - "# Create a Singly Linked List here" + "a = Node(1)\n", + "b = Node(2)\n", + "c = Node(3)\n", + "\n", + "a.next = b\n", + "b.next = c" ] }, { @@ -46,23 +53,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb index de16594d..411b110c 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Nth to Last Node .ipynb @@ -12,10 +12,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "class Node:\n", @@ -35,9 +33,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "a = Node(1)\n", @@ -58,9 +54,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -87,15 +81,22 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "def nth_to_last_node(n, head):\n", - "\n", - " pass" + " left = head\n", + " right = head\n", + " \n", + " for i in range(n - 1):\n", + " right = right.nextnode\n", + " \n", + " while right.nextnode:\n", + " left = left.nextnode\n", + " right = right.nextnode\n", + " \n", + " return left" ] }, { @@ -107,10 +108,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -147,7 +146,7 @@ " def test(self,sol):\n", " \n", " assert_equal(sol(2,a),d)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "# Run tests\n", "t = TestNLast()\n", @@ -164,23 +163,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb index d6d333eb..07fbe7a3 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Linked List Reversal .ipynb @@ -15,16 +15,12 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "class Node(object):\n", - " \n", " def __init__(self,value):\n", - " \n", " self.value = value\n", " self.nextnode = None" ] @@ -40,15 +36,23 @@ }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [], "source": [ "def reverse(head):\n", + " prev_node = None\n", + " curr_node = head\n", + " next_node = None\n", " \n", - " pass" + " while curr_node is not None:\n", + " next_node = curr_node.next_node\n", + " curr_node.nextnode = prev_node\n", + " \n", + " prev_node = curr_node\n", + " curr_node = next_node\n", + " \n", + " return prev_node" ] }, { @@ -64,10 +68,8 @@ }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": true - }, + "execution_count": 20, + "metadata": {}, "outputs": [], "source": [ "# Create a list of 4 nodes\n", @@ -91,10 +93,8 @@ }, { "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": false - }, + "execution_count": 21, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -107,17 +107,15 @@ } ], "source": [ - "print a.nextnode.value\n", - "print b.nextnode.value\n", - "print c.nextnode.value" + "print(a.nextnode.value)\n", + "print(b.nextnode.value)\n", + "print(c.nextnode.value)" ] }, { "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, + "execution_count": 22, + "metadata": {}, "outputs": [ { "ename": "AttributeError", @@ -126,7 +124,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'" ] } @@ -144,32 +142,17 @@ }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "<__main__.Node at 0x104bd7dd0>" - ] - }, - "execution_count": 46, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": 23, + "metadata": {}, + "outputs": [], "source": [ "reverse(a)" ] }, { "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, + "execution_count": 24, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -182,17 +165,15 @@ } ], "source": [ - "print d.nextnode.value\n", - "print c.nextnode.value\n", - "print b.nextnode.value" + "print(d.nextnode.value)\n", + "print(c.nextnode.value)\n", + "print(b.nextnode.value)" ] }, { "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, + "execution_count": 25, + "metadata": {}, "outputs": [ { "ename": "AttributeError", @@ -201,13 +182,13 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;31m# This will give an error since it now points to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnextnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# This will give an error since it now points to None\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'" ] } ], "source": [ - "print a.nextnode.value # This will give an error since it now points to None" + "print(a.nextnode.value) # This will give an error since it now points to None" ] }, { @@ -222,23 +203,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb index 8c41dde7..03cf6973 100644 --- a/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb +++ b/Linked Lists/Linked Lists Interview Problems/Linked List Interview Problems /Singly Linked List Cycle Check.ipynb @@ -17,16 +17,12 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ - "class Node(object):\n", - " \n", - " def __init__(self,value):\n", - " \n", + "class Node:\n", + " def __init__(self, value):\n", " self.value = value\n", " self.nextnode = None" ] @@ -42,15 +38,17 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "def cycle_check(node):\n", - "\n", - " pass #Your function should return a boolean" + " visited = {node}\n", + " while node.nextnode != None:\n", + " if node.nextnode in visited:\n", + " return True\n", + " node = node.nextnode\n", + " return False" ] }, { @@ -62,10 +60,8 @@ }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -107,7 +103,7 @@ " assert_equal(sol(a),True)\n", " assert_equal(sol(x),False)\n", " \n", - " print \"ALL TEST CASES PASSED\"\n", + " print(\"ALL TEST CASES PASSED\")\n", " \n", "# Run Tests\n", "\n", @@ -125,23 +121,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb index 03231384..c69ff553 100644 --- a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb +++ b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb @@ -25,27 +25,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def product_list(array):\n", + " result = []\n", + " \n", + " for i in range(len(array)):\n", + " product = 1\n", + " for j in range(len(array)):\n", + " if j != i:\n", + " product *= array[j]\n", + " result.append(product)\n", + " \n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[24, 12, 8, 6]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product_list([1, 2, 3, 4])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 1.ipynb b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 1.ipynb index 301d05a0..61bc35de 100644 --- a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 1.ipynb +++ b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 1.ipynb @@ -30,27 +30,122 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def max_profit(prices):\n", + " if len(prices) < 2:\n", + " raise Exception(\"Need at least two stock prices!\")\n", + " \n", + " min_price = prices[0]\n", + " max_profit = prices[1] - prices[0]\n", + " \n", + " for price in prices[1:]: \n", + " if price - min_price > max_profit:\n", + " max_profit = price - min_price\n", + " \n", + " if price < min_price:\n", + " min_price = price\n", + " \n", + " return max_profit" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prices = [12, 11, 15, 3, 10]\n", + "max_profit(prices)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "Exception", + "evalue": "Need at least two stock prices!", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprices\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmax_profit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprices\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mmax_profit\u001b[0;34m(prices)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmax_profit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprices\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprices\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Need at least two stock prices!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mmin_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprices\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mException\u001b[0m: Need at least two stock prices!" + ] + } + ], + "source": [ + "prices = [12]\n", + "max_profit(prices)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-4" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prices = [10, 5, 1]\n", + "max_profit(prices)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 2 .ipynb b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 2 .ipynb index a68bb887..63d8c167 100644 --- a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 2 .ipynb +++ b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 2 .ipynb @@ -25,6 +25,59 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def product_list(array):\n", + " output = [None] * len(array)\n", + " \n", + " product = 1\n", + " i = 0\n", + " while i < len(array):\n", + " output[i] = product\n", + " product *= array[i]\n", + " i += 1\n", + " \n", + " product = 1\n", + " i = len(array) - 1\n", + " while i >= 0:\n", + " output[i] *= product\n", + " product *= array[i]\n", + " i -= 1\n", + " \n", + " return output" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[24, 12, 8, 6]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product_list([1, 2, 3, 4])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -43,7 +96,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.3" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 3.ipynb b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 3.ipynb index b7d2c7e4..c42fb653 100644 --- a/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 3.ipynb +++ b/Mock Interviews/Large E-Commerce Company/E-Commerce Company - Interview Problems/On-Site Question 3.ipynb @@ -13,10 +13,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "r1 = {\n", @@ -52,27 +50,243 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "def rect_overlap_dim(x1, w1, x2, w2):\n", + " #\n", + " # x1-----------------x1+w1\n", + " # x2-----------------x2+w2\n", + " #\n", + " #\n", + " # x1-----------------x1+w1\n", + " # x2-----------------x2+w2\n", + " #\n", + " \n", + " begin = max(x1, x2)\n", + " end = min(x1 + w1, x2 + w2)\n", + " \n", + " if begin >= end:\n", + " return None, None\n", + " \n", + " return begin, end - begin\n", + " \n", + "\n", + "def rect_overlap(r1, r2):\n", + " x1, y1, w1, h1 = r1['x'], r1['y'], r1['w'], r1['h']\n", + " x2, y2, w2, h2 = r2['x'], r2['y'], r2['w'], r2['h']\n", + " \n", + " x, w = rect_overlap_dim(x1, w1, x2, w2)\n", + " y, h = rect_overlap_dim(y1, h1, y2, h2)\n", + " \n", + " if not w or not h:\n", + " return None\n", + " \n", + " return {\n", + " 'x': x,\n", + " 'y': y,\n", + " 'w': w,\n", + " 'h': h,\n", + " }" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'x': 5, 'y': 5, 'w': 10, 'h': 5}" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# One rectangle inside the other.\n", + "\n", + "r1 = {\n", + " 'x': 0,\n", + " 'y': 0,\n", + " 'w': 20,\n", + " 'h': 10,\n", + "}\n", + "\n", + "r2 = {\n", + " 'x': 5,\n", + " 'y': 5,\n", + " 'w': 10,\n", + " 'h': 5,\n", + "}\n", + "\n", + "assert rect_overlap(r1, r2) == rect_overlap(r2, r1)\n", + "rect_overlap(r1, r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'x': 5, 'y': 5, 'w': 15, 'h': 5}" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# One rectangle partially inside the other.\n", + "\n", + "r1 = {\n", + " 'x': 0,\n", + " 'y': 0,\n", + " 'w': 20,\n", + " 'h': 10,\n", + "}\n", + "\n", + "r2 = {\n", + " 'x': 5,\n", + " 'y': 5,\n", + " 'w': 30,\n", + " 'h': 5,\n", + "}\n", + "\n", + "assert rect_overlap(r1, r2) == rect_overlap(r2, r1)\n", + "rect_overlap(r1, r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'x': 15, 'y': 15, 'w': 10, 'h': 10}" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Cross.\n", + "\n", + "r1 = {\n", + " 'x': 15,\n", + " 'y': 0,\n", + " 'w': 10,\n", + " 'h': 40,\n", + "}\n", + "\n", + "r2 = {\n", + " 'x': 0,\n", + " 'y': 15,\n", + " 'w': 40,\n", + " 'h': 10,\n", + "}\n", + "\n", + "\n", + "assert rect_overlap(r1, r2) == rect_overlap(r2, r1)\n", + "rect_overlap(r1, r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "# No overlap.\n", + "\n", + "r1 = {\n", + " 'x': 0,\n", + " 'y': 0,\n", + " 'w': 10,\n", + " 'h': 10,\n", + "}\n", + "\n", + "r2 = {\n", + " 'x': 50,\n", + " 'y': 50,\n", + " 'w': 10,\n", + " 'h': 10,\n", + "}\n", + "\n", + "\n", + "assert rect_overlap(r1, r2) == rect_overlap(r2, r1)\n", + "rect_overlap(r1, r2)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "# Sharing an edge.\n", + "\n", + "r1 = {\n", + " 'x': 0,\n", + " 'y': 0,\n", + " 'w': 10,\n", + " 'h': 10,\n", + "}\n", + "\n", + "r2 = {\n", + " 'x': 10,\n", + " 'y': 10,\n", + " 'w': 10,\n", + " 'h': 10,\n", + "}\n", + "\n", + "\n", + "assert rect_overlap(r1, r2) == rect_overlap(r2, r1)\n", + "rect_overlap(r1, r2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb index b3c51b99..8b698ab7 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb @@ -67,9 +67,7 @@ { "cell_type": "code", "execution_count": 24, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def convert5to7():\n", @@ -98,9 +96,7 @@ { "cell_type": "code", "execution_count": 25, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -127,23 +123,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 4 - SOLUTION.ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 4 - SOLUTION.ipynb index 8c32f0a1..e5874d89 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 4 - SOLUTION.ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems - SOLUTIONS/On-Site Question 4 - SOLUTION.ipynb @@ -49,9 +49,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -71,9 +69,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -93,9 +89,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -150,9 +144,7 @@ { "cell_type": "code", "execution_count": 9, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -172,9 +164,7 @@ { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -194,9 +184,7 @@ { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -241,23 +229,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb index a63ca005..efd0a97d 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb @@ -19,7 +19,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "___" + "from random import randint\n", + "from math import ceil\n", + "\n", + "def five_sided_dice():\n", + " return randint(1, 5)\n", + " \n", + "def double_five_sided_dice():\n", + " return five_sided_dice() - 1, five_sided_dice() - 1\n", + " \n", + "def seven_sided_dice():\n", + " r1, r2 = double_five_sided_dice()\n", + " while r1*5 + r2 > 21:\n", + " r1, r2 = double_five_sided_dice()\n", + " return ceil((r1*5 + r2) / 3)" ] }, { @@ -28,27 +41,34 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 3 -checkpoint.ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 3 -checkpoint.ipynb index 49a04c0d..f7b09b17 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 3 -checkpoint.ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/.ipynb_checkpoints/On-Site Question 3 -checkpoint.ipynb @@ -19,7 +19,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "____" + "def reverse(s):\n", + " if len(s) <= 1:\n", + " return s\n", + " return reverse(s[1:]) + s[0]" ] }, { @@ -32,23 +35,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 1 .ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 1 .ipynb index c4514711..a8095f2b 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 1 .ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 1 .ipynb @@ -19,7 +19,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "___" + "from random import randint\n", + "\n", + "def seven_sided_dice():\n", + " return randint(1, 7)\n", + "\n", + "def five_sided_dice():\n", + " r = seven_sided_dice()\n", + " while r > 5:\n", + " r = seven_sided_dice()\n", + " return r" ] }, { @@ -32,23 +41,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 2 .ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 2 .ipynb index a63ca005..efd0a97d 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 2 .ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 2 .ipynb @@ -19,7 +19,20 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "___" + "from random import randint\n", + "from math import ceil\n", + "\n", + "def five_sided_dice():\n", + " return randint(1, 5)\n", + " \n", + "def double_five_sided_dice():\n", + " return five_sided_dice() - 1, five_sided_dice() - 1\n", + " \n", + "def seven_sided_dice():\n", + " r1, r2 = double_five_sided_dice()\n", + " while r1*5 + r2 > 21:\n", + " r1, r2 = double_five_sided_dice()\n", + " return ceil((r1*5 + r2) / 3)" ] }, { @@ -28,27 +41,34 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 3 .ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 3 .ipynb index 49a04c0d..f7b09b17 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 3 .ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 3 .ipynb @@ -19,7 +19,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "____" + "def reverse(s):\n", + " if len(s) <= 1:\n", + " return s\n", + " return reverse(s[1:]) + s[0]" ] }, { @@ -32,23 +35,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 4 .ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 4 .ipynb index d69bf130..772e0f2a 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 4 .ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/On-Site Question 4 .ipynb @@ -24,23 +24,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/Phone Screen.ipynb b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/Phone Screen.ipynb index 8edb0203..3c083ddc 100644 --- a/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/Phone Screen.ipynb +++ b/Mock Interviews/Large Search Engine Company /Search Engine Company - Interview Problems/Phone Screen.ipynb @@ -46,7 +46,27 @@ { "cell_type": "markdown", "metadata": {}, - "source": [] + "source": [ + "def fib_rec(n):\n", + " if n == 0 or n == 1:\n", + " return 1\n", + " return fib_rec(n - 1) + fib_rec(n - 2)\n", + " \n", + "def fib_iter(n):\n", + " a = b = 1\n", + " for i in range(n - 1):\n", + " a, b = b, a + b\n", + " return a\n", + " \n", + "cache = {}\n", + "def fib_dyn(n):\n", + " if n == 0 or n == 1:\n", + " return 1\n", + " if n in cache:\n", + " return cache[n]\n", + " cache[n] = fib_dyn(n - 1) + fib_rec(n - 2)\n", + " return cache[n]" + ] }, { "cell_type": "markdown", @@ -58,23 +78,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 1 - SOLUTION.ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 1 - SOLUTION.ipynb index a471b4b1..e1a82d06 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 1 - SOLUTION.ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 1 - SOLUTION.ipynb @@ -94,9 +94,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -127,23 +125,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb index 0c613795..843ad59e 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions - SOLUTIONS/On-Site Question 2 - SOLUTION.ipynb @@ -32,10 +32,8 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "def solution(n, coins):\n", @@ -56,10 +54,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -67,7 +63,7 @@ "884" ] }, - "execution_count": 6, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -89,27 +85,54 @@ "source": [ "# Good Job!" ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "solution(5, [1, 5])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb index cd47095c..2f1e3e11 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /.ipynb_checkpoints/On-Site Question 2 -checkpoint.ipynb @@ -21,27 +21,52 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def solution(n, coins):\n", + " ways = [1] + [0] * n\n", + " \n", + " for coin in coins:\n", + " for i in range(coin, n + 1):\n", + " ways[i] += ways[i - coin]\n", + " \n", + " if n == 0:\n", + " return 0\n", + " return ways[n]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 1 .ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 1 .ipynb index ccfa72f7..f6cdd859 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 1 .ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 1 .ipynb @@ -23,27 +23,113 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "def largest_product(arr):\n", + " assert len(arr) >= 3\n", + " \n", + " max1 = max2 = max3 = None\n", + " min1 = min2 = None\n", + " \n", + " for el in arr:\n", + " if max1 is None or el > max1:\n", + " max3 = max2\n", + " max2 = max1\n", + " max1 = el\n", + " elif max2 is None or el > max2:\n", + " max3 = max2\n", + " max2 = el\n", + " elif max3 is None or el > max3:\n", + " max3 = el\n", + " \n", + " if min1 is None or el < min1:\n", + " min2 = min1\n", + " min1 = el\n", + " elif min2 is None or el < min2:\n", + " min2 = el\n", + " \n", + " a = max1 * max2 * max3\n", + " b = min1 * min2 * max1\n", + " \n", + " if a > b:\n", + " return (max1, max2, max3), a\n", + " else:\n", + " return (min1, min2, max1), b" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((-3, -2, 1), 6)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "largest_product([-1, -2, -3, 1])" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((-94, -82, 99), 763092)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = [99,-82,82,40,75,-24,39, -82, 5, 30, -25, -94, 93, -23, 48, 50, 49,-81,41,63]\n", + "largest_product(l)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 2 .ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 2 .ipynb index cd47095c..2f1e3e11 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 2 .ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 2 .ipynb @@ -21,27 +21,52 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def solution(n, coins):\n", + " ways = [1] + [0] * n\n", + " \n", + " for coin in coins:\n", + " for i in range(coin, n + 1):\n", + " ways[i] += ways[i - coin]\n", + " \n", + " if n == 0:\n", + " return 0\n", + " return ways[n]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 3 .ipynb b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 3 .ipynb index 380ec415..6937d2c1 100644 --- a/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 3 .ipynb +++ b/Mock Interviews/Ride Share Start-Up Company/Ride Share Company - Interview Questions /On-Site Question 3 .ipynb @@ -28,27 +28,102 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, key):\n", + " self.key = key\n", + " self.left = None\n", + " self.right = None" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def binary_check(node, lower=None, upper=None):\n", + " if node is None:\n", + " return True\n", + " \n", + " if lower is not None and node.key < lower:\n", + " return False\n", + " if upper is not None and node.key > upper:\n", + " return False\n", + " \n", + " return binary_check(node.left, lower, node.key) and binary_check(node.right, node.key, upper)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "tree = Node(5)\n", + "\n", + "tree.left = Node(4)\n", + "tree.left.left = Node(3)\n", + "tree.left.right = Node(5)\n", + "\n", + "tree.right = Node(8)\n", + "tree.right.left = Node(7)\n", + "tree.right.right = Node(9)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary_check(tree)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/On-Site Question 3 - SOLUTION.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/On-Site Question 3 - SOLUTION.ipynb index 62cbf2b9..4c104ede 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/On-Site Question 3 - SOLUTION.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions - SOLUTIONS/On-Site Question 3 - SOLUTION.ipynb @@ -63,9 +63,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -94,23 +92,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/On-Site Question 1-checkpoint.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/On-Site Question 1-checkpoint.ipynb index a72a4672..5a3e661c 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/On-Site Question 1-checkpoint.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/On-Site Question 1-checkpoint.ipynb @@ -23,27 +23,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def check(arr, target):\n", + " if len(arr) <= 2:\n", + " return False\n", + " \n", + " seen = set()\n", + " for el in arr:\n", + " if target - el in seen:\n", + " return True\n", + " seen.add(el)\n", + " \n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "check([2, 7, -1, 5, 10, 14], 25)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/Phone Screen -checkpoint.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/Phone Screen -checkpoint.ipynb index d082e4e5..3f73eca5 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/Phone Screen -checkpoint.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /.ipynb_checkpoints/Phone Screen -checkpoint.ipynb @@ -21,27 +21,70 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def remove_duplicates(s):\n", + " seen = set()\n", + " new_s = []\n", + " for c in s:\n", + " if c not in seen:\n", + " new_s.append(c)\n", + " seen.add(c)\n", + " return ''.join(new_s)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'tre avsl'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "remove_duplicates('tree traversal')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 1.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 1.ipynb index a72a4672..5a3e661c 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 1.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 1.ipynb @@ -23,27 +23,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def check(arr, target):\n", + " if len(arr) <= 2:\n", + " return False\n", + " \n", + " seen = set()\n", + " for el in arr:\n", + " if target - el in seen:\n", + " return True\n", + " seen.add(el)\n", + " \n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "check([2, 7, -1, 5, 10, 14], 25)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 2.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 2.ipynb index e65f70c4..0a0993c5 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 2.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 2.ipynb @@ -21,27 +21,73 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def find_unique(arr):\n", + " counts = {}\n", + " for el in arr:\n", + " if el in counts:\n", + " counts[el] += 1\n", + " else:\n", + " counts[el] = 1\n", + " \n", + " for el, count in counts.items():\n", + " if count == 1:\n", + " return el" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_unique([1, 1, 2, 2, 3, 4, 5])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 3.ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 3.ipynb index 139bd6c5..8aa67db7 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 3.ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /On-Site Question 3.ipynb @@ -21,27 +21,88 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "def counting_sort(arr, place):\n", + " size = len(arr)\n", + " count = [0] * 10\n", + " output = [0] * size\n", + " \n", + " for val in arr:\n", + " index = (val // place) % 10\n", + " count[index] += 1\n", + " \n", + " for i in range(1, 10):\n", + " count[i] += count[i - 1]\n", + " \n", + " for i in range(size - 1, -1, -1):\n", + " index = (arr[i] // place) % 10\n", + " output[count[index] - 1] = arr[i]\n", + " count[index] -= 1\n", + " \n", + " return output\n", + "\n", + "def radix_sort(arr):\n", + " max_element = max(arr)\n", + " place = 1\n", + " while max_element // place > 0:\n", + " arr = counting_sort(arr, place)\n", + " place *= 10\n", + " return arr" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 10, 22, 45, 101]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "radix_sort([10, 101, 1, 22, 45])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /Phone Screen .ipynb b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /Phone Screen .ipynb index d082e4e5..3f73eca5 100644 --- a/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /Phone Screen .ipynb +++ b/Mock Interviews/Social Network Company/Social Network Company - Interview Questions /Phone Screen .ipynb @@ -21,27 +21,70 @@ "source": [ "# Good Luck!" ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def remove_duplicates(s):\n", + " seen = set()\n", + " new_s = []\n", + " for c in s:\n", + " if c not in seen:\n", + " new_s.append(c)\n", + " seen.add(c)\n", + " return ''.join(new_s)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'tre avsl'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "remove_duplicates('tree traversal')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Memoization.ipynb b/Recursion/Memoization.ipynb index 8921ed05..b8ab9de9 100644 --- a/Recursion/Memoization.ipynb +++ b/Recursion/Memoization.ipynb @@ -137,7 +137,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Recursion/Recursion Homework Example Problems.ipynb b/Recursion/Recursion Homework Example Problems.ipynb index ebe52213..46592142 100644 --- a/Recursion/Recursion Homework Example Problems.ipynb +++ b/Recursion/Recursion Homework Example Problems.ipynb @@ -26,23 +26,20 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "def rec_sum(n):\n", - " \n", - " pass" + " if n == 0:\n", + " return 0\n", + " return n + rec_sum(n - 1)" ] }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 2, + "metadata": {}, "outputs": [ { "data": { @@ -50,7 +47,7 @@ "10" ] }, - "execution_count": 8, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -72,22 +69,21 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "def sum_func(n):\n", - " pass" + " if n // 10 == 0:\n", + " return n\n", + " else:\n", + " return (n % 10) + sum_func(n // 10)" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { @@ -95,7 +91,7 @@ "10" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -113,10 +109,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { "data": { @@ -124,7 +118,7 @@ "1" ] }, - "execution_count": 13, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -137,9 +131,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -182,9 +174,7 @@ { "cell_type": "code", "execution_count": 32, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -204,9 +194,7 @@ { "cell_type": "code", "execution_count": 33, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -226,9 +214,7 @@ { "cell_type": "code", "execution_count": 34, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -247,14 +233,52 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 35, + "metadata": {}, "outputs": [], "source": [ - "def word_split(phrase,list_of_words, output = None):\n", - " pass " + "def _word_split(phrase, word_set, min_len, max_len):\n", + " for i in range(min_len, max_len + 1):\n", + " word = phrase[:i]\n", + " if word in word_set:\n", + " rest = phrase[i:]\n", + " split = _word_split(rest, word_set, min_len, max_len)\n", + " return [word] + split\n", + " return []\n", + "\n", + "def word_split2(phrase, list_of_words):\n", + " min_len = min(len(w) for w in list_of_words)\n", + " max_len = max(len(w) for w in list_of_words)\n", + " word_set = set(list_of_words)\n", + " return _word_split(phrase, word_set, min_len, max_len)\n", + "\n", + "def word_split(phrase, list_of_words):\n", + " for word in list_of_words:\n", + " if phrase[:len(word)] == word:\n", + " rest = phrase[len(word):]\n", + " return [word] + word_split(rest, list_of_words)\n", + " return []\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['the', 'man', 'ran']\n", + "['i', 'love', 'dogs', 'John']\n", + "[]\n" + ] + } + ], + "source": [ + "print(word_split('themanran',['the','ran','man']))\n", + "print(word_split('ilovedogsJohn',['i','am','a','dogs','lover','love','John']))\n", + "print(word_split('themanran',['clown','ran','man']))" ] }, { @@ -269,23 +293,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb index a774e188..c1577654 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 3 - Fibonacci Sequence - SOLUTION.ipynb @@ -247,7 +247,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb index 2368b86b..ae8c7489 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems - SOLUTIONS/Recursion Problem 4 - Coin Change - SOLUTION.ipynb @@ -291,7 +291,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb index 4d4308f6..ecac0fe7 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 1 - Reverse String -checkpoint.ipynb @@ -17,23 +17,20 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "def reverse(s):\n", - " \n", - " pass" + " if len(s) < 2:\n", + " return s\n", + " return reverse(s[1:]) + s[0]" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -41,7 +38,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +64,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -94,7 +89,7 @@ " assert_equal(solution('hello world'),'dlrow olleh')\n", " assert_equal(solution('123456789'),'987654321')\n", " \n", - " print 'PASSED ALL TEST CASES!'\n", + " print('PASSED ALL TEST CASES!')\n", " \n", "# Run Tests\n", "test = TestReverse()\n", @@ -111,23 +106,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 3 - Fibonacci Sequence-checkpoint.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 3 - Fibonacci Sequence-checkpoint.ipynb index e85c24d0..13c9b1e8 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 3 - Fibonacci Sequence-checkpoint.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/.ipynb_checkpoints/Recursion Problem 3 - Fibonacci Sequence-checkpoint.ipynb @@ -41,23 +41,20 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "def fib_rec(n):\n", - " \n", - " pass" + " if n == 0 or n == 1:\n", + " return n\n", + " return fib_rec(n-1) + fib_rec(n-2)" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -65,7 +62,7 @@ "55" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -85,28 +82,29 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ "# Instantiate Cache information\n", "n = 10\n", "cache = [None] * (n + 1)\n", "\n", - "\n", "def fib_dyn(n):\n", + " if n == 0 or n == 1:\n", + " return n\n", + " \n", + " if cache[n] is not None:\n", + " return cache[n]\n", " \n", - " pass" + " cache[n] = fib_dyn(n-1) + fib_dyn(n-2)\n", + " return cache[n]" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -114,7 +112,7 @@ "55" ] }, - "execution_count": 9, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -134,23 +132,24 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [], "source": [ "def fib_iter(n):\n", + " a = 0\n", + " b = 1\n", " \n", - " pass" + " for i in range(n):\n", + " a, b = b, a + b \n", + " \n", + " return a" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "data": { @@ -158,7 +157,7 @@ "28657" ] }, - "execution_count": 14, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -179,9 +178,7 @@ { "cell_type": "code", "execution_count": 19, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -226,23 +223,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb index 4d4308f6..ecac0fe7 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 1 - Reverse String .ipynb @@ -17,23 +17,20 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "def reverse(s):\n", - " \n", - " pass" + " if len(s) < 2:\n", + " return s\n", + " return reverse(s[1:]) + s[0]" ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 4, + "metadata": {}, "outputs": [ { "data": { @@ -41,7 +38,7 @@ "'dlrow olleh'" ] }, - "execution_count": 10, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +64,8 @@ }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -94,7 +89,7 @@ " assert_equal(solution('hello world'),'dlrow olleh')\n", " assert_equal(solution('123456789'),'987654321')\n", " \n", - " print 'PASSED ALL TEST CASES!'\n", + " print('PASSED ALL TEST CASES!')\n", " \n", "# Run Tests\n", "test = TestReverse()\n", @@ -111,23 +106,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb index e2585c12..7f10d91a 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 2 - String Permutation.ipynb @@ -24,23 +24,25 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, + "execution_count": 11, + "metadata": {}, "outputs": [], "source": [ "def permute(s):\n", + " if len(s) <= 1:\n", + " return [s]\n", " \n", - " pass" + " permutations = []\n", + " for i, ch in enumerate(s):\n", + " rest = s[:i] + s[i+1:]\n", + " permutations += [s[i] + p for p in permute(rest)]\n", + " return permutations" ] }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [ { "data": { @@ -48,7 +50,7 @@ "['abc', 'acb', 'bac', 'bca', 'cab', 'cba']" ] }, - "execution_count": 24, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +69,8 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -94,7 +94,7 @@ " assert_equal(sorted(solution('abc')),sorted(['abc', 'acb', 'bac', 'bca', 'cab', 'cba']))\n", " assert_equal(sorted(solution('dog')),sorted(['dog', 'dgo', 'odg', 'ogd', 'gdo', 'god']) )\n", " \n", - " print 'All test cases passed.'\n", + " print('All test cases passed.')\n", " \n", "\n", "\n", @@ -113,23 +113,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb index e85c24d0..13c9b1e8 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 3 - Fibonacci Sequence.ipynb @@ -41,23 +41,20 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [], "source": [ "def fib_rec(n):\n", - " \n", - " pass" + " if n == 0 or n == 1:\n", + " return n\n", + " return fib_rec(n-1) + fib_rec(n-2)" ] }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { @@ -65,7 +62,7 @@ "55" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -85,28 +82,29 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ "# Instantiate Cache information\n", "n = 10\n", "cache = [None] * (n + 1)\n", "\n", - "\n", "def fib_dyn(n):\n", + " if n == 0 or n == 1:\n", + " return n\n", + " \n", + " if cache[n] is not None:\n", + " return cache[n]\n", " \n", - " pass" + " cache[n] = fib_dyn(n-1) + fib_dyn(n-2)\n", + " return cache[n]" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -114,7 +112,7 @@ "55" ] }, - "execution_count": 9, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -134,23 +132,24 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [], "source": [ "def fib_iter(n):\n", + " a = 0\n", + " b = 1\n", " \n", - " pass" + " for i in range(n):\n", + " a, b = b, a + b \n", + " \n", + " return a" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "data": { @@ -158,7 +157,7 @@ "28657" ] }, - "execution_count": 14, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -179,9 +178,7 @@ { "cell_type": "code", "execution_count": 19, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -226,23 +223,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb index f8785dfb..fddf0ab0 100644 --- a/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb +++ b/Recursion/Recursion Interview Problems/Recursion Problems/Recursion Problem 4 - Coin Change.ipynb @@ -39,23 +39,33 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 32, + "metadata": {}, "outputs": [], "source": [ - "def rec_coin(target,coins):\n", + "def rec_coin(target, coins, cache=None):\n", + " if cache is None:\n", + " cache = {}\n", + "\n", + " if target in coins:\n", + " return 1\n", + " if target in cache:\n", + " return cache[target]\n", " \n", - " pass" + " min_coins = target\n", + " for coin in (c for c in coins if c <= target):\n", + " n_coins = 1 + rec_coin(target - coin, coins, cache)\n", + " if n_coins < min_coins:\n", + " min_coins = n_coins\n", + "\n", + " cache[target] = min_coins\n", + " return min_coins" ] }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, + "execution_count": 33, + "metadata": {}, "outputs": [ { "data": { @@ -63,7 +73,7 @@ "2" ] }, - "execution_count": 3, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -85,11 +95,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Passed all tests.\n" + ] + } + ], "source": [ "\"\"\"\n", "RUN THIS CELL TO TEST YOUR FUNCTION.\n", @@ -106,7 +122,7 @@ " assert_equal(solution(45,coins),3)\n", " assert_equal(solution(23,coins),5)\n", " assert_equal(solution(74,coins),8)\n", - " print 'Passed all tests.'\n", + " print('Passed all tests.')\n", "# Run Test\n", "\n", "test = TestCoins()\n", @@ -125,23 +141,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Binary Search-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Binary Search-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..d97d67ee --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Binary Search-Copy1-checkpoint.ipynb @@ -0,0 +1,214 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Binary Search\n", + "\n", + "In this notebook we will just implement two versions of a simple binary search. View the video lecture for a full breakdown!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Binary Search" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def binary_search(arr,ele):\n", + " \n", + " # First and last index values\n", + " first = 0\n", + " last = len(arr) - 1\n", + " \n", + " found = False\n", + " \n", + " \n", + " while first <= last and not found:\n", + " \n", + " mid = (first+last)/2 # or // for Python 3\n", + " \n", + " # Match found\n", + " if arr[mid] == ele:\n", + " found = True\n", + " \n", + " # Set new midpoints up or down depending on comparison\n", + " else:\n", + " # Set down\n", + " if ele < arr[mid]:\n", + " last = mid -1\n", + " # Set up \n", + " else:\n", + " first = mid + 1\n", + " \n", + " return found" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "# list must already be sorted!\n", + "arr = [1,2,3,4,5,6,7,8,9,10]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary_search(arr,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary_search(arr,2.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Recursive Version of Binary Search" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def rec_bin_search(arr,ele):\n", + " \n", + " # Base Case!\n", + " if len(arr) == 0:\n", + " return False\n", + " \n", + " # Recursive Case\n", + " else:\n", + " \n", + " mid = len(arr)/2\n", + " \n", + " # If match found\n", + " if arr[mid]==ele:\n", + " return True\n", + " \n", + " else:\n", + " \n", + " # Call again on second half\n", + " if elearr[k+1]:\n", + " temp = arr[k]\n", + " arr[k] = arr[k+1]\n", + " arr[k+1] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [3,2,13,4,6,5,7,8,1,20]\n", + "bubble_sort(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 13, 20]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Insertion Sort-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Insertion Sort-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..95a0bf83 --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Insertion Sort-Copy1-checkpoint.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Insertion Sort\n", + "\n", + "Insertion Sort builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Insertion sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Animation](http://cs.armstrong.edu/liang/animation/web/InsertionSort.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/insertion-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def insertion_sort(arr):\n", + " for i in range(1, len(arr)):\n", + " val = arr[i]\n", + " pos = i\n", + " \n", + " while pos > 0 and arr[pos - 1] > val:\n", + " arr[pos] = arr[pos - 1]\n", + " pos = pos - 1\n", + " \n", + " arr[pos] = val" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 8, 12, 25, 41]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr =[3,5,4,6,8,1,2,12,41,25]\n", + "insertion_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Merge Sort-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Merge Sort-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..6e3f5460 --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Merge Sort-Copy1-checkpoint.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Merge Sort\n", + "\n", + "Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Merge sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Merge_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/merge-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def merge(arr, left_part, right_part):\n", + " l_index = r_index = index = 0\n", + " \n", + " # Merge left and right parts.\n", + " while l_index < len(left_part) and r_index < len(right_part):\n", + " if left_part[l_index] < right_part[r_index]:\n", + " arr[index] = left_part[l_index]\n", + " l_index += 1\n", + " else:\n", + " arr[index] = right_part[r_index]\n", + " r_index += 1\n", + " index += 1\n", + "\n", + " # Merge leftovers.\n", + " while l_index < len(left_part):\n", + " arr[index] = left_part[l_index]\n", + " l_index += 1\n", + " index += 1\n", + "\n", + " while r_index < len(right_part):\n", + " arr[index] = right_part[r_index]\n", + " r_index += 1\n", + " index += 1\n", + " \n", + "def merge_sort(arr):\n", + " if len(arr) <= 1:\n", + " return\n", + " \n", + " mid = len(arr) // 2\n", + " left_part = arr[:mid]\n", + " right_part = arr[mid:]\n", + " \n", + " merge_sort(left_part)\n", + " merge_sort(right_part)\n", + " merge(arr, left_part, right_part)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 4, 5, 6, 7, 8, 11, 23]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [11,2,5,4,7,6,8,1,23]\n", + "merge_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Quick Sort-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Quick Sort-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..72856103 --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Quick Sort-Copy1-checkpoint.ipynb @@ -0,0 +1,111 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Quick Sort\n", + "\n", + "A quick sort first selects a value, which is called the pivot value. Although there are many different ways to choose the pivot value, we will simply use the first item in the list. The role of the pivot value is to assist with splitting the list. The actual position where the pivot value belongs in the final sorted list, commonly called the split point, will be used to divide the list for subsequent calls to the quick sort." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Insertion sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Quicksort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/quick-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def quick_sort(arr):\n", + " quick_sort_helper(arr, 0, len(arr) - 1)\n", + "\n", + "def quick_sort_helper(arr, first, last):\n", + " if first < last:\n", + " split_point = partition(arr, first, last)\n", + " quick_sort_helper(arr, first, split_point - 1)\n", + " quick_sort_helper(arr, split_point + 1, last)\n", + " \n", + "def partition(arr, first, last):\n", + " pivot = arr[first]\n", + " left_mark = first + 1\n", + " right_mark = last\n", + " \n", + " while True:\n", + " while left_mark <= right_mark and arr[left_mark] <= pivot:\n", + " left_mark += 1\n", + " while left_mark <= right_mark and arr[right_mark] > pivot:\n", + " right_mark -= 1\n", + " \n", + " if right_mark < left_mark:\n", + " break\n", + " \n", + " arr[left_mark], arr[right_mark] = arr[right_mark], arr[left_mark]\n", + " \n", + " arr[first], arr[right_mark] = arr[right_mark], arr[first]\n", + " return right_mark" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 4, 5, 6, 7, 11, 12]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [2,5,4,6,7,3,1,4,12,11]\n", + "quick_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Selection Sort-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Selection Sort-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..e4ebf214 --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Selection Sort-Copy1-checkpoint.ipynb @@ -0,0 +1,102 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Selection Sort\n", + "\n", + "The selection sort improves on the bubble sort by making only one exchange for every pass through the list. In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing the pass, places it in the proper location. As with a bubble sort, after the first pass, the largest item is in the correct place. After the second pass, the next largest is in place. This process continues and requires n−1 passes to sort n items, since the final item must be in place after the (n−1) st pass." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Selection sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Animation](http://cs.armstrong.edu/liang/animation/web/SelectionSort.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/selection-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def selection_sort(arr):\n", + " \n", + " # For every slot in array\n", + " for fillslot in range(len(arr)-1,0,-1):\n", + " positionOfMax=0\n", + " \n", + " # For every set of 0 to fillslot+1\n", + " for location in range(1,fillslot+1):\n", + " # Set maximum's location\n", + " if arr[location]>arr[positionOfMax]:\n", + " positionOfMax = location\n", + "\n", + " temp = arr[fillslot]\n", + " arr[fillslot] = arr[positionOfMax]\n", + " arr[positionOfMax] = temp" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 5, 6, 7, 8, 12, 21, 40]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [3,5,2,7,6,8,12,40,21]\n", + "selection_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/.ipynb_checkpoints/Implementation of Shell Sort-Copy1-checkpoint.ipynb b/Sorting and Searching/.ipynb_checkpoints/Implementation of Shell Sort-Copy1-checkpoint.ipynb new file mode 100644 index 00000000..2f345aac --- /dev/null +++ b/Sorting and Searching/.ipynb_checkpoints/Implementation of Shell Sort-Copy1-checkpoint.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Shell Sort\n", + "\n", + "The shell sort improves on the insertion sort by breaking the original list into a number of smaller sublists, each of which is sorted using an insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an increment i, sometimes called the gap, to create a sublist by choosing all items that are i items apart." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Shell sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Shellsort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/shell-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def shell_sort(arr):\n", + " # Start with the biggest possible gap.\n", + " sublist_count = len(arr) // 2\n", + " \n", + " while sublist_count > 0:\n", + " for start in range(sublist_count):\n", + " gap_insertion_sort(arr, start, sublist_count)\n", + " # Halve the size of the gap.\n", + " sublist_count //= 2\n", + "\n", + "def gap_insertion_sort(arr, start, gap):\n", + " for i in range(start + gap, len(arr), gap):\n", + " val = arr[i]\n", + " pos = i\n", + " \n", + " while pos > 0 and arr[pos - gap] > val:\n", + " arr[pos] = arr[pos - gap]\n", + " pos = pos - gap\n", + " \n", + " arr[pos] = val" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 7, 21, 23, 24, 45, 45, 67, 90]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [45,67,23,45,21,24,7,2,6,4,90]\n", + "shell_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Binary Search-Copy1.ipynb b/Sorting and Searching/Implementation of Binary Search-Copy1.ipynb new file mode 100644 index 00000000..6ed96bde --- /dev/null +++ b/Sorting and Searching/Implementation of Binary Search-Copy1.ipynb @@ -0,0 +1,189 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Binary Search\n", + "\n", + "In this notebook we will just implement two versions of a simple binary search. View the video lecture for a full breakdown!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Binary Search" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def binary_search(array, el):\n", + " left = 0\n", + " right = len(array) - 1\n", + " found = False\n", + " \n", + " while left <= right and not found:\n", + " mid = (left + right) // 2\n", + " \n", + " if array[mid] > el:\n", + " right = mid - 1\n", + " elif array[mid] < el:\n", + " left = mid + 1\n", + " else:\n", + " found = True\n", + " \n", + " return found" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "# list must already be sorted!\n", + "arr = [1,2,3,4,5,6,7,8,9,10]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary_search(arr,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "binary_search(arr,2.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Recursive Version of Binary Search" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def rec_bin_search(array, el):\n", + " if len(array) == 0:\n", + " return False\n", + " \n", + " mid = len(array) // 2\n", + " \n", + " if array[mid] > el:\n", + " return rec_bin_search(array[:mid], el)\n", + " elif array[mid] < el:\n", + " return rec_bin_search(array[mid + 1:], el)\n", + " else:\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_bin_search(arr,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rec_bin_search(arr,15)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Binary Search.ipynb b/Sorting and Searching/Implementation of Binary Search.ipynb index aa558014..d97d67ee 100644 --- a/Sorting and Searching/Implementation of Binary Search.ipynb +++ b/Sorting and Searching/Implementation of Binary Search.ipynb @@ -56,9 +56,7 @@ { "cell_type": "code", "execution_count": 14, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# list must already be sorted!\n", @@ -68,9 +66,7 @@ { "cell_type": "code", "execution_count": 15, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -90,9 +86,7 @@ { "cell_type": "code", "execution_count": 16, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -119,9 +113,7 @@ { "cell_type": "code", "execution_count": 17, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def rec_bin_search(arr,ele):\n", @@ -153,9 +145,7 @@ { "cell_type": "code", "execution_count": 18, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -175,9 +165,7 @@ { "cell_type": "code", "execution_count": 20, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -204,23 +192,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Sorting and Searching/Implementation of Bubble Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Bubble Sort-Copy1.ipynb new file mode 100644 index 00000000..244d2da5 --- /dev/null +++ b/Sorting and Searching/Implementation of Bubble Sort-Copy1.ipynb @@ -0,0 +1,98 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of a Bubble Sort\n", + "\n", + "The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Bubble sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Animation](http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/bubble-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def bubble_sort(arr):\n", + " for n in range(len(arr) - 1, 0, -1):\n", + " for i in range(n):\n", + " if arr[i] > arr[i + 1]:\n", + " arr[i], arr[i + 1] = arr[i + 1], arr[i]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "arr = [3,2,13,4,6,5,7,8,1,20]\n", + "bubble_sort(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 13, 20]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Great Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Bubble Sort.ipynb b/Sorting and Searching/Implementation of Bubble Sort.ipynb index 4385de2d..d83ce722 100644 --- a/Sorting and Searching/Implementation of Bubble Sort.ipynb +++ b/Sorting and Searching/Implementation of Bubble Sort.ipynb @@ -97,7 +97,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Sorting and Searching/Implementation of Insertion Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Insertion Sort-Copy1.ipynb new file mode 100644 index 00000000..95a0bf83 --- /dev/null +++ b/Sorting and Searching/Implementation of Insertion Sort-Copy1.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Insertion Sort\n", + "\n", + "Insertion Sort builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Insertion sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Insertion_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Animation](http://cs.armstrong.edu/liang/animation/web/InsertionSort.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/insertion-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def insertion_sort(arr):\n", + " for i in range(1, len(arr)):\n", + " val = arr[i]\n", + " pos = i\n", + " \n", + " while pos > 0 and arr[pos - 1] > val:\n", + " arr[pos] = arr[pos - 1]\n", + " pos = pos - 1\n", + " \n", + " arr[pos] = val" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 8, 12, 25, 41]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr =[3,5,4,6,8,1,2,12,41,25]\n", + "insertion_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Insertion Sort.ipynb b/Sorting and Searching/Implementation of Insertion Sort.ipynb index b00bb835..27dd5cab 100644 --- a/Sorting and Searching/Implementation of Insertion Sort.ipynb +++ b/Sorting and Searching/Implementation of Insertion Sort.ipynb @@ -95,7 +95,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Sorting and Searching/Implementation of Merge Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Merge Sort-Copy1.ipynb new file mode 100644 index 00000000..6e3f5460 --- /dev/null +++ b/Sorting and Searching/Implementation of Merge Sort-Copy1.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Merge Sort\n", + "\n", + "Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, it is sorted by definition (the base case). If the list has more than one item, we split the list and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Merge sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Merge_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/merge-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "def merge(arr, left_part, right_part):\n", + " l_index = r_index = index = 0\n", + " \n", + " # Merge left and right parts.\n", + " while l_index < len(left_part) and r_index < len(right_part):\n", + " if left_part[l_index] < right_part[r_index]:\n", + " arr[index] = left_part[l_index]\n", + " l_index += 1\n", + " else:\n", + " arr[index] = right_part[r_index]\n", + " r_index += 1\n", + " index += 1\n", + "\n", + " # Merge leftovers.\n", + " while l_index < len(left_part):\n", + " arr[index] = left_part[l_index]\n", + " l_index += 1\n", + " index += 1\n", + "\n", + " while r_index < len(right_part):\n", + " arr[index] = right_part[r_index]\n", + " r_index += 1\n", + " index += 1\n", + " \n", + "def merge_sort(arr):\n", + " if len(arr) <= 1:\n", + " return\n", + " \n", + " mid = len(arr) // 2\n", + " left_part = arr[:mid]\n", + " right_part = arr[mid:]\n", + " \n", + " merge_sort(left_part)\n", + " merge_sort(right_part)\n", + " merge(arr, left_part, right_part)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 4, 5, 6, 7, 8, 11, 23]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [11,2,5,4,7,6,8,1,23]\n", + "merge_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Merge Sort.ipynb b/Sorting and Searching/Implementation of Merge Sort.ipynb index 0ad3b17d..1a6ab700 100644 --- a/Sorting and Searching/Implementation of Merge Sort.ipynb +++ b/Sorting and Searching/Implementation of Merge Sort.ipynb @@ -109,7 +109,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Sorting and Searching/Implementation of Quick Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Quick Sort-Copy1.ipynb new file mode 100644 index 00000000..72856103 --- /dev/null +++ b/Sorting and Searching/Implementation of Quick Sort-Copy1.ipynb @@ -0,0 +1,111 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Quick Sort\n", + "\n", + "A quick sort first selects a value, which is called the pivot value. Although there are many different ways to choose the pivot value, we will simply use the first item in the list. The role of the pivot value is to assist with splitting the list. The actual position where the pivot value belongs in the final sorted list, commonly called the split point, will be used to divide the list for subsequent calls to the quick sort." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Insertion sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Quicksort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/quick-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def quick_sort(arr):\n", + " quick_sort_helper(arr, 0, len(arr) - 1)\n", + "\n", + "def quick_sort_helper(arr, first, last):\n", + " if first < last:\n", + " split_point = partition(arr, first, last)\n", + " quick_sort_helper(arr, first, split_point - 1)\n", + " quick_sort_helper(arr, split_point + 1, last)\n", + " \n", + "def partition(arr, first, last):\n", + " pivot = arr[first]\n", + " left_mark = first + 1\n", + " right_mark = last\n", + " \n", + " while True:\n", + " while left_mark <= right_mark and arr[left_mark] <= pivot:\n", + " left_mark += 1\n", + " while left_mark <= right_mark and arr[right_mark] > pivot:\n", + " right_mark -= 1\n", + " \n", + " if right_mark < left_mark:\n", + " break\n", + " \n", + " arr[left_mark], arr[right_mark] = arr[right_mark], arr[left_mark]\n", + " \n", + " arr[first], arr[right_mark] = arr[right_mark], arr[first]\n", + " return right_mark" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 4, 5, 6, 7, 11, 12]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [2,5,4,6,7,3,1,4,12,11]\n", + "quick_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Quick Sort.ipynb b/Sorting and Searching/Implementation of Quick Sort.ipynb index bf2e99c0..83161598 100644 --- a/Sorting and Searching/Implementation of Quick Sort.ipynb +++ b/Sorting and Searching/Implementation of Quick Sort.ipynb @@ -122,7 +122,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Sorting and Searching/Implementation of Selection Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Selection Sort-Copy1.ipynb new file mode 100644 index 00000000..156faec6 --- /dev/null +++ b/Sorting and Searching/Implementation of Selection Sort-Copy1.ipynb @@ -0,0 +1,92 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Selection Sort\n", + "\n", + "The selection sort improves on the bubble sort by making only one exchange for every pass through the list. In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing the pass, places it in the proper location. As with a bubble sort, after the first pass, the largest item is in the correct place. After the second pass, the next largest is in place. This process continues and requires n−1 passes to sort n items, since the final item must be in place after the (n−1) st pass." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Selection sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Animation](http://cs.armstrong.edu/liang/animation/web/SelectionSort.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/selection-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def selection_sort(arr):\n", + " for fill_slot in range(len(arr) - 1, 0, -1):\n", + " max_index = 0\n", + " for i in range(1, fill_slot + 1):\n", + " if arr[i] > arr[max_index]:\n", + " max_index = i\n", + " arr[max_index], arr[fill_slot] = arr[fill_slot], arr[max_index]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 5, 6, 7, 8, 12, 21, 40]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [3,5,2,7,6,8,12,40,21]\n", + "selection_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Selection Sort.ipynb b/Sorting and Searching/Implementation of Selection Sort.ipynb index c6d82a2d..e4ebf214 100644 --- a/Sorting and Searching/Implementation of Selection Sort.ipynb +++ b/Sorting and Searching/Implementation of Selection Sort.ipynb @@ -51,9 +51,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -82,23 +80,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Sorting and Searching/Implementation of Shell Sort-Copy1.ipynb b/Sorting and Searching/Implementation of Shell Sort-Copy1.ipynb new file mode 100644 index 00000000..2f345aac --- /dev/null +++ b/Sorting and Searching/Implementation of Shell Sort-Copy1.ipynb @@ -0,0 +1,104 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Implementation of Shell Sort\n", + "\n", + "The shell sort improves on the insertion sort by breaking the original list into a number of smaller sublists, each of which is sorted using an insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead of breaking the list into sublists of contiguous items, the shell sort uses an increment i, sometimes called the gap, to create a sublist by choosing all items that are i items apart." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Resources for Review\n", + "\n", + "Check out the resources below for a review of Shell sort!\n", + "\n", + "* [Wikipedia](https://en.wikipedia.org/wiki/Shellsort)\n", + "* [Visual Algo](http://visualgo.net/sorting.html)\n", + "* [Sorting Algorithms Animcation with Pseudocode](http://www.sorting-algorithms.com/shell-sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def shell_sort(arr):\n", + " # Start with the biggest possible gap.\n", + " sublist_count = len(arr) // 2\n", + " \n", + " while sublist_count > 0:\n", + " for start in range(sublist_count):\n", + " gap_insertion_sort(arr, start, sublist_count)\n", + " # Halve the size of the gap.\n", + " sublist_count //= 2\n", + "\n", + "def gap_insertion_sort(arr, start, gap):\n", + " for i in range(start + gap, len(arr), gap):\n", + " val = arr[i]\n", + " pos = i\n", + " \n", + " while pos > 0 and arr[pos - gap] > val:\n", + " arr[pos] = arr[pos - gap]\n", + " pos = pos - gap\n", + " \n", + " arr[pos] = val" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 4, 6, 7, 21, 23, 24, 45, 45, 67, 90]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr = [45,67,23,45,21,24,7,2,6,4,90]\n", + "shell_sort(arr)\n", + "arr" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Good Job!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Sorting and Searching/Implementation of Shell Sort.ipynb b/Sorting and Searching/Implementation of Shell Sort.ipynb index 52bc30fc..d769aabb 100644 --- a/Sorting and Searching/Implementation of Shell Sort.ipynb +++ b/Sorting and Searching/Implementation of Shell Sort.ipynb @@ -61,9 +61,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -92,23 +90,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions -SOLUTIONS/Implement a Queue -Using Two Stacks - SOLUTION.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions -SOLUTIONS/Implement a Queue -Using Two Stacks - SOLUTION.ipynb index 74ca5fee..a4cd94d5 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions -SOLUTIONS/Implement a Queue -Using Two Stacks - SOLUTION.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions -SOLUTIONS/Implement a Queue -Using Two Stacks - SOLUTION.ipynb @@ -124,7 +124,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Deque -checkpoint.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Deque -checkpoint.ipynb index d5c255f7..9fd7bde2 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Deque -checkpoint.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Deque -checkpoint.ipynb @@ -16,36 +16,60 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ - "class Deque(object):\n", - " pass" + "class Deque:\n", + " def __init__(self):\n", + " self.items = []\n", + " \n", + " def is_empty(self):\n", + " return len(self.items) == 0\n", + " \n", + " def push_front(self, item):\n", + " self.items.append(item)\n", + " \n", + " def push_rear(self, item):\n", + " self.items.insert(0, item)\n", + " \n", + " def pop_front(self):\n", + " return self.items.pop()\n", + " \n", + " def pop_rear(self):\n", + " return self.items.pop(0)\n", + " \n", + " def size(self):\n", + " return len(self.items)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Queue-checkpoint.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Queue-checkpoint.ipynb index a5ac0019..ccd672d5 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Queue-checkpoint.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Queue-checkpoint.ipynb @@ -16,36 +16,122 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Queue:\n", + " def __init__(self):\n", + " self.items = []\n", + " \n", + " def enqueue(self, item):\n", + " self.items.append(item)\n", + " \n", + " def dequeue(self):\n", + " return self.items.pop(0)\n", + " \n", + " def size(self):\n", + " return len(self.items)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ - "class Queue(object):\n", - " pass" + "q = Queue()\n", + "q.enqueue(1)\n", + "q.enqueue(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.size()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Stack -checkpoint.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Stack -checkpoint.ipynb index 6dea3952..64907b72 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Stack -checkpoint.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /.ipynb_checkpoints/Implement a Stack -checkpoint.ipynb @@ -25,33 +25,46 @@ }, "outputs": [], "source": [ - "class Stack(object):\n", + "class Stack:\n", + " def __init__(self):\n", + " self.items = []\n", " \n", + " def is_empty(self):\n", + " return len(self.items) == 0\n", " \n", - " # Fill out the Stack Methods here\n", - " pass" + " def push(self, item):\n", + " self.items.append(item)\n", + " \n", + " def pop(self):\n", + " self.items.pop()\n", + " \n", + " def peek(self):\n", + " return self.items[-1]\n", + " \n", + " def size(self):\n", + " return len(self.items)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb index 274f3ace..c3f56c74 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Balanced Parentheses Check .ipynb @@ -20,23 +20,34 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "def balance_check(s):\n", - " \n", - " pass" + " stack = []\n", + " matches = {\n", + " '(': ')',\n", + " '[': ']',\n", + " '{': '}',\n", + " }\n", + " for ch in s:\n", + " if ch in matches.keys():\n", + " stack.append(ch)\n", + " else:\n", + " if len(stack) == 0:\n", + " return False\n", + " \n", + " prev = stack.pop()\n", + " if matches[prev] != ch:\n", + " return False\n", + " return len(stack) == 0" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -44,7 +55,7 @@ "True" ] }, - "execution_count": 13, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -55,10 +66,8 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, "outputs": [ { "data": { @@ -66,7 +75,7 @@ "True" ] }, - "execution_count": 16, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -77,10 +86,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": 21, + "metadata": {}, "outputs": [ { "data": { @@ -88,7 +95,7 @@ "False" ] }, - "execution_count": 17, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -106,10 +113,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 22, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -131,7 +136,7 @@ " assert_equal(sol('[](){([[[]]])}('),False)\n", " assert_equal(sol('[{{{(())}}}]((()))'),True)\n", " assert_equal(sol('[[[]])]'),False)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "# Run Tests\n", "\n", @@ -149,23 +154,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb index d5c255f7..9fd7bde2 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Deque .ipynb @@ -16,36 +16,60 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ - "class Deque(object):\n", - " pass" + "class Deque:\n", + " def __init__(self):\n", + " self.items = []\n", + " \n", + " def is_empty(self):\n", + " return len(self.items) == 0\n", + " \n", + " def push_front(self, item):\n", + " self.items.append(item)\n", + " \n", + " def push_rear(self, item):\n", + " self.items.insert(0, item)\n", + " \n", + " def pop_front(self):\n", + " return self.items.pop()\n", + " \n", + " def pop_rear(self):\n", + " return self.items.pop(0)\n", + " \n", + " def size(self):\n", + " return len(self.items)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb index 907956a6..5c9f3c23 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue -Using Two Stacks .ipynb @@ -33,29 +33,26 @@ }, { "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": true - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ - "class Queue2Stacks(object):\n", - " \n", + "class Queue2Stacks:\n", " def __init__(self):\n", - " \n", - " # Two Stacks\n", " self.stack1 = []\n", " self.stack2 = []\n", " \n", - " def enqueue(self,element):\n", + " def enqueue(self, element):\n", + " while len(self.stack1) > 0:\n", + " self.stack2.append(self.stack1.pop())\n", + " \n", + " self.stack1.append(element)\n", " \n", - " # FILL OUT CODE HERE\n", - " pass\n", + " while len(self.stack2) > 0:\n", + " self.stack1.append(self.stack2.pop())\n", " \n", " def dequeue(self):\n", - " \n", - " # FILL OUT CODE HERE\n", - " pass " + " return self.stack1.pop()" ] }, { @@ -69,10 +66,8 @@ }, { "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -92,11 +87,11 @@ "\"\"\"\n", "q = Queue2Stacks()\n", "\n", - "for i in xrange(5):\n", + "for i in range(5):\n", " q.enqueue(i)\n", " \n", - "for i in xrange(5):\n", - " print q.dequeue()" + "for i in range(5):\n", + " print(q.dequeue())" ] }, { @@ -109,23 +104,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb index a5ac0019..ccd672d5 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Queue.ipynb @@ -16,36 +16,122 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "class Queue:\n", + " def __init__(self):\n", + " self.items = []\n", + " \n", + " def enqueue(self, item):\n", + " self.items.append(item)\n", + " \n", + " def dequeue(self):\n", + " return self.items.pop(0)\n", + " \n", + " def size(self):\n", + " return len(self.items)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, "outputs": [], "source": [ - "class Queue(object):\n", - " pass" + "q = Queue()\n", + "q.enqueue(1)\n", + "q.enqueue(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.dequeue()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.size()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb index 6dea3952..64907b72 100644 --- a/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb +++ b/Stacks, Queues and Deques/Stacks, Queues, and Deques Interview Problems/Stacks, Queues, Deques Interview Questions /Implement a Stack .ipynb @@ -25,33 +25,46 @@ }, "outputs": [], "source": [ - "class Stack(object):\n", + "class Stack:\n", + " def __init__(self):\n", + " self.items = []\n", " \n", + " def is_empty(self):\n", + " return len(self.items) == 0\n", " \n", - " # Fill out the Stack Methods here\n", - " pass" + " def push(self, item):\n", + " self.items.append(item)\n", + " \n", + " def pop(self):\n", + " self.items.pop()\n", + " \n", + " def peek(self):\n", + " return self.items[-1]\n", + " \n", + " def size(self):\n", + " return len(self.items)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Trees/Binary Heap Implementation.ipynb b/Trees/Binary Heap Implementation.ipynb index e5630cee..1db7affc 100644 --- a/Trees/Binary Heap Implementation.ipynb +++ b/Trees/Binary Heap Implementation.ipynb @@ -27,10 +27,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "class BinHeap:\n", @@ -98,35 +96,71 @@ " i = i - 1" ] }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "b = BinHeap()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "b.buildHeap([3, 9, 1, 7, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 7, 9, 3]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b.heapList" + ] + }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Trees/Binary Search Trees.ipynb b/Trees/Binary Search Trees.ipynb index c6679bb0..ef49b31d 100644 --- a/Trees/Binary Search Trees.ipynb +++ b/Trees/Binary Search Trees.ipynb @@ -17,9 +17,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class TreeNode:\n", @@ -249,9 +247,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -283,23 +279,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Trees/Trees Interview Problems - SOLUTIONS/Tree Level Order Print - SOLUTION.ipynb b/Trees/Trees Interview Problems - SOLUTIONS/Tree Level Order Print - SOLUTION.ipynb index 9b0ec477..d14e3691 100644 --- a/Trees/Trees Interview Problems - SOLUTIONS/Tree Level Order Print - SOLUTION.ipynb +++ b/Trees/Trees Interview Problems - SOLUTIONS/Tree Level Order Print - SOLUTION.ipynb @@ -105,7 +105,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Trees/Trees Interview Problems - SOLUTIONS/Trim a Binary Search Tree - SOLUTION.ipynb b/Trees/Trees Interview Problems - SOLUTIONS/Trim a Binary Search Tree - SOLUTION.ipynb index cb59ddc4..80cdc5b1 100644 --- a/Trees/Trees Interview Problems - SOLUTIONS/Trim a Binary Search Tree - SOLUTION.ipynb +++ b/Trees/Trees Interview Problems - SOLUTIONS/Trim a Binary Search Tree - SOLUTION.ipynb @@ -92,7 +92,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.8.1" } }, "nbformat": 4, diff --git a/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb b/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb index 74a68fe1..87beff8d 100644 --- a/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb +++ b/Trees/Trees Interview Problems/Binary Search Tree Check.ipynb @@ -19,13 +19,66 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ - "# Code goes Here" + "class Node:\n", + " def __init__(self, key):\n", + " self.key = key\n", + " self.left = None\n", + " self.right = None\n", + "\n", + "def in_order(node):\n", + " if not node:\n", + " return []\n", + " array = in_order(node.left)\n", + " array.append(node.key)\n", + " array += in_order(node.right)\n", + " return array\n", + " \n", + "def bst_check2(node):\n", + " array = in_order(node)\n", + " for i in range(len(array) - 1):\n", + " if not array[i] <= array[i+1]:\n", + " return False\n", + " return True\n", + "\n", + "def bst_check(node, min_node = None, max_node = None):\n", + " if not node:\n", + " return True\n", + " \n", + " if min_node and node.key < min_node.key:\n", + " return False\n", + " \n", + " if max_node and node.key > max_node.key:\n", + " return False\n", + " \n", + " return bst_check(node.left, min_node, node) and bst_check(node.right, node, max_node)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "tree = Node(5)\n", + "tree.left = Node(3)\n", + "tree.right = Node(7)\n", + "\n", + "tree.left.left = Node(1)\n", + "tree.left.right = Node(7)\n", + "\n", + "print(bst_check(tree))" ] }, { @@ -38,23 +91,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Trees/Trees Interview Problems/Tree Level Order Print.ipynb b/Trees/Trees Interview Problems/Tree Level Order Print.ipynb index 7d44b4d9..c826cd71 100644 --- a/Trees/Trees Interview Problems/Tree Level Order Print.ipynb +++ b/Trees/Trees Interview Problems/Tree Level Order Print.ipynb @@ -28,10 +28,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "class Node:\n", @@ -43,37 +41,83 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ + "from collections import deque\n", + "\n", "def levelOrderPrint(tree):\n", - " #Code here\n", - " pass" + " current_level = 0\n", + " queue = deque([(tree, current_level)])\n", + " \n", + " while queue:\n", + " node, new_level = queue.popleft()\n", + " \n", + " if new_level != current_level:\n", + " current_level = new_level\n", + " print()\n", + " \n", + " print(f'{node.val} ', end='')\n", + " \n", + " if node.left:\n", + " queue.append((node.left, current_level + 1))\n", + " if node.right:\n", + " queue.append((node.right, current_level + 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 \n", + "2 3 \n", + "4 5 6 " + ] + } + ], + "source": [ + "node = Node(1)\n", + "node.left = Node(2)\n", + "node.right = Node(3)\n", + "node.left.left = Node(4)\n", + "node.right.left = Node(5)\n", + "node.right.right = Node(6)\n", + "levelOrderPrint(node)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb b/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb index b3aa23dd..c1c7a7a4 100644 --- a/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb +++ b/Trees/Trees Interview Problems/Trim a Binary Search Tree .ipynb @@ -26,18 +26,23 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "def trimBST(tree,minVal,maxVal):\n", + "def trimBST(tree, minVal, maxVal):\n", + " if not tree:\n", + " return None\n", " \n", - " print tree.left # LeftChild\n", - " print tree.right # Right Child\n", - " print tree.val # Node's value\n", + " tree.left = trimBST(tree.left, minVal, maxVal)\n", + " tree.right = trimBST(tree.right, minVal, maxVal)\n", " \n", - " pass\n", + " if minVal <= tree.val <= maxVal:\n", + " return tree\n", + " if treeVal < minVal:\n", + " return tree.right\n", + " if treeVal > maxVal:\n", + " return tree.left\n", + " \n", "\n", "# Use tree.left , tree.right , and tree.val as your methods to call" ] @@ -54,23 +59,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.11" + "pygments_lexer": "ipython3", + "version": "3.8.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }