1

I'm trying to solve a math problem but I would like to make some informatic tests before to understand what happend (and maybe find the solution with my program), my problem is :

Consider the list consisting of the first n natural numbers without zero, ie from 1 to n. We define the transformation "moving average" on the list of n elements by adding the average of all the terms at the end of the list and eliminating the first term at the beginning of the list. For example, if n = 4, we have: (1,2,3,4) -> (2,3,4,2.5) By iterating this process many times, one can observe a phenomenon of standardization and that all elements of the list tend to a common value when the number of iterations tends to + infinity. It asks for the value of n for this limit is 254859658745.

Well, i'm trying to program the function "moving average" like this :

def moving_average(liste,t):
k=0
S=0
m=0
c=0
n=len(liste)
while c<t:
    while k<n:
        S+=int(liste[k])
        k+=1
        m=S/n
    liste.pop(0)
    liste.append(m)
    c+=1        
return m

My program works but don't answer what I want, if I take liste=[1,2,3] (for example) for all t>1 the answer is always the same... but I don't understand why.

Can you help me please ?

5
  • Just noting that this is the way to ask a homework question. Well done, I wish more were like it. Commented May 8, 2015 at 2:33
  • This is not an homework question, I find this problem on the net ;) And I don't understand the second part of your sentence..why "were" and not "will be" ? Commented May 8, 2015 at 2:40
  • Hah - it is in the "class" of "homework question", which are so often asked badly. I wish both "were" and "will be" :). I'm looking at your question... Commented May 8, 2015 at 2:41
  • Sorry but i'm french student and my english is not fluent ^^ Commented May 8, 2015 at 2:43
  • You have excellent english to ask a question like this: no problem, and it is a valid question about the tense of my statement. In English, we sometimes use this tense, "I wish these things were ...", to mean that "I wish that they always are that way - not in the past or in the future specifically, but always. Like "I wish that it were possible to fly". I don't mean that it was possible in the past, nor that it becomes possible in the future, but subtly different: I wish that it always was and will be. Commented May 8, 2015 at 2:47

1 Answer 1

1

In the interests of helping you move forwards, here is the first part of an answer. The way to debug this is like this:

def moving_average(liste,t):
    k=0
    S=0
    m=0
    c=0
    n=len(liste)
    while c<t:
        print("At c: ", c)
        k=0
        while k<n:
            print(" At k: ", k) 
            S+=int(liste[k])
            k+=1
            m=S/n
            print("  .. new S", S)
            print("  .. new k", k)
            print("  .. new m", m)

        liste.pop(0)
        liste.append(m)
        print(" liste: ", liste)
        c+=1        
    return m

test_list = [1,2,3]

test_t = 4

print("Result:", moving_average(test_list, test_t))

Then look at each result until you find one that isn't what you expect

Since you know better than I do what you expect at each step, you might find the underlying issue quicker than I can by doing this :)

Update:

One "obvious" reason why it's not working is because you're not resetting k each time around the c loop.

If you look at the output before fixing, you will see that the "at K" messages only come out once, the first time through.

I've updated the code above to fix this, and I think it does something like you are expecting. I'm not sure why you are taking taking the int() of liste[k], but that is a separate issue.

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

6 Comments

Your program answer an error on my computer is it normal ?
It works for me, but I have python 2.7. Maybe the print statement is not valid syntax for python 3? I will check
Yes, you need parens in python 3
I add the parenthesis but for the last one I have invalid syntax ^^
Hopefully the update I posted works... I need to get python3 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.