alae
alae

Reputation: 130

Sum of contigous subsequences in an array

Given the following array:

tab = [80,12,14,5,70,9,26,30,8,12,16,15]

I want to compute the sum of all possible sequences of size 4 as follow:

S1=80+12+14+5=111
S2=12+14+5+70 =101
S3=14+5+70+9 =98
....

I have implmented a short program on python to do this and its not efficient:

import numpy as np


tab= np.array([80,12,14,5,70,9,26,30,8,12,16,15])
tab_size=tab.size
n=tab_size
s=0
seq_len=5
for i in range (0,n-(seq_len-1),1):
    print("index i ",i)
    for k in range(i+1,(seq_len+i),1):
            print ("index k ", k)
            tab[i]=tab[i]+tab[k]
            s=s+1
print(s)         
tab

the result is as follow :

array([111, 101,  98, 110, 135,  73,  76,  66,  51,  12,  16,  15])

I notice that each element will participate in the sum operation 4 times which is not good. do you have any efficient idea to do that?
I want to add that, the sequence size is not fixed, in this example is just 4. Thank you in advance

Upvotes: 4

Views: 194

Answers (6)

Fallen
Fallen

Reputation: 4565

Another approach would be to keep the cumulitive sum upto current index and subtract the cumulative sum 4 index back,

tab = [80,12,14,5,70,9,26,30,8,12,16,15]
cumulative_sum = [0]*(len(tab)+1)
ret = []
for i in xrange(len(tab)):
    cumulative_sum[i+1] = cumulative_sum[i] + tab[i]
    if i >= 3:
      ret.append(cumulative_sum[i+1] - cumulative_sum[i-3])

In place version(without the cumulativeSum list and storing that in tab instead),

tab = [0] + [80,12,14,5,70,9,26,30,8,12,16,15]
ret = []
for i in xrange(1, len(tab)):
    tab[i] += tab[i-1]
    if i >= 4:
      ret.append(tab[i] - tab[i-4])

It works because it keeps track of the cumulative sum upto every index. So for any index, the sum of the sequence of length n ending at that index can be found using cumulativeSum[index] - cumulativeSum[index-n]

Upvotes: 1

bobtt
bobtt

Reputation: 73

if you want to use numpy

A=np.array([80, 12, 14, 5, 70, 9, 26, 30, 8, 12, 16, 15])
print reduce(lambda x,y: x + [sum(y)], np.array(zip(A,A[1:],A[2:],A[3:])).tolist(),[])

output

[111, 101, 98, 110, 135, 73, 76, 66, 51]

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Short solution using sum and enumerate functions:

tab = [80,12,14,5,70,9,26,30,8,12,16,15]
sums = [sum(tab[i:i+4]) for i, v in enumerate(tab) if i+4 <= len(tab)]

print(sums)

The output:

[111, 101, 98, 110, 135, 73, 76, 66, 51]

The last "4-items" consequent sequence to be summed up is 8,12,16,15(gives 51)

Upvotes: 2

Rahul K P
Rahul K P

Reputation: 16081

Try this,

print [sum(item) for item in [tab[n:n+4] for n in range(0, len(tab))] if len(item) == 4]
# Result [111, 101, 98, 110, 135, 73, 76, 66, 51]

Upvotes: 3

Eric Duminil
Eric Duminil

Reputation: 54213

  • Once you calculated S1, you just need to add 70 and substract 80 to get S2.
  • Once you calculated S2, you just need to add 9 and substract 12 to get S3.
  • ...

This way, you'll avoid using each element 4 times.

Upvotes: 5

lmiguelvargasf
lmiguelvargasf

Reputation: 69695

I think you can use this approach:

tab = [80,12,14,5,70,9,26,30,8,12,16,15]

for i in range(len(tab) - 3):
    summation = sum(tab[i:i+4])
    print(summation)

Upvotes: 1

Related Questions