MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_valid

Function is_valid

backtracking/crossword_puzzle_solver.py:4–33  ·  view source on GitHub ↗

Check if a word can be placed at the given position. >>> puzzle = [ ... ['', '', '', ''], ... ['', '', '', ''], ... ['', '', '', ''], ... ['', '', '', ''] ... ] >>> is_valid(puzzle, 'word', 0, 0, True) True >>> puzzle = [ ... ['', '',

(
    puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool
)

Source from the content-addressed store, hash-verified

2
3
4def is_valid(
5 puzzle: list[list[str]], word: str, row: int, col: int, vertical: bool
6) -> bool:
7 """
8 Check if a word can be placed at the given position.
9
10 >>> puzzle = [
11 ... ['', '', '', ''],
12 ... ['', '', '', ''],
13 ... ['', '', '', ''],
14 ... ['', '', '', '']
15 ... ]
16 >>> is_valid(puzzle, 'word', 0, 0, True)
17 True
18 >>> puzzle = [
19 ... ['', '', '', ''],
20 ... ['', '', '', ''],
21 ... ['', '', '', ''],
22 ... ['', '', '', '']
23 ... ]
24 >>> is_valid(puzzle, 'word', 0, 0, False)
25 True
26 """
27 for i in range(len(word)):
28 if vertical:
29 if row + i >= len(puzzle) or puzzle[row + i][col] != "":
30 return False
31 elif col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
32 return False
33 return True
34
35
36def place_word(

Callers 1

solve_crosswordFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected