Last active
December 5, 2019 21:51
-
-
Save antosha417/d5f45e5d92b67760d5beba2b453f929c to your computer and use it in GitHub Desktop.
Sudoku checker Антон Ковальков kovalkov.av@phystech.edu
This file contains 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
NUMBERS_IN_ROW = sorted(range(1, 10)) | |
def check_row(row): | |
# all x in NUMBERS_IN_ROW needed if row is smth like ['hello', True, 1, 5.5] or else sorted will throw | |
return all(map(lambda x: x in NUMBERS_IN_ROW, row)) and sorted(row) == NUMBERS_IN_ROW | |
def check_rows(matrix): | |
return all(map(check_row, matrix)) | |
def transpose(matrix): | |
return zip(*matrix) | |
def check_columns(matrix): | |
return check_rows(transpose(matrix)) | |
def get_sudoku_squares(matrix): | |
for i in range(0, 9, 3): | |
for j in range(0, 9, 3): | |
yield (row[j:j+3] for row in matrix[i:i+3]) | |
def flatten_array(matrix): | |
return sum(matrix, []) | |
def check_squares(matrix): | |
return check_rows(map(flatten_array, get_sudoku_squares(matrix))) | |
def is_valid_sudoku(matrix): | |
return check_rows(matrix) and check_columns(matrix) and check_squares(matrix) | |
if __name__ == '__main__': | |
sudoku_to_check = [ | |
[3, 1, 6, 5, 7, 8, 4, 9, 2], | |
[5, 2, 9, 1, 3, 4, 7, 6, 8], | |
[4, 8, 7, 6, 2, 9, 5, 3, 1], | |
[2, 6, 3, 4, 1, 5, 9, 8, 7], | |
[9, 7, 4, 8, 6, 3, 1, 2, 5], | |
[8, 5, 1, 7, 9, 2, 6, 4, 3], | |
[1, 3, 8, 9, 4, 7, 2, 5, 6], | |
[6, 9, 2, 3, 5, 1, 8, 7, 4], | |
[7, 4, 5, 2, 8, 6, 3, 1, 9] | |
] | |
if is_valid_sudoku(sudoku_to_check): | |
print('корректно') | |
else: | |
print('некорректно') |
This file contains 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
import unittest | |
from sudoku import * | |
class MyTestCase(unittest.TestCase): | |
def test_check_row(self): | |
row = range(1, 10) | |
self.assertEqual(check_row(row), True) | |
row = [1] * 9 | |
self.assertEqual(check_row(row), False) | |
row = [sum(NUMBERS_IN_ROW)] | |
self.assertEqual(check_row(row), False) | |
row = [3, 7, 5, 2, 6, 8, 9, 1, 4] | |
self.assertEqual(check_row(row), True) | |
row = [9] * 5 | |
self.assertEqual(check_row(row), False) | |
row = [1, 2, 4, 4, 4, 6, 7, 8, 9] | |
self.assertEqual(check_row(row), False) | |
def test_check_rows(self): | |
matrix = [[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8]] | |
self.assertEqual(check_rows(matrix), True) | |
matrix = [[8, 11, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8]] | |
self.assertEqual(check_rows(matrix), False) | |
def test_check_transpose(self): | |
matrix = [[1, 2, 3], | |
[5, 7, 9]] | |
self.assertSequenceEqual(list(transpose(matrix)), [(1, 5), (2, 7), (3, 9)]) | |
def test_flatten_tuple(self): | |
matrix = [[1, 2, 3], [7, 5, 3, 1]] | |
self.assertSequenceEqual(flatten_array(matrix), (1, 2, 3, 7, 5, 3, 1)) | |
def test_get_sudoku_squares(self): | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 5, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
[5, 9, 8, 7, 3, 6, 2, 4, 1], | |
] | |
self.assertSequenceEqual(list(map(list, get_sudoku_squares(test_sudoku))), [ | |
[[8, 5, 9], | |
[7, 2, 3], | |
[1, 6, 4]], | |
[[6, 1, 2], | |
[8, 5, 4], | |
[3, 7, 9]], | |
[[4, 3, 7], | |
[1, 6, 9], | |
[5, 2, 8]], | |
[[9, 8, 6], | |
[3, 7, 5], | |
[2, 4, 1]], | |
[[1, 4, 7], | |
[2, 6, 8], | |
[5, 9, 3]], | |
[[3, 5, 2], | |
[9, 1, 4], | |
[7, 8, 6]], | |
[[4, 3, 2], | |
[6, 1, 7], | |
[5, 9, 8]], | |
[[9, 8, 1], | |
[4, 2, 5], | |
[7, 3, 6]], | |
[[6, 7, 5], | |
[8, 9, 3], | |
[2, 4, 1]] | |
]) | |
def test_is_valid_sudoku(self): | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 5, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
[5, 9, 8, 7, 3, 6, 2, 4, 1], | |
] | |
self.assertEqual(is_valid_sudoku(test_sudoku), True ) | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 0, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
[5, 9, 8, 7, 3, 6, 2, 4, 1], | |
] | |
self.assertEqual(is_valid_sudoku(test_sudoku), False) | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7, 0], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 5, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
[5, 9, 8, 7, 3, 6, 2, 4, 1], | |
] | |
self.assertEqual(is_valid_sudoku(test_sudoku), False) | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 4, 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 5, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
] | |
self.assertEqual(is_valid_sudoku(test_sudoku), False) | |
test_sudoku = [ | |
[8, 5, 9, 6, 1, 2, 4, 3, 7], | |
[7, 2, 3, 8, 5, 4, 1, 6, 9], | |
[1, 6, 'hello', 3, 7, 9, 5, 2, 8], | |
[9, 8, 6, 1, 4, 7, 3, 5, 2], | |
[3, 7, 5, 2, 6, 8, 9, 1, 4], | |
[2, 4, 1, 5, 9, 3, 7, 8, 6], | |
[4, 3, 2, 9, 8, 1, 6, 7, 5], | |
[6, 1, 7, 4, 2, 5, 8, 9, 3], | |
[5, 9, 8, 7, 3, 6, 2, 4, 1], | |
] | |
self.assertEqual(is_valid_sudoku(test_sudoku), False) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment