Change longest common substring to subsequence (#196)

This commit is contained in:
Evan Cameron
2017-05-29 20:50:39 -04:00
committed by Donne Martin
parent 9f89a51aba
commit 00673c0bfd
6 changed files with 52 additions and 52 deletions

View File

@@ -331,7 +331,7 @@ Unit tested, fully functional implementations of the following algorithms:
| Implement fibonacci recursively, dynamically, and iteratively | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/fibonacci/fibonacci_solution.ipynb) | | Implement fibonacci recursively, dynamically, and iteratively | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/fibonacci/fibonacci_solution.ipynb) |
| Maximize items placed in a knapsack | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_01/knapsack_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_01/knapsack_solution.ipynb) | | Maximize items placed in a knapsack | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_01/knapsack_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_01/knapsack_solution.ipynb) |
| Maximize unbounded items placed in a knapsack | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_unbounded/knapsack_unbounded_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_unbounded/knapsack_unbounded_solution.ipynb) | | Maximize unbounded items placed in a knapsack | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_unbounded/knapsack_unbounded_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/knapsack_unbounded/knapsack_unbounded_solution.ipynb) |
| Find the longest common substring | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_common_substring/longest_common_substr_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_common_substring/longest_common_substr_solution.ipynb) | | Find the longest common subsequence | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_common_subsequence/longest_common_subseq_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_common_subsequence/longest_common_subseq_solution.ipynb) |
| Find the longest increasing subsequence | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_inc_subseq/longest_inc_subseq_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_inc_subseq/longest_inc_subseq_solution.ipynb) | | Find the longest increasing subsequence | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_inc_subseq/longest_inc_subseq_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/longest_inc_subseq/longest_inc_subseq_solution.ipynb) |
| Minimize the cost of matrix multiplication | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/matrix_mult/find_min_cost_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/matrix_mult/find_min_cost_solution.ipynb) | | Minimize the cost of matrix multiplication | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/matrix_mult/find_min_cost_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/matrix_mult/find_min_cost_solution.ipynb) |
| Maximize stock prices given k transactions | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/max_profit_k/max_profit_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/max_profit_k/max_profit_solution.ipynb) | | Maximize stock prices given k transactions | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/max_profit_k/max_profit_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/max_profit_k/max_profit_solution.ipynb) |

View File

