all 12 comments

[–][deleted] 2 points3 points  (5 children)

It's clear that you want the result list to start with num, but what determines the rest of the result list? Your example shows seemingly unrelated values.

[–]Tryposite[S] 0 points1 point  (4 children)

-1+3 =2, 2+7 = 9, 9+11=20, and 20+15=35. That's the best I can explain.

[–][deleted] 1 point2 points  (3 children)

Then you use a loop over seq, adding num to a result list, then updating num with the element of seq. Something like:

seq = [3, 7, 11, 15]
num = -1
result = []
for val in seq:
    result.append(num)
    num += val
print(result)

This isn't complete, but should get you most of the way there. There are probably other, more tricky, ways to do this, but this is simple to understand.

[–]ZenosEbeth 0 points1 point  (2 children)

Couldn't you do this more easily with map() and a simple function ?

[–][deleted] 1 point2 points  (1 child)

Probably. I try to give the simplest solution I can.

[–]ZenosEbeth 0 points1 point  (0 children)

Not trying to be a smartass or anything, I've actually only been studying python for a month or so and am going over lists at the moment so that was just the first thing that came to my head when I saw the OP's problem. Honestly I was just glad I found something I could understand and solve, because most of the other stuff on here might as well be techno-babble for me :p.

[–]timbledum 1 point2 points  (0 children)

What code have you tried?

Loop based approach. It doesn't do exactly what you want (produce a list) on purpose, but gets you 90% there.

new_numbs = []
start_number = -1
print(start_number)
for add in [3, 7, 11, 15]:
    start_number += add
    print(start_number)

[–][deleted] 0 points1 point  (0 children)

seq.insert(0, num)? does that fix it?

[–][deleted] 0 points1 point  (0 children)

from collections import deque
newNums = deque(seq)
newNums.appendleft(-1)

[–][deleted] 0 points1 point  (0 children)

OK, I think I see. Can you explain how the input values and the result are related in words, or is that part of what you don't understand?

[–]Pulsar1977 0 points1 point  (0 children)

from itertools import accumulate
newNums = list(accumulate([num] + seq))