-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxsublist.py
More file actions
34 lines (32 loc) · 895 Bytes
/
maxsublist.py
File metadata and controls
34 lines (32 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/env python
class maxsum:
def __init__(self,list):
self.list=list
def getmaxsub(self):
max= 0
x,y=0,0
sublist=[]
for i in range(0,len(self.list)+1):
for j in range(0,len(self.list)+1):
res = sum(self.list[i:j])
if res > max:
max = res
x = i
y = j
sublist=self.list[i:j]
return max,sublist
def sublist(self):
sum = self.list[0]
presum = 0
for i in self.list:
if presum < 0:
presum = i
else:
presum += i
sum = max(presum,sum)
return sum
if __name__=='__main__':
list=[-2, 1, 7, -4, 5, 2, -3, -6, 4, 3, -8, -1, 6, -7, -9, -5]
a=maxsum(list)
print('max: %s' % a.sublist())
print('max: %s' % a.getmaxsub()[0],'sublist: %s' % a.getmaxsub()[1])