-1

I would like to create a function that sums first two elements and so on. Like:

    arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    result = [3, 5, 7, 9, 11, 13, 15, 17]

I did something like that but got no results

def Consec_Sum(arr):
    result = []
    for i in range(len(arr)-1):
        result = result.append(arr[i]+arr[i+1])
    return result
aearslan
  • 156
  • 8
  • which programming language? You mean a prefix sum operation? I would like to know if there is a statistically significant correlation between questions that are not tagged with a programming language and `javascript` :D – Jan Aug 12 '19 at 14:35
  • sorry, I edited the question. I hope it is clarified – aearslan Aug 12 '19 at 14:42
  • 1
    `append` doesn't return the list. If you remove `result = ` your code should work. – mkrieger1 Aug 12 '19 at 14:44
  • i removed the result= part but still have a problem – aearslan Aug 12 '19 at 14:57

1 Answers1

0
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def Consec_Sum(arr):
    result = []
    for i in range(len(arr)-1):
        result.append(arr[i]+arr[i+1])
    print(result)

Consec_Sum(arr)

Result

[3, 5, 7, 9, 11, 13, 15, 17]

I didn't analize the code. Just removed result =.

More similar to your code:

 arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

 def Consec_Sum(arr):
     result = []
     for i in range(len(arr)-1):
         result.append(arr[i]+arr[i+1])
     return result

 print(Consec_Sum(arr))
Novik
  • 88
  • 1
  • 6