From a7326f84aa5f3076d1b043baef5f5a0fdd65485b Mon Sep 17 00:00:00 2001 From: Tom Stepp Date: Sat, 6 Jul 2019 12:04:18 -0700 Subject: [PATCH 1/2] Working on array problems --- .../Anagram Check .ipynb | 16 +- .../Anagram Check - SOLUTION.ipynb | 2 +- .../Anagram Check -checkpoint.ipynb | 148 ++++++------------ .../Array Pair Sum -checkpoint.ipynb | 102 +++++++++--- .../Anagram Check .ipynb | 148 ++++++------------ .../Array Pair Sum .ipynb | 105 ++++++++++--- .../Find the Missing Element .ipynb | 85 ++++++---- .../Largest Continuous Sum .ipynb | 53 ++++--- .../Sentence Reversal.ipynb | 47 +++--- 9 files changed, 370 insertions(+), 336 deletions(-) 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..6d21f073 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 @@ -132,14 +132,18 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 1, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" + "ename": "NameError", + "evalue": "name 'anagram' 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[1;32m 16\u001b[0m \u001b[0;31m# Run Tests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAnagramTest\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[0;32m---> 18\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0managram\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 'anagram' is not defined" ] } ], @@ -205,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.2" + "version": "3.7.3" } }, "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..bcd15184 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.7.3" } }, "nbformat": 4, diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Anagram Check -checkpoint.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Anagram Check -checkpoint.ipynb index fd6f0824..97111bd0 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Anagram Check -checkpoint.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Anagram Check -checkpoint.ipynb @@ -25,81 +25,42 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [], "source": [ - "def anagram(s1,s2):\n", + "def anagramFirstTry(s1,s2):\n", " \n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('dog','god')" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('clint eastwood','old west action')" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('aa','bb')" + " for letter in s1:\n", + " if (letter in s2) and (not letter.isspace()):\n", + " idx = s2.index(letter)\n", + " s2 = s2[:idx] + s2[idx+1:]\n", + " elif letter.isspace():\n", + " pass\n", + " else:\n", + " return False\n", + " \n", + " return True\n", + "\n", + "def anagramImproved(s1, s2):\n", + " s1 = s1.replace(' ', '').lower()\n", + " s2 = s2.replace(' ', '').lower()\n", + " \n", + " for letter in s1:\n", + " if (letter in s2) and (not letter.isspace()):\n", + " idx = s2.index(letter)\n", + " s2 = s2[:idx] + s2[idx+1:]\n", + " elif letter.isspace():\n", + " pass\n", + " else:\n", + " return False\n", + " \n", + " return True\n", + " \n", + "def anagramPythonTricks(s1, s2):\n", + " s1 = s1.replace(' ', '').lower()\n", + " s2 = s2.replace(' ', '').lower()\n", + " return sorted(s1) == sorted(s2)" ] }, { @@ -113,14 +74,14 @@ { "cell_type": "code", "execution_count": 44, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -139,30 +100,13 @@ " 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", - "t.test(anagram)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] - } - ], - "source": [ - "t.test(anagram2)" + "t.test(anagramFirstTry)\n", + "t.test(anagramImproved)\n", + "t.test(anagramPythonTricks)" ] }, { @@ -175,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.11" + "pygments_lexer": "ipython3", + "version": "3.7.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Array Pair Sum -checkpoint.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Array Pair Sum -checkpoint.ipynb index 50ee6bfa..ffc6388b 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Array Pair Sum -checkpoint.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/Array Pair Sum -checkpoint.ipynb @@ -28,37 +28,76 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 85, + "metadata": {}, "outputs": [], "source": [ "def pair_sum(arr,k):\n", + " if len(arr) < 2:\n", + " print('Size of array too small!')\n", + " return 0\n", + " \n", + " pairs = set()\n", + " \n", + " for idx, num1 in enumerate(arr): \n", + " for num2 in list(arr[idx+1:]):\n", + " if num1+num2 == k:\n", + " pairA = (num1, num2)\n", + " pairB = (num2, num1)\n", + " if pairA not in pairs and pairB not in pairs:\n", + " pairs.add(pairA)\n", + " \n", + " return len(pairs)\n", + " \n", " \n", - " pass" + "def pair_sum_sol(arr, k):\n", + " if len(arr) < 2:\n", + " print('Size of array too small!')\n", + " return 0\n", + " \n", + " seen = set()\n", + " output = set()\n", + " \n", + " for num in arr:\n", + " target = k-num\n", + " if target not in seen:\n", + " seen.add(num)\n", + " else:\n", + " new_val = (min(num, target), max(num, target))\n", + " output.add(new_val)\n", + " \n", + " print('Printing results below!')\n", + " print('\\n'.join(map(str, list(output))))\n", + " return len(output)" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 86, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Size of array too small!\n", + "Size of array too small!\n" + ] + }, { "data": { "text/plain": [ - "2" + "0" ] }, - "execution_count": 14, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "pair_sum([1,3,2,2],4)" + "pair_sum([2],3)\n", + "pair_sum_sol([2],3)" ] }, { @@ -70,15 +109,23 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 87, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", + "(4, 6)\n", + "(5, 5)\n", + "(2, 8)\n", + "(-1, 11)\n", + "(1, 9)\n", + "(3, 7)\n", + "(1, 2)\n", + "(1, 3)\n", + "(2, 2)\n", "ALL TEST CASES PASSED\n" ] } @@ -95,12 +142,12 @@ " 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", "t.test(pair_sum)\n", - " " + "t.test(pair_sum_sol)" ] }, { @@ -109,27 +156,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.7.3" } }, "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..97111bd0 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,81 +25,42 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": false - }, + "execution_count": 43, + "metadata": {}, "outputs": [], "source": [ - "def anagram(s1,s2):\n", + "def anagramFirstTry(s1,s2):\n", " \n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('dog','god')" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('clint eastwood','old west action')" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "anagram('aa','bb')" + " for letter in s1:\n", + " if (letter in s2) and (not letter.isspace()):\n", + " idx = s2.index(letter)\n", + " s2 = s2[:idx] + s2[idx+1:]\n", + " elif letter.isspace():\n", + " pass\n", + " else:\n", + " return False\n", + " \n", + " return True\n", + "\n", + "def anagramImproved(s1, s2):\n", + " s1 = s1.replace(' ', '').lower()\n", + " s2 = s2.replace(' ', '').lower()\n", + " \n", + " for letter in s1:\n", + " if (letter in s2) and (not letter.isspace()):\n", + " idx = s2.index(letter)\n", + " s2 = s2[:idx] + s2[idx+1:]\n", + " elif letter.isspace():\n", + " pass\n", + " else:\n", + " return False\n", + " \n", + " return True\n", + " \n", + "def anagramPythonTricks(s1, s2):\n", + " s1 = s1.replace(' ', '').lower()\n", + " s2 = s2.replace(' ', '').lower()\n", + " return sorted(s1) == sorted(s2)" ] }, { @@ -113,14 +74,14 @@ { "cell_type": "code", "execution_count": 44, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -139,30 +100,13 @@ " 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", - "t.test(anagram)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ALL TEST CASES PASSED\n" - ] - } - ], - "source": [ - "t.test(anagram2)" + "t.test(anagramFirstTry)\n", + "t.test(anagramImproved)\n", + "t.test(anagramPythonTricks)" ] }, { @@ -175,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.11" + "pygments_lexer": "ipython3", + "version": "3.7.3" } }, "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..82391a87 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,37 +28,76 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 90, + "metadata": {}, "outputs": [], "source": [ "def pair_sum(arr,k):\n", + " if len(arr) < 2:\n", + " print('Size of array too small!')\n", + " return 0\n", + " \n", + " pairs = set()\n", + " \n", + " for idx, num1 in enumerate(arr): \n", + " for num2 in list(arr[idx+1:]):\n", + " if num1+num2 == k:\n", + " pairA = (num1, num2)\n", + " pairB = (num2, num1)\n", + " if pairA not in pairs and pairB not in pairs:\n", + " pairs.add(pairA)\n", + " \n", + " return len(pairs)\n", + " \n", " \n", - " pass" + "def pair_sum_sol(arr, k):\n", + " if len(arr) < 2:\n", + " print('Size of array too small!')\n", + " return 0\n", + " \n", + " seen = set()\n", + " output = set()\n", + " \n", + " for num in arr:\n", + " target = k-num\n", + " if target not in seen:\n", + " seen.add(num)\n", + " else:\n", + " new_val = (min(num, target), max(num, target))\n", + " output.add(new_val)\n", + " \n", + " print('Printing results below!')\n", + " print('\\n'.join(map(str, list(output))))\n", + " return len(output)" ] }, { "cell_type": "code", - "execution_count": 14, - "metadata": { - "collapsed": false - }, + "execution_count": 91, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Size of array too small!\n", + "Size of array too small!\n" + ] + }, { "data": { "text/plain": [ - "2" + "0" ] }, - "execution_count": 14, + "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "pair_sum([1,3,2,2],4)" + "pair_sum([2],3)\n", + "pair_sum_sol([2],3)" ] }, { @@ -70,15 +109,26 @@ }, { "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": false - }, + "execution_count": 92, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", + "Printing results below!\n", + "(4, 6)\n", + "(5, 5)\n", + "(2, 8)\n", + "(-1, 11)\n", + "(1, 9)\n", + "(3, 7)\n", + "Printing results below!\n", + "(1, 2)\n", + "Printing results below!\n", + "(1, 3)\n", + "(2, 2)\n", "ALL TEST CASES PASSED\n" ] } @@ -95,12 +145,12 @@ " 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", "t.test(pair_sum)\n", - " " + "t.test(pair_sum_sol)" ] }, { @@ -109,27 +159,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.7.3" } }, "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..a42ff771 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,50 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": 22, + "metadata": {}, "outputs": [], "source": [ "def finder(arr1,arr2):\n", + " for num in arr1:\n", + " if num in arr2:\n", + " arr2.remove(num)\n", + " else:\n", + " return num\n", + " return arr1[0]\n", + " \n", + "def finder_sol(arr1, arr2):\n", + " arr1.sort()\n", + " arr2.sort()\n", + " \n", + " for num1, num2 in zip(arr1, arr2):\n", + " if num1 != num2:\n", + " return num1\n", + " \n", + " return arr1[-1]\n", + "\n", + "import collections\n", + "\n", + "def finder_sol2(arr1, arr2):\n", + " d = collections.defaultdict(int)\n", + " \n", + " for num in arr2:\n", + " d[num] += 1\n", + " \n", + " for num in arr1:\n", + " if d[num] == 0:\n", + " return num\n", + " else:\n", + " d[num] -= 1\n", + " \n", " \n", - " pass" + " " ] }, { "cell_type": "code", - "execution_count": 46, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ { "data": { @@ -51,7 +78,7 @@ "5" ] }, - "execution_count": 46, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -64,10 +91,8 @@ }, { "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "data": { @@ -75,7 +100,7 @@ "5" ] }, - "execution_count": 47, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -89,9 +114,7 @@ }, { "cell_type": "markdown", - "metadata": { - "collapsed": false - }, + "metadata": {}, "source": [ "_____" ] @@ -105,15 +128,15 @@ }, { "cell_type": "code", - "execution_count": 43, - "metadata": { - "collapsed": false - }, + "execution_count": 23, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -130,11 +153,13 @@ " 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", - "t.test(finder)" + "t.test(finder)\n", + "t.test(finder_sol)\n", + "t.test(finder_sol2)" ] }, { @@ -147,23 +172,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.7.3" } }, "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..8fd6f05a 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,30 +16,40 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ "def large_cont_sum(arr): \n", - " pass" + " large_sum = curr_sum = arr[0]\n", + " \n", + " for num in arr[1:]:\n", + " curr_sum = max(curr_sum+num, num)\n", + " large_sum = max(curr_sum, large_sum)\n", + " \n", + " print('Largest sum: {}'.format(large_sum))\n", + " return large_sum" ] }, { "cell_type": "code", - "execution_count": 25, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Largest sum: 29\n" + ] + }, { "data": { "text/plain": [ "29" ] }, - "execution_count": 25, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -65,15 +75,16 @@ }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Largest sum: 9\n", + "Largest sum: 29\n", + "Largest sum: 1\n", "ALL TEST CASES PASSED\n" ] } @@ -86,7 +97,7 @@ " 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,1]),1)\n", - " print 'ALL TEST CASES PASSED'\n", + " print('ALL TEST CASES PASSED')\n", " \n", "#Run Test\n", "t = LargeContTest()\n", @@ -103,23 +114,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.7.3" } }, "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..51fcb47d 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,21 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ "def rev_word(s):\n", - " pass" + " words = s.split()\n", + " rwords = list(reversed(words))\n", + " rev_s = ' '.join(rwords)\n", + " return rev_s" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, + "execution_count": 17, + "metadata": {}, "outputs": [ { "data": { @@ -56,7 +55,7 @@ "'go? to ready you are John, Hi'" ] }, - "execution_count": 4, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -67,10 +66,8 @@ }, { "cell_type": "code", - "execution_count": 20, - "metadata": { - "collapsed": false - }, + "execution_count": 18, + "metadata": {}, "outputs": [ { "data": { @@ -78,7 +75,7 @@ "'before space'" ] }, - "execution_count": 20, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -103,10 +100,8 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": false - }, + "execution_count": 19, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -130,7 +125,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", @@ -147,23 +142,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.7.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } From ff6e8fc1b1bd20aa61f9a0d3b866f4b2670611ec Mon Sep 17 00:00:00 2001 From: Tom Stepp Date: Sat, 6 Jul 2019 19:24:33 -0700 Subject: [PATCH 2/2] Complete array sequence problems --- .../String Compression -checkpoint.ipynb | 115 ++++++++++++++---- ...ique Characters in String-checkpoint.ipynb | 43 ++++--- .../String Compression .ipynb | 115 ++++++++++++++---- .../Unique Characters in String.ipynb | 43 ++++--- 4 files changed, 238 insertions(+), 78 deletions(-) diff --git a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/String Compression -checkpoint.ipynb b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/String Compression -checkpoint.ipynb index e8c7d5f4..c6db0672 100644 --- a/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/String Compression -checkpoint.ipynb +++ b/Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/.ipynb_checkpoints/String Compression -checkpoint.ipynb @@ -23,22 +23,66 @@ }, { "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "def compress(s):\n", - " pass" + " if len(s) == 0:\n", + " return '' # empty string\n", + " \n", + " # initial variables\n", + " curr_letter = s[0]\n", + " count = 1\n", + " result = ''\n", + " \n", + " # iterate over letters\n", + " for idx, letter in enumerate(s[1:]):\n", + " if letter == curr_letter :\n", + " count += 1\n", + " \n", + " if letter != curr_letter or idx == len(s[1:])-1:\n", + " # update result\n", + " result += curr_letter\n", + " result += str(count)\n", + " \n", + " # reset curr_letter and count\n", + " curr_letter = letter\n", + " count = 1\n", + " \n", + " return result\n", + "\n", + "def compress_sol(s):\n", + " \n", + " r = \"\"\n", + " l = len(s)\n", + " \n", + " if l == 0:\n", + " return \"\"\n", + " elif l == 1:\n", + " return s+\"1\"\n", + " \n", + " last = s[0]\n", + " cnt = 1\n", + " i = 1\n", + " \n", + " while i < l:\n", + " if s[i] == s[i-1]:\n", + " cnt += 1\n", + " else:\n", + " r += s[i-1] + str(cnt)\n", + " cnt = 1\n", + " \n", + " i += 1\n", + " r += s[i-1] + str(cnt)\n", + " \n", + " return r" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { @@ -46,7 +90,7 @@ "'A5B4C4'" ] }, - "execution_count": 66, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -55,6 +99,26 @@ "compress('AAAAABBBBCCCC')" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A5B4C4'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compress_sol('AAAAABBBBCCCC')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -64,15 +128,14 @@ }, { "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -89,11 +152,12 @@ " 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", - "t.test(compress)" + "t.test(compress)\n", + "t.test(compress_sol)" ] }, { @@ -102,27 +166,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.7.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } 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..ef90a164 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,23 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "def uni_char(s):\n", - " pass" + " unique = set(s)\n", + " return len(unique) == len(s)\n", + "\n", + "def uni_char_sol(s):\n", + " chars = set()\n", + " \n", + " for letter in s:\n", + " if letter in chars:\n", + " return False\n", + " else:\n", + " chars.add(letter)\n", + " return True" ] }, { @@ -39,15 +48,14 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -65,11 +73,12 @@ " 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", - "t.test(uni_char)" + "t.test(uni_char)\n", + "t.test(uni_char_sol)" ] }, { @@ -82,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.10" + "pygments_lexer": "ipython3", + "version": "3.7.3" } }, "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..c6db0672 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,22 +23,66 @@ }, { "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": true - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "def compress(s):\n", - " pass" + " if len(s) == 0:\n", + " return '' # empty string\n", + " \n", + " # initial variables\n", + " curr_letter = s[0]\n", + " count = 1\n", + " result = ''\n", + " \n", + " # iterate over letters\n", + " for idx, letter in enumerate(s[1:]):\n", + " if letter == curr_letter :\n", + " count += 1\n", + " \n", + " if letter != curr_letter or idx == len(s[1:])-1:\n", + " # update result\n", + " result += curr_letter\n", + " result += str(count)\n", + " \n", + " # reset curr_letter and count\n", + " curr_letter = letter\n", + " count = 1\n", + " \n", + " return result\n", + "\n", + "def compress_sol(s):\n", + " \n", + " r = \"\"\n", + " l = len(s)\n", + " \n", + " if l == 0:\n", + " return \"\"\n", + " elif l == 1:\n", + " return s+\"1\"\n", + " \n", + " last = s[0]\n", + " cnt = 1\n", + " i = 1\n", + " \n", + " while i < l:\n", + " if s[i] == s[i-1]:\n", + " cnt += 1\n", + " else:\n", + " r += s[i-1] + str(cnt)\n", + " cnt = 1\n", + " \n", + " i += 1\n", + " r += s[i-1] + str(cnt)\n", + " \n", + " return r" ] }, { "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { @@ -46,7 +90,7 @@ "'A5B4C4'" ] }, - "execution_count": 66, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -55,6 +99,26 @@ "compress('AAAAABBBBCCCC')" ] }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'A5B4C4'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "compress_sol('AAAAABBBBCCCC')" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -64,15 +128,14 @@ }, { "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -89,11 +152,12 @@ " 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", - "t.test(compress)" + "t.test(compress)\n", + "t.test(compress_sol)" ] }, { @@ -102,27 +166,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.7.3" } }, "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..ef90a164 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,23 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "def uni_char(s):\n", - " pass" + " unique = set(s)\n", + " return len(unique) == len(s)\n", + "\n", + "def uni_char_sol(s):\n", + " chars = set()\n", + " \n", + " for letter in s:\n", + " if letter in chars:\n", + " return False\n", + " else:\n", + " chars.add(letter)\n", + " return True" ] }, { @@ -39,15 +48,14 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "ALL TEST CASES PASSED\n", "ALL TEST CASES PASSED\n" ] } @@ -65,11 +73,12 @@ " 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", - "t.test(uni_char)" + "t.test(uni_char)\n", + "t.test(uni_char_sol)" ] }, { @@ -82,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.10" + "pygments_lexer": "ipython3", + "version": "3.7.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }