Last active
May 13, 2017 21:13
-
-
Save telmotrooper/d6794af42385bd24f07b49854f5ec3f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sudoku: | |
"""Sudoku validator""" | |
grid = [[0] * 9 for i in range(9)] | |
def __init__(self): | |
self.populate_grid() | |
self.print_grid() | |
self.validate_grid() | |
def populate_grid(self): | |
with open('input.txt', 'r') as file: | |
x, y = 0, 0 | |
for line in file: | |
for value in line: | |
if value != " " and value != "\n": | |
self.grid[x][y] = value | |
y += 1 | |
x += 1 | |
y = 0 | |
def print_grid(self): | |
for row in self.grid: | |
print(row) | |
def validate_grid(self): | |
tmp = set() | |
x, y = 0, 0 | |
# rows | |
while x < 9: | |
while y < 9: | |
tmp.add(self.grid[x][y]) | |
y += 1 | |
if len(tmp) != 9: | |
print("Error at line {0}.".format(x+1)) | |
x += 1 | |
y = 0 | |
tmp.clear() | |
x = 0 | |
# columns | |
while y < 9: | |
while x < 9: | |
tmp.add(self.grid[x][y]) | |
x += 1 | |
if len(tmp) != 9: | |
print("Error at column {0}.".format(y+1)) | |
y += 1 | |
x = 0 | |
tmp.clear() | |
y = 0 | |
if __name__ == "__main__": | |
Sudoku() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment