Hi there!
I am very new to coding and I could really use some help with an assignment.
The goal is to write a function that accepts a list x and a integer n (as argument) and returns a new list r, where each elements are the average of the original and the surrounding 2n elements.
In addition: The interval 2n should never count beyond the list.
For example:
If the original list (argument x) is [1, 2, 6, 4, 5, 0, 1, 2] and n=1, the new list r should be
[1.5, 3.0, 4.0, 5.0, 3.0, 2.0, 1.0, 1.5]. As the iteration hits the first element in r, only(!) the average of that and the second should be calculated ((1+2)/2 = 1.5).
The code i’ve written so far is:
def smooth_b(x, n):
r=
for i in range(len(x)):
r.append(sum(x[i-n:i+n+1])/(2*n+1))
which only works for the elements were the interval stays between the borders in the original list x. The result I get so far is:
[0.0, 3.0, 4.0, 5.0, 3.0, 2.0, 1.0, 1.0].
This is, as I said, an assignment for my course, so I am not asking for it to be solved completely, but I am utterly stuck and could really use some guidance.
Thank you so much in advance!
Best regards,
Elias