Created
January 29, 2019 13:56
-
-
Save neila/938d529ad31814d5f3bc5a4f26730c3c to your computer and use it in GitHub Desktop.
CS110 3.1 Preclass code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def increment_max(lst, prevmax): | |
currentmax = prevmax | |
for i in range(len(lst)): | |
if sum(lst[i:len(lst)]) > currentmax: | |
currentmax = sum(lst[i:len(lst)]) | |
return currentmax | |
###test#### | |
sho = [-2,-3,4,-1,-2,1,5,-2] #max subarray has sum 7 at sho[2:6] | |
increment_max(sho, 7) | |
sho1 = [-2,-3,4,-1,-2,1,5,-2,-1] #new item but max subarray is still 7 at sho1[2:6] | |
increment_max(sho1, 7) #the function only returns the old subarray sum because the new one doesn't change anything | |
sho2 = [-2,-3,4,-1,-2,1,5,-2,3] #new item now makes the max subarray at sho2[2:8] with sum 8 | |
increment_max(sho2, 7) #confirm that the function this time returns the new subarray sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment