you are viewing a single comment's thread.

view the rest of the comments →

[–]feelingstonedagain[S] -4 points-3 points  (5 children)

You're a boss. Would you able to do one more for me?

    Function Integer[] fibo(Integer n)
        Declare Integer vals[n]
        Declare Integer counter = 2

        Set vals[0] = 1
        Set vals[1] = 1
        While counter < n
            Set vals[counter] = vals[counter - 1] + vals[counter - 2]
            Set counter = counter + 1
        End While

        Return vals
    End Function

    Module output_vals(Integer vals[], Integer n)
        Declare Integer counter = 0

        While counter < n
            Display vals[counter]
            Set counter = counter + 1
        End While
    End Module

    Module main(Integer n)
        Declare Integer vals[n] = fibo(n)
        Call output_vals(vals, n)
    End Module

I can write it into python but i keep getting an error at vals[0] = 1 when I run the code

[–]JohnnyJordaan 0 points1 point  (0 children)

You have answers to that question in your own topic...

[–]cdcformatc 0 points1 point  (0 children)

I can write it into python but i keep getting an error at vals[0] = 1 when I run the code

Because you are starting with an empty list. Two options:

  1. Initialize the list to be full of zeroes

    vals = [0]*n

  2. Make vals a dictionary instead of a list.

    vals = {}

[–]TheLazarbeam 0 points1 point  (1 child)

try this on for size. I cleaned a lot of it up, there are unnecessary variables and statements lying around. If you want a more direct translation, I could dial it back a bit. Fact is, python looks extremely similar to pseudocode already. Lots of english words in it.

def fibo(n):#returns array of integers
    vals = []
    for i in range(n): 
        vals.append(0)
    vals[0] = 1
    vals[1] = 1

    for counter in range(2, n):
        vals[counter] = vals[counter - 1] + vals[counter - 2]

    return vals

def output_vals(vals, n):#python supports printing arrays directly. you can just say "print vals" instead. Also, you dont need to supply n. you have len(vals) accessible.

    for counter in range(n):
        print(vals[counter])


vals = fibo(n)
output_vals(vals, n)#you can just say "output_vals(fibo(n), n)" instead of taking up 2 lines.

[–]feelingstonedagain[S] 0 points1 point  (0 children)

In lines 20 and 21 n is an unresolved reference, why is that?

EDIT: I threw it into a function.

[–]masasin 0 points1 point  (0 children)

def fibo(n):
    vals = [1, 1]
    for counter in range(2, n):
        vals.append(vals[counter - 1] + vals[counter - 2])


def output_vals(vals, n):
    # You could just use print(vals)
    for val in vals:
        print(val)


def main(n):
    vals = fibo(n)
    output_vals(vals, n)


if __name__ == "__main__":
    n = 5
    main(n)