Compare commits

...

3 Commits

Author SHA1 Message Date
3f5146d5a8 Added leetcode exercise 36 2022-09-03 15:59:16 +02:00
fb0684dd27 Fixed import and formatting of leetcode 17 2022-09-03 15:59:02 +02:00
5e3a66aa45 Added typing.List import to leetcode template 2022-09-03 15:56:19 +02:00
3 changed files with 63 additions and 9 deletions

View File

@ -1,21 +1,24 @@
from typing import List
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if len(digits) == 0:
return []
options = {
'2': ('a', 'b', 'c'),
'3': ('d', 'e', 'f'),
'4': ('g', 'h', 'i'),
'5': ('j', 'k', 'l'),
'6': ('m', 'n', 'o'),
'7': ('p', 'q', 'r', 's'),
'8': ('t', 'u', 'v'),
'9': ('w', 'x', 'y', 'z')
"2": ("a", "b", "c"),
"3": ("d", "e", "f"),
"4": ("g", "h", "i"),
"5": ("j", "k", "l"),
"6": ("m", "n", "o"),
"7": ("p", "q", "r", "s"),
"8": ("t", "u", "v"),
"9": ("w", "x", "y", "z"),
}
pools = (options[digit] for digit in digits)
result = [""]
for pool in pools:
result = [x+y for x in result for y in pool]
result = [x + y for x in result for y in pool]
return result

View File

@ -0,0 +1,50 @@
from typing import List
import pytest
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows, cols, cubes = set(), set(), set()
for i in range(9):
for j in range(9):
val = board[i][j]
if not val.isdigit():
continue
cube = (i // 3) * 3 + (j // 3)
if (i, val) in rows or (j, val) in cols or (cube, val) in cubes:
return False
rows.add((i, val))
cols.add((j, val))
cubes.add((cube, val))
return True
@pytest.fixture
def solution():
return Solution()
@pytest.mark.parametrize(
"sudoku, expected",
[
(
[
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
],
True,
)
],
)
def test_search(solution: Solution, sudoku: List[List[str]], expected: bool):
assert solution.isValidSudoku(sudoku) == expected

View File

@ -1,3 +1,4 @@
from typing import List
import pytest