74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
from typing import Dict, List, Set
|
|
import pytest
|
|
from collections import defaultdict
|
|
|
|
|
|
class Solution:
|
|
"""Bruteforce solution (4 hints)"""
|
|
|
|
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
|
squares: Dict = {
|
|
"00": set(),
|
|
"01": set(),
|
|
"02": set(),
|
|
"10": set(),
|
|
"11": set(),
|
|
"12": set(),
|
|
"20": set(),
|
|
"21": set(),
|
|
"22": set(),
|
|
}
|
|
lines = defaultdict(set)
|
|
for i in range(0, len(board)):
|
|
for j in range(0, len(board[i])):
|
|
board_elem = board[i][j]
|
|
squares_index = f"{i // 3}{j // 3}"
|
|
squares_index_len = len(squares[squares_index])
|
|
lines_index_len = len(lines[squares_index])
|
|
if board_elem != ".":
|
|
squares[squares_index].add(board_elem)
|
|
if len(squares[squares_index]) == squares_index_len:
|
|
return False
|
|
lines[f"{i}{j}"].add(board_elem)
|
|
if len(lines[f"{i}{j}"]) == lines_index_len:
|
|
breakpoint()
|
|
return False
|
|
return True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"input_value, expected_value",
|
|
[
|
|
(
|
|
[
|
|
["1", "2", ".", ".", "3", ".", ".", ".", "."],
|
|
["4", ".", ".", "5", ".", ".", ".", ".", "."],
|
|
[".", "9", "8", ".", ".", ".", ".", ".", "3"],
|
|
["5", ".", ".", ".", "6", ".", ".", ".", "4"],
|
|
[".", ".", ".", "8", ".", "3", ".", ".", "5"],
|
|
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
|
|
[".", ".", ".", ".", ".", ".", "2", ".", "."],
|
|
[".", ".", ".", "4", "1", "9", ".", ".", "8"],
|
|
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
|
|
],
|
|
True,
|
|
),
|
|
(
|
|
[
|
|
["1", "2", ".", ".", "3", ".", ".", ".", "."],
|
|
["4", ".", ".", "5", ".", ".", ".", ".", "."],
|
|
[".", "9", "1", ".", ".", ".", ".", ".", "3"],
|
|
["5", ".", ".", ".", "6", ".", ".", ".", "4"],
|
|
[".", ".", ".", "8", ".", "3", ".", ".", "5"],
|
|
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
|
|
[".", ".", ".", ".", ".", ".", "2", ".", "."],
|
|
[".", ".", ".", "4", "1", "9", ".", ".", "8"],
|
|
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
|
|
],
|
|
False,
|
|
),
|
|
],
|
|
)
|
|
def test_solution(input_value, expected_value):
|
|
s = Solution()
|
|
assert s.isValidSudoku(board=input_value) == expected_value
|