Python Forum

Full Version: Explain: x0 = (x//3)*3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm following this youtube code for a sudoku solver using python.
In the following piece
def possible(y,x,n):
    global grid
    for i in range(0,9):
        if grid[y][i] == n:
            return False
    for i in range(0,9):
        if grid[i][x] == n:
            return False
 """ [b]don't understand the following 2 lines[/b] """ 
    x0 = (x//3)*3 
    y0 = (y//3)*3

    for i in range(0,3):
        for j in range(0,3):
            if grid[y0+i][x0+j] == n :
                return False
    return True          
Could someone explain the 2 I don't understand. I understand from the context that it's connected to checking a 3 by 3 square, but I can't find an explanation for this code.
Thanks
// integer division, so (x // 3) * 3 will round down to a multiple of 3. If x was 5, x //3 would be 1 (not 1.66), and (x // 3) * 3 is 3. It looks like this is used to force x and y to be on a 3 x 3 grid.
Thanks Dean,
Searched for Python symbol "//" but wasn't successful