4

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

2
  • Do you want all permutations of length 4 in list : for x in list(permutations(tab, 4)):print (sum(x)) Commented Feb 9, 2017 at 9:11
  • can you explain more concerning the permutations? Commented Feb 9, 2017 at 9:24

6 Answers 6

5
  • 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.

Sign up to request clarification or add additional context in comments.

2 Comments

works only with integer sums. With floating point you can have loss of precision. Works here!
@Jean-FrançoisFabre: Interesting point. I'm not sure how big this loss could be. I don't think it could always drift away in the same direction, since any +x will be compensated by -x 4 steps later.
3

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] 

Comments

2

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)

Comments

1

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]

1 Comment

Please can you explain more your idea and put the complete code?@Fallen
1

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) 

Comments

1

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.