Skip to content

Instantly share code, notes, and snippets.

@Nashtic
Created March 7, 2020 13:44
Show Gist options
  • Save Nashtic/5d2e44bf57de1e819bb493ce9778f5ae to your computer and use it in GitHub Desktop.
Save Nashtic/5d2e44bf57de1e819bb493ce9778f5ae to your computer and use it in GitHub Desktop.
liste_1=[4,-3,5,-2,-1,2,6,-2]
max_1=0
for i in range(8):
for j in range(i,8):
t=0
for k in range(i,j):
t=t+liste_1[k]
if max_1<t:
max_1=t
i_1,i_2=i,j
print(max_1,i_1,i_2)
list_1=[4,-3,2,1,-2,6,-2]
n=len(list_1)
maxSum=0
for i in range(n):
for j in range(i,n):
#print(i,j)
t=0
for k in range(i,j):
t=t+list_1[k]
if(t>maxSum):
maxSum=t
print(maxSum)
def my_f_1(list_1=[4,-3,5,-2,-1,2,6,-2]):
n = len(list_1)
maxSum=0
for i in range(n):
for j in range(i,n):
#print(i,j)
t=0
for k in range(i,j):
t=t+list_1[k]
if(t>maxSum):
maxSum=t
return maxSum
my_f_1([4,-3,5,-2,-1,2,6,-2,4,-3,5,-2,-1,2,6,-2])
def my_f_2(list_1=[4,-3,5,-2,-1,2,6,-2]):
n=len(list_1)
maxSum=0
for i in range(n):
t=0
for j in range(i,n):
#print(i,j)
t=t+list_1[k]
if(t>maxSum):
maxSum=t
return maxSum
my_f_2([4,-3,5,-2,-1,2,6,-2,4,-3,5,-2,-1,2,6,-2])
def my_f_3(list_1=[4,-3,5,-2,-1,2,6,-2]):
n=len(list_1)
if(n==1):
return list_1[0]
left_i=0
left_j=n//2
right_i=n//2
right_j=n
left_sum=my_f_3(list_1[left_i:left_j])
right_sum=my_f_3(list_1[right_i:right_j])
temp_left_sum=0
t=0
for i in range(left_j-1,left_i-1,-1):
t=t+list_1[i]
if(t>temp_left_sum):
temp_left_sum=t
temp_right_sum=0
t=0
for i in range(right_i,right_j):
t=t+list_1[i]
if(t>temp_right_sum):
temp_right_sum=t
center_sum=temp_left_sum+temp_right_sum
return max_of_three(left_sum,right_sum,center_sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment