Skip to content

Instantly share code, notes, and snippets.

@Johnicholas
Created April 14, 2017 16:13
Show Gist options
  • Save Johnicholas/095c2f5f4aade60e695bfdbc65e19881 to your computer and use it in GitHub Desktop.
Save Johnicholas/095c2f5f4aade60e695bfdbc65e19881 to your computer and use it in GitHub Desktop.
def helper(a):
if len(a) == 0
# base case
return 0, 0
else:
# recursive case
best_prefix_total, best_span_total = helper(a[1:])
# TODO
return foo, bar
# after getting this recursion working, then we would need to add memoization
def solve(a):
best_prefix_total, best_span_total = helper(a)
return best_span_total
def brute_force(a):
return max([sum(a[i:j]) for i in range(len(a)) for j in range(len(a)) if j - i >= 0])
@given(st.lists(st.integers()))
def test_against_brute_force(self, a):
self.assertEqual(solve(a), brute_force(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment