-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sum.py
More file actions
64 lines (46 loc) · 1.19 KB
/
array_sum.py
File metadata and controls
64 lines (46 loc) · 1.19 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## Find the largest sum of consecutive nums in a give array
##
import pprint
l1=[1,2,-1,0,4,-3,4,0,-6,10]
#
# solution 1
#
# create a list of list
# add pos[0]+ pos[1] and save in the d1[0][0]
# add pos[0]+post[1]+pos[2] and save in d1[0][1]
# ........ add pos[1]+pos[2] and save in d1[1][0] .. and go on
d1=list()
sum1=0
pp = pprint.PrettyPrinter(indent=4)
for i in range(len(l1)):
if i+1 < len(l1):
d1.append(list())
for j in range(i+1,len(l1)):
d1[i].append(sum(l1[i:j+1]))
pp.pprint(d1)
#
# solution 2
# optimized here
# add pos[0]+pos[1] and save in d1[0][0]
# add d1[0][0] + pos[2] and save in d1[0][1]
# add d1[0][1] + pos[3] and save in d1[0][2]
# then when d1[0] is fully done, exit this loop
d1=list()
d1.append(list())
for i in range(1,len(l1)):
if len(d1[-1]) > 0:
d1[-1].append(d1[-1][-1]+l1[i])
else:
d1[-1].append(l1[0]+l1[i])
# d1[1][0] = d1[0][1] - l1[0]
# d1[1][1] = d1[0][2] - l1[0] .. until d1[1] is done
#
# d1[2][0] = d1[1][1] - l1[1] ....etc
for j in l1:
#get the last index
index = len(d1)-1
for k in d1[index][1:]:
if index == len(d1)-1:
d1.append(list())
d1[-1].append(k-j)
pp.pprint(d1)