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

@@ -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",

View File

@@ -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"
]
}
],

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()