-
Notifications
You must be signed in to change notification settings - Fork 0
/
codes.py
45 lines (45 loc) · 1.36 KB
/
codes.py
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
35
36
37
38
39
40
41
42
43
44
45
#Dark00infinity
def sum_list(array,max_sum):
curindlist=[]
curvallist=[]
sum=0
maxscore=0
startindex=len(array)
while (len(curvallist)>0 and curindlist[0]!=0) or len(curindlist)==0:
startindex=startindex - 1
for i in range(startindex,-1,-1):
currentvalue=array[i]
tempsum=sum+currentvalue
if tempsum==max_sum:
sum=tempsum
curindlist.append(i)
curvallist.append(currentvalue)
break
elif tempsum>max_sum:
continue
elif tempsum<max_sum:
sum=tempsum
curindlist.append(i)
curvallist.append(currentvalue)
continue
if maxscore < sum:
maxscore=sum
solutionindex=[]
solutionvalue=[]
for y in curindlist:
solutionindex.append(y)
for y in curvallist:
solutionvalue.append(y)
if maxscore==max_sum:
break
if len(curvallist)!=0:
last=curvallist.pop()
sum=sum-last
if len(curindlist)!=0:
lastindex=curindlist.pop()
startindex=lastindex
if len(curindlist)==0 and startindex==0:
break
solutionindex.reverse()
print(solutionindex)
sum_list([1,2,3,4,5],7)