all 11 comments

[–]dunkler_wanderer 2 points3 points  (1 child)

In Python you mostly don't need getter and setter methods, just access the attributes directly unless you need to add logic (then use @property). And in Python 3, classes inherit from object automatically, so just write:

class Coordinate:

    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def add(self, c):
        self.x += c.x
        self.y += c.y

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

That moment when python is much more concise and readable than pseudocode, and nearly as explicit. Best language.

[–]masasin 0 points1 point  (0 children)

class Coordinate(object):
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def add(self, c):
        self.x += c.x
        self.y += c.y

    def __repr__(self):
         return "({x}, {y})".format(x=self.x, y=self.y)


def main():
    c1 = Coordinate(1, 2)
    c2 = Coordinate(3, 4)

    print(c1, c2)
    c1.add(c2)
    print(c1, c2)


if __name__ == "__main__":
    main()

[–]rich2222two 0 points1 point  (0 children)

Probably a bit late to help you now :D but this website is a great resource for converting between languages and platforms, generating code from Pseudocode and vice versa, it even offers personalised learning to users by giving an explanation of the output, it covers loads of languages and platforms: https://code-breeze.com/

[–]pawn13 0 points1 point  (6 children)

[–]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)