Skip to content

Commit

Permalink
One of my favorite problems refactored.
Browse files Browse the repository at this point in the history
Must add my notes here too.
  • Loading branch information
skytreader committed Feb 13, 2016
1 parent bb5343d commit cbf3b05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
21 changes: 0 additions & 21 deletions algorithms/max_subseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,3 @@ def find_max_subarray(numseq, low, high):
def max_subarray(numseq):
"""Driver method. Call this not find_max_subarray"""
return find_max_subarray(numseq, 0, len(numseq) - 1)

class FunctionsTest(unittest.TestCase):

def test_find_biased_max(self):
self.assertEqual((6, 45), find_biased_max((5, 15, -30, 10, -5, 40, 10), 4, 6))

def test_max_subarray(self):
self.assertEqual((0, 1, 20), max_subarray((5, 15, -30, 10)))
# The example from the book
stocks = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7]
self.assertEqual((7, 10, 43), max_subarray(stocks))
self.assertEqual((0, 1, 10), max_subarray((7, 3)))
self.assertEqual((1, 1, 10), max_subarray((-10, 10)))
self.assertEqual((0, 0, 10), max_subarray((10, -10)))
self.assertEqual((0, 0, 10), max_subarray([10]))
self.assertEqual((1, 2, 50), max_subarray((-5, 40, 10)))
self.assertEqual((0, 0, -10), max_subarray([-10]))
self.assertEqual((3, 6, 55), max_subarray((5, 15, -30, 10, -5, 40, 10)))

if __name__ == "__main__":
unittest.main()
21 changes: 21 additions & 0 deletions algorithms/tests/max_subseq_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ..max_subseq import max_subarray, find_biased_max

import unittest

class FunctionsTest(unittest.TestCase):

def test_find_biased_max(self):
self.assertEqual((6, 45), find_biased_max((5, 15, -30, 10, -5, 40, 10), 4, 6))

def test_max_subarray(self):
self.assertEqual((0, 1, 20), max_subarray((5, 15, -30, 10)))
# The example from the book
stocks = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7]
self.assertEqual((7, 10, 43), max_subarray(stocks))
self.assertEqual((0, 1, 10), max_subarray((7, 3)))
self.assertEqual((1, 1, 10), max_subarray((-10, 10)))
self.assertEqual((0, 0, 10), max_subarray((10, -10)))
self.assertEqual((0, 0, 10), max_subarray([10]))
self.assertEqual((1, 2, 50), max_subarray((-5, 40, 10)))
self.assertEqual((0, 0, -10), max_subarray([-10]))
self.assertEqual((3, 6, 55), max_subarray((5, 15, -30, 10, -5, 40, 10)))

0 comments on commit cbf3b05

Please sign in to comment.