diff --git a/README.md b/README.md index 4e300ad..493b148 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/recursion_dynamic/longest_common_substring/__init__.py b/recursion_dynamic/longest_common_subsequence/__init__.py similarity index 100% rename from recursion_dynamic/longest_common_substring/__init__.py rename to recursion_dynamic/longest_common_subsequence/__init__.py diff --git a/recursion_dynamic/longest_common_substring/longest_common_substr_challenge.ipynb b/recursion_dynamic/longest_common_subsequence/longest_common_subseq_challenge.ipynb similarity index 86% rename from recursion_dynamic/longest_common_substring/longest_common_substr_challenge.ipynb rename to recursion_dynamic/longest_common_subsequence/longest_common_subseq_challenge.ipynb index 77d4218..531bdd4 100644 --- a/recursion_dynamic/longest_common_substring/longest_common_substr_challenge.ipynb +++ b/recursion_dynamic/longest_common_subsequence/longest_common_subseq_challenge.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", diff --git a/recursion_dynamic/longest_common_substring/longest_common_substr_solution.ipynb b/recursion_dynamic/longest_common_subsequence/longest_common_subseq_solution.ipynb similarity index 88% rename from recursion_dynamic/longest_common_substring/longest_common_substr_solution.ipynb rename to recursion_dynamic/longest_common_subsequence/longest_common_subseq_solution.ipynb index 4c90b2b..77d186f 100644 --- a/recursion_dynamic/longest_common_substring/longest_common_substr_solution.ipynb +++ b/recursion_dynamic/longest_common_subsequence/longest_common_subseq_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", @@ -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" ] } ], diff --git a/recursion_dynamic/longest_common_subsequence/test_longest_common_subseq.py b/recursion_dynamic/longest_common_subsequence/test_longest_common_subseq.py new file mode 100644 index 0000000..6aa2e25 --- /dev/null +++ b/recursion_dynamic/longest_common_subsequence/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() diff --git a/recursion_dynamic/longest_common_substring/test_longest_common_substr.py b/recursion_dynamic/longest_common_substring/test_longest_common_substr.py deleted file mode 100644 index 8af8fe7..0000000 --- a/recursion_dynamic/longest_common_substring/test_longest_common_substr.py +++ /dev/null @@ -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() \ No newline at end of file