Reputation: 11
Let x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
Use a for loop to add adjacent elements in x
.
Store each of these results in a vector, sa
.
Display both the vector x and the vector sa
.
For example, the first 3 numbers of sa
will be:
sa = [11, 9, 12, …] = [(3+8), (3+8+(-2)), (8+(-2)+6), …]
I have something like this...
x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []
for i in range (0, len(x)-1):
if i<1:
sa.append(x[0] + x[1])
elif i >= 1 & i< len(x):
sa.append(x[i-1] + x[i] + x[i+1])
if i == 0:
sa.append(x[i] + x[i+1])
print("Here is sa", sa)
but I can't get the last variable of -5 +8 to appear please help
what I end up getting is
Here is sa
, [11, 11, 9, 12, 13, 11, 12, 4, 3, 4]
but I also need the last value it should be (-5+8)= 3 so the total final answer should include the three like
[11, 11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
or even
[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Upvotes: 1
Views: 1935
Reputation: 23783
>>> x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
Create a list with the sum of the first two elements,
>>> q = [sum(x[:2])]
Iterate over the list three at a time and append the sums.
>>> for thing in zip(x,x[1:],x[2:]):
... q.append(sum(thing))
Append the sum of the last two items.
>>> q.append(sum(x[-2:]))
>>> q
[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Upvotes: 0
Reputation: 346
Your code has some mistakes:
x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []
# the last element of the loop will be x[len(x)-2], it is -5 in this case
# -5 is the last element this loop process, it adds up 1, -5 and 8
# this loop will not take 8 as the central number, so the results miss the last value
for i in range (0, len(x)-1):
if i<1:
sa.append(x[0] + x[1])
# in python, it is `and` not &, & is bitwise and.
elif i >= 1 & i< len(x):
sa.append(x[i-1] + x[i] + x[i+1])
if i == 0: # this is meaningless, i == 0 is covered in the first if branch
sa.append(x[i] + x[i+1])
print("Here is sa", sa)
Fix your code:
x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []
# fix one: loop to the last element
for i in range (0, len(x)):
if i<1:
sa.append(x[0] + x[1])
# fix two: do not include the last element in this elif branch
elif i >= 1 and i < len(x) - 1:
sa.append(x[i-1] + x[i] + x[i+1])
# fix three: process the last element.
else:
sa.append(x[i - 1] + x[i])
print("Here is sa", sa)
output:
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Upvotes: 0
Reputation: 2035
Just in case if you are looking for non comprehensive way, here it is.
for i in range(len(x)):
if i == 0:
sa.append(x[i] + x[i+1])
elif 1 <= i < len(x)-1:
sa.append(x[i-1] + x[i] + x[i+1])
else:
sa.append(x[i] + x[i-1])
print("Here is sa", sa)
Output:
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Upvotes: 1
Reputation: 147216
You can write this as a list comprehension, noting the i
th element in sa
is the sum
of the x
values from x[i-1]
to x[i+1]
. Now those indexes may overrun the boundaries of x
, so we need to use max
and min
to keep them in range:
x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = [sum(x[max(i-1, 0):min(i+2, len(x))]) for i in range(len(x))]
print(sa)
Output:
[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Upvotes: 2