mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Change longest common substring to subsequence (#196)
This commit is contained in:
parent
9f89a51aba
commit
00673c0bfd
|
@ -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) |
|
||||
| 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) |
|
||||
| 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) |
|
||||
| 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) |
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Given two strings, find the longest common substring.\n",
|
||||
"## Problem: Given two strings, find the longest common subsequence.\n",
|
||||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
|
@ -40,7 +40,7 @@
|
|||
" * Yes\n",
|
||||
"* Is this case sensitive?\n",
|
||||
" * Yes\n",
|
||||
"* Is a substring a contiguous block of chars?\n",
|
||||
"* Is a subsequence a non-contiguous block of chars?\n",
|
||||
" * Yes\n",
|
||||
"* Do we expect a string as a result?\n",
|
||||
" * Yes\n",
|
||||
|
@ -90,7 +90,7 @@
|
|||
"source": [
|
||||
"class StringCompare(object):\n",
|
||||
"\n",
|
||||
" def longest_common_substr(self, str0, str1):\n",
|
||||
" def longest_common_subseq(self, str0, str1):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
|
@ -117,26 +117,26 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_longest_common_substr.py\n",
|
||||
"# %load test_longest_common_subseq.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestLongestCommonSubstr(object):\n",
|
||||
"class TestLongestCommonSubseq(object):\n",
|
||||
"\n",
|
||||
" def test_longest_common_substr(self):\n",
|
||||
" def test_longest_common_subseq(self):\n",
|
||||
" str_comp = StringCompare()\n",
|
||||
" assert_raises(TypeError, str_comp.longest_common_substr, None, None)\n",
|
||||
" assert_equal(str_comp.longest_common_substr('', ''), '')\n",
|
||||
" assert_raises(TypeError, str_comp.longest_common_subseq, None, None)\n",
|
||||
" assert_equal(str_comp.longest_common_subseq('', ''), '')\n",
|
||||
" str0 = 'ABCDEFGHIJ'\n",
|
||||
" str1 = 'FOOBCDBCDE'\n",
|
||||
" expected = 'BCDE'\n",
|
||||
" assert_equal(str_comp.longest_common_substr(str0, str1), expected)\n",
|
||||
" print('Success: test_longest_common_substr')\n",
|
||||
" assert_equal(str_comp.longest_common_subseq(str0, str1), expected)\n",
|
||||
" print('Success: test_longest_common_subseq')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestLongestCommonSubstr()\n",
|
||||
" test.test_longest_common_substr()\n",
|
||||
" test = TestLongestCommonSubseq()\n",
|
||||
" test.test_longest_common_subseq()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
|
@ -18,7 +18,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Problem: Given two strings, find the longest common substring.\n",
|
||||
"## Problem: Given two strings, find the longest common subsequence.\n",
|
||||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
|
@ -39,7 +39,7 @@
|
|||
" * Yes\n",
|
||||
"* Is this case sensitive?\n",
|
||||
" * Yes\n",
|
||||
"* Is a substring a contiguous block of chars?\n",
|
||||
"* Is a subsequence a non-contiguous block of chars?\n",
|
||||
" * Yes\n",
|
||||
"* Do we expect a string as a result?\n",
|
||||
" * Yes\n",
|
||||
|
@ -123,7 +123,7 @@
|
|||
"source": [
|
||||
"class StringCompare(object):\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",
|
||||
" raise TypeError('str input cannot be None')\n",
|
||||
" # Add one to number of rows and cols for the dp table's\n",
|
||||
|
@ -143,7 +143,7 @@
|
|||
" results = ''\n",
|
||||
" i = num_rows - 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",
|
||||
" if T[i][j] == T[i][j - 1]:\n",
|
||||
" j -= 1\n",
|
||||
|
@ -177,31 +177,31 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting test_longest_common_substr.py\n"
|
||||
"Overwriting test_longest_common_subseq.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_longest_common_substr.py\n",
|
||||
"%%writefile test_longest_common_subseq.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestLongestCommonSubstr(object):\n",
|
||||
"class TestLongestCommonSubseq(object):\n",
|
||||
"\n",
|
||||
" def test_longest_common_substr(self):\n",
|
||||
" def test_longest_common_subseq(self):\n",
|
||||
" str_comp = StringCompare()\n",
|
||||
" assert_raises(TypeError, str_comp.longest_common_substr, None, None)\n",
|
||||
" assert_equal(str_comp.longest_common_substr('', ''), '')\n",
|
||||
" assert_raises(TypeError, str_comp.longest_common_subseq, None, None)\n",
|
||||
" assert_equal(str_comp.longest_common_subseq('', ''), '')\n",
|
||||
" str0 = 'ABCDEFGHIJ'\n",
|
||||
" str1 = 'FOOBCDBCDE'\n",
|
||||
" expected = 'BCDE'\n",
|
||||
" assert_equal(str_comp.longest_common_substr(str0, str1), expected)\n",
|
||||
" print('Success: test_longest_common_substr')\n",
|
||||
" assert_equal(str_comp.longest_common_subseq(str0, str1), expected)\n",
|
||||
" print('Success: test_longest_common_subseq')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestLongestCommonSubstr()\n",
|
||||
" test.test_longest_common_substr()\n",
|
||||
" test = TestLongestCommonSubseq()\n",
|
||||
" test.test_longest_common_subseq()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
|
@ -219,12 +219,12 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Success: test_longest_common_substr\n"
|
||||
"Success: test_longest_common_subseq\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%run -i test_longest_common_substr.py"
|
||||
"%run -i test_longest_common_subseq.py"
|
||||
]
|
||||
}
|
||||
],
|
|
@ -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()
|
|
@ -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()
|
Loading…
Reference in New Issue
Block a user