@@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Problem: Given two strings, find the longest common substring.\n", "## Problem: Given two strings, find the longest common subsequence.\n",
"\n", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@@ -40,7 +40,7 @@
" * Yes\n", " * Yes\n",
"* Is this case sensitive?\n", "* Is this case sensitive?\n",
" * Yes\n", " * Yes\n",
"* Is a substring a contiguous block of chars?\n", "* Is a subsequence a non-contiguous block of chars?\n",
" * Yes\n", " * Yes\n",
"* Do we expect a string as a result?\n", "* Do we expect a string as a result?\n",
" * Yes\n", " * Yes\n",
@@ -90,7 +90,7 @@
"source": [ "source": [
"class StringCompare(object):\n", "class StringCompare(object):\n",
"\n", "\n",
" def longest_common_substr(self, str0, str1):\n", " def longest_common_subseq(self, str0, str1):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@@ -117,26 +117,26 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_longest_common_substr.py\n", "# %load test_longest_common_subseq.py\n",
"from nose.tools import assert_equal, assert_raises\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestLongestCommonSubstr(object):\n", "class TestLongestCommonSubseq(object):\n",
"\n", "\n",
" def test_longest_common_substr(self):\n", " def test_longest_common_subseq(self):\n",
" str_comp = StringCompare()\n", " str_comp = StringCompare()\n",
" assert_raises(TypeError, str_comp.longest_common_substr, None, None)\n", " assert_raises(TypeError, str_comp.longest_common_subseq, None, None)\n",
" assert_equal(str_comp.longest_common_substr('', ''), '')\n", " assert_equal(str_comp.longest_common_subseq('', ''), '')\n",
" str0 = 'ABCDEFGHIJ'\n", " str0 = 'ABCDEFGHIJ'\n",
" str1 = 'FOOBCDBCDE'\n", " str1 = 'FOOBCDBCDE'\n",
" expected = 'BCDE'\n", " expected = 'BCDE'\n",
" assert_equal(str_comp.longest_common_substr(str0, str1), expected)\n", " assert_equal(str_comp.longest_common_subseq(str0, str1), expected)\n",
" print('Success: test_longest_common_substr')\n", " print('Success: test_longest_common_subseq')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestLongestCommonSubstr()\n", " test = TestLongestCommonSubseq()\n",
" test.test_longest_common_substr()\n", " test.test_longest_common_subseq()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@@ -18,7 +18,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Problem: Given two strings, find the longest common substring.\n", "## Problem: Given two strings, find the longest common subsequence.\n",
"\n", "\n",
"* [Constraints](#Constraints)\n", "* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n", "* [Test Cases](#Test-Cases)\n",
@@ -39,7 +39,7 @@
" * Yes\n", " * Yes\n",
"* Is this case sensitive?\n", "* Is this case sensitive?\n",
" * Yes\n", " * Yes\n",
"* Is a substring a contiguous block of chars?\n", "* Is a subsequence a non-contiguous block of chars?\n",
" * Yes\n", " * Yes\n",
"* Do we expect a string as a result?\n", "* Do we expect a string as a result?\n",
" * Yes\n", " * Yes\n",
@@ -123,7 +123,7 @@
"source": [ "source": [
"class StringCompare(object):\n", "class StringCompare(object):\n",
"\n", "\n",
" def longest_common_substr(self, str0, str1):\n", " def longest_common_subseq(self, str0, str1):\n",
" if str0 is None or str1 is None:\n", " if str0 is None or str1 is None:\n",
" raise TypeError('str input cannot be None')\n", " raise TypeError('str input cannot be None')\n",
" # Add one to number of rows and cols for the dp table's\n", " # Add one to number of rows and cols for the dp table's\n",
@@ -143,7 +143,7 @@
" results = ''\n", " results = ''\n",
" i = num_rows - 1\n", " i = num_rows - 1\n",
" j = num_cols - 1\n", " j = num_cols - 1\n",
" # Walk backwards to determine the substring\n", " # Walk backwards to determine the subsequence\n",
" while T[i][j]:\n", " while T[i][j]:\n",
" if T[i][j] == T[i][j - 1]:\n", " if T[i][j] == T[i][j - 1]:\n",
" j -= 1\n", " j -= 1\n",
@@ -177,31 +177,31 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Overwriting test_longest_common_substr.py\n" "Overwriting test_longest_common_subseq.py\n"
] ]
} }
], ],
"source": [ "source": [
"%%writefile test_longest_common_substr.py\n", "%%writefile test_longest_common_subseq.py\n",
"from nose.tools import assert_equal, assert_raises\n", "from nose.tools import assert_equal, assert_raises\n",
"\n", "\n",
"\n", "\n",
"class TestLongestCommonSubstr(object):\n", "class TestLongestCommonSubseq(object):\n",
"\n", "\n",
" def test_longest_common_substr(self):\n", " def test_longest_common_subseq(self):\n",
" str_comp = StringCompare()\n", " str_comp = StringCompare()\n",
" assert_raises(TypeError, str_comp.longest_common_substr, None, None)\n", " assert_raises(TypeError, str_comp.longest_common_subseq, None, None)\n",
" assert_equal(str_comp.longest_common_substr('', ''), '')\n", " assert_equal(str_comp.longest_common_subseq('', ''), '')\n",
" str0 = 'ABCDEFGHIJ'\n", " str0 = 'ABCDEFGHIJ'\n",
" str1 = 'FOOBCDBCDE'\n", " str1 = 'FOOBCDBCDE'\n",
" expected = 'BCDE'\n", " expected = 'BCDE'\n",
" assert_equal(str_comp.longest_common_substr(str0, str1), expected)\n", " assert_equal(str_comp.longest_common_subseq(str0, str1), expected)\n",
" print('Success: test_longest_common_substr')\n", " print('Success: test_longest_common_subseq')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestLongestCommonSubstr()\n", " test = TestLongestCommonSubseq()\n",
" test.test_longest_common_substr()\n", " test.test_longest_common_subseq()\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@@ -219,12 +219,12 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_longest_common_substr\n" "Success: test_longest_common_subseq\n"
] ]
} }
], ],
"source": [ "source": [
"%run -i test_longest_common_substr.py" "%run -i test_longest_common_subseq.py"
] ]
} }
], ],

View File

@@ -0,0 +1,23 @@
from nose.tools import assert_equal, assert_raises
class TestLongestCommonSubseq(object):
def test_longest_common_subseq(self):
str_comp = StringCompare()
assert_raises(TypeError, str_comp.longest_common_subseq, None, None)
assert_equal(str_comp.longest_common_subseq('', ''), '')
str0 = 'ABCDEFGHIJ'
str1 = 'FOOBCDBCDE'
expected = 'BCDE'
assert_equal(str_comp.longest_common_subseq(str0, str1), expected)
print('Success: test_longest_common_subseq')
def main():
test = TestLongestCommonSubseq()
test.test_longest_common_subseq()
if __name__ == '__main__':
main()

View File

@@ -1,23 +0,0 @@
from nose.tools import assert_equal, assert_raises
class TestLongestCommonSubstr(object):
def test_longest_common_substr(self):
str_comp = StringCompare()
assert_raises(TypeError, str_comp.longest_common_substr, None, None)
assert_equal(str_comp.longest_common_substr('', ''), '')
str0 = 'ABCDEFGHIJ'
str1 = 'FOOBCDBCDE'
expected = 'BCDE'
assert_equal(str_comp.longest_common_substr(str0, str1), expected)
print('Success: test_longest_common_substr')
def main():
test = TestLongestCommonSubstr()
test.test_longest_common_substr()
if __name__ == '__main__':
main()