Skip to content
Snippets Groups Projects
Commit ffd634dd authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
def get_maximum_prefix_sum(values):
if len(values) > 0:
candidate = get_maximum_prefix_sum(values[:-1])
total = sum(values) # requires a loop, which makes our recursion slow!
if candidate > total:
return candidate
return total
return 0
print(get_maximum_prefix_sum([5, -4, 6]))
def helper(values):
if len(values) > 0:
candidate, partial_total = helper(values[:-1])
total = partial_total + values[-1]
if candidate > total:
return candidate, total
return total, total
return 0, 0
def get_maximum_prefix_sum(values):
result, _ = helper(values)
return result
print(get_maximum_prefix_sum([5, -4, 6]))
\ No newline at end of file
def is_palindrome(text):
return is_palindrome_helper(text, 0, len(text)-1)
def is_palindrome_helper(text, start, end):
if start >= end:
return True
first = text[start].lower()
last = text[end].lower()
if first.isalpha() and last.isalpha():
if first == last:
return is_palindrome_helper(text, start+1, end-1)
else:
return False
elif not last.isalpha():
return is_palindrome_helper(text, start, end-1)
else:
return is_palindrome_helper(text, start+1, end)
print(is_palindrome('Madam, I\'m Adam'))
print(is_palindrome('Go hang a salami, I\'m a lasagna hog'))
print(is_palindrome('A man, a plan, a canal–Panama!'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment