Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explain: x0 = (x//3)*3
#1
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
Reply
#2
// 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.
Reply
#3
Thanks Dean,
Searched for Python symbol "//" but wasn't successful
Reply
#4
floor division
3.1.1. Numbers>Division
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020