mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Compare commits
7 Commits
6ba76e42c0
...
fe908d59ae
Author | SHA1 | Date | |
---|---|---|---|
|
fe908d59ae | ||
|
f3f32ae3ca | ||
|
e3bd7721c8 | ||
|
e3f3d668be | ||
|
3e9ca92ca9 | ||
|
4279985962 | ||
|
9daa2bbb3d |
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.7.1
|
rev: v0.7.4
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -29,7 +29,7 @@ repos:
|
||||||
- tomli
|
- tomli
|
||||||
|
|
||||||
- repo: https://github.com/tox-dev/pyproject-fmt
|
- repo: https://github.com/tox-dev/pyproject-fmt
|
||||||
rev: "v2.4.3"
|
rev: "v2.5.0"
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyproject-fmt
|
- id: pyproject-fmt
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ repos:
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.22
|
rev: v0.23
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
|
|
76
backtracking/sodukuSolver
Normal file
76
backtracking/sodukuSolver
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
class Solution(object):
|
||||||
|
def solveSudoku(self, board):
|
||||||
|
"""
|
||||||
|
:type board: List[List[str]]
|
||||||
|
:rtype: None Do not return anything, modify board in-place instead.
|
||||||
|
"""
|
||||||
|
# Helper functions for bit manipulation
|
||||||
|
def char_to_index(ch):
|
||||||
|
return ord(ch) - ord('1')
|
||||||
|
|
||||||
|
def index_to_char(index):
|
||||||
|
return chr(index + ord('1'))
|
||||||
|
|
||||||
|
# Initialize bitmasks for rows, columns, and boxes
|
||||||
|
rows = [0] * 9
|
||||||
|
cols = [0] * 9
|
||||||
|
boxes = [0] * 9
|
||||||
|
|
||||||
|
# Populate bitmasks with initial board values
|
||||||
|
for r in range(9):
|
||||||
|
for c in range(9):
|
||||||
|
if board[r][c] != '.':
|
||||||
|
num = char_to_index(board[r][c])
|
||||||
|
bit = 1 << num
|
||||||
|
rows[r] |= bit
|
||||||
|
cols[c] |= bit
|
||||||
|
boxes[(r // 3) * 3 + (c // 3)] |= bit
|
||||||
|
|
||||||
|
# Check if placing a number is valid
|
||||||
|
def is_valid(row, col, num):
|
||||||
|
bit = 1 << num
|
||||||
|
box_index = (row // 3) * 3 + (col // 3)
|
||||||
|
return not (rows[row] & bit or cols[col] & bit or boxes[box_index] & bit)
|
||||||
|
|
||||||
|
# Forward Checking to find the most constrained cell
|
||||||
|
def select_cell():
|
||||||
|
min_possibilities = float('inf')
|
||||||
|
cell = None
|
||||||
|
for row in range(9):
|
||||||
|
for col in range(9):
|
||||||
|
if board[row][col] == '.':
|
||||||
|
possibilities = 0
|
||||||
|
for num in range(9):
|
||||||
|
if is_valid(row, col, num):
|
||||||
|
possibilities += 1
|
||||||
|
if possibilities < min_possibilities:
|
||||||
|
min_possibilities = possibilities
|
||||||
|
cell = (row, col)
|
||||||
|
return cell
|
||||||
|
|
||||||
|
# Recursive function to solve the board
|
||||||
|
def solve():
|
||||||
|
cell = select_cell()
|
||||||
|
if cell is None:
|
||||||
|
return True # All cells are filled
|
||||||
|
|
||||||
|
row, col = cell
|
||||||
|
for num in range(9):
|
||||||
|
if is_valid(row, col, num):
|
||||||
|
bit = 1 << num
|
||||||
|
board[row][col] = index_to_char(num)
|
||||||
|
rows[row] |= bit
|
||||||
|
cols[col] |= bit
|
||||||
|
boxes[(row // 3) * 3 + (col // 3)] |= bit
|
||||||
|
|
||||||
|
if solve():
|
||||||
|
return True
|
||||||
|
|
||||||
|
board[row][col] = '.'
|
||||||
|
rows[row] &= ~bit
|
||||||
|
cols[col] &= ~bit
|
||||||
|
boxes[(row // 3) * 3 + (col // 3)] &= ~bit
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
solve()
|
|
@ -172,7 +172,7 @@ def solved(values):
|
||||||
|
|
||||||
def from_file(filename, sep="\n"):
|
def from_file(filename, sep="\n"):
|
||||||
"Parse a file into a list of strings, separated by sep."
|
"Parse a file into a list of strings, separated by sep."
|
||||||
return open(filename).read().strip().split(sep) # noqa: SIM115
|
return open(filename).read().strip().split(sep)
|
||||||
|
|
||||||
|
|
||||||
def random_puzzle(assignments=17):
|
def random_puzzle(assignments=17):
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!python
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user