a bug in the range () function?

0

I've been writing a function for a class that takes a dictionary and converts it to a string to represent an array.

def __str__(self):

    grid_str = ['' for _ in range(self.heigth)]

    for y in range(self.heigth):

        for x in range(self.heigth):

            grid_str[y] = grid_str[y] + self.st(self.grid[y, x])

    string = ""
    for line in grid_str:
        string = string + line + "\n"
    return string

But it gives the following error:

Traceback (most recent call last):
  File "C:/Users/Jean/Dropbox/game of life 2/core.py", line 51, in <module>
    print(g.__str__())
  File "C:/Users/Jean/Dropbox/game of life 2/core.py", line 41, in __str__
    grid_str[y] = grid_str[y] + self.st(self.grid[y, x])
KeyError: (10, 0)

So I decided to put a print call to debug.

def __str__(self):

    grid_str = ['' for _ in range(self.heigth)]

    for y in range(self.heigth):

        for x in range(self.heigth):

            print(x, ",", y)      #<<< chamada de print
            grid_str[y] = grid_str[y] + self.st(self.grid[y, x])

    string = ""
    for line in grid_str:
        string = string + line + "\n"
    return string

And the result was as follows:
0 , 0 1 , 0 2 , 0 3 , 0 4 , 0 5 , 0 6 , 0 7 , 0 ..., ... ..., ... 43 , 9 44 , 9 45 , 9 46 , 9 47 , 9 48 , 9 49 , 9 0 , 10 # <<< ??

Considering that it was a 10x50 matrix, how so 0 , 10 ? As far as I know the range function starts counting from scratch and stops and does not return to zero, much less complete with y = 10 in this case. Well, if anyone knows what's going on, I'm grateful :).

class grid:
    grid = {}
    width = 0
    heigth = 0

    def __init__(self, width, heigth):
        self.width = width
        self.heigth = heigth
        for x in range(width):
            for y in range(heigth):
                self.grid[x, y] = 0

    def set_region(self, replaceGrid, shift_X, shift_Y):
        for key, item in list(replaceGrid.items()):

            newkey = (key[0] + shift_X, key[1], shift_Y)
            self.grid[newkey] = item

    def rand_region(self, x1, y1, x2, y2, rate):
        from random import random
        for x in range(x1, x2):
            for y in range(y1, y2):
                if random() > rate:
                    v = 0
                else:
                    v = 1

                self.grid[x, y] = v

    st = str

    def __str__(self):

        grid_str = ['' for _ in range(self.heigth)]

        for y in range(self.heigth):

            for x in range(self.width):

                print(x, ",", y)
                grid_str[y] = grid_str[y] + self.st(self.grid[y, x])

        string = ""
        for line in grid_str:
            string = string + line + "\n"
        return string


if __name__ == "__main__":
    g = grid(10, 50)
    print(g.__str__())
    
asked by anonymous 25.01.2017 / 12:47

1 answer

3

Well Jeacom, you had a subtle error, it's like this:

in this block where population self.grid in init :

for x in range(width):
    for y in range(heigth):
        self.grid[x, y] = 0

You're popular like this:

  

self.grid [x, y] = 0

where x is equal to a width unit and y is equal to a height unit, well then when you are trying to access the keys in this block you are inverting, you are trying to access it in the reverse order What did you do?

for y in range(self.heigth):
    for x in range(self.width):
        print(x, ",", y)
        grid_str[y] = grid_str[y] + self.st(self.grid[y, x])

Here the second element (which when populated was the first) is a unit of width and the same value for height:

That is, final fix:

...
for y in range(self.heigth):
    for x in range(self.width):
        print(x, ",", y)
        grid_str[y] = grid_str[y] + self.st(self.grid[x, y])
...

In the background, just reverse the order when you try to access the keys, it is:

self.grid[x, y]
    
25.01.2017 / 13:48