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

Function validate_matrix_content

matrix/matrix_based_game.py:64–89  ·  view source on GitHub ↗

Validates that the number of elements in the matrix matches the given size. >>> validate_matrix_content(['aaaa', 'aaaa', 'aaaa', 'aaaa'], 3) Traceback (most recent call last): ... ValueError: The matrix dont match with size. >>> validate_matrix_content(['aa%', 'aaa', 'a

(matrix: list[str], size: int)

Source from the content-addressed store, hash-verified

62
63
64def validate_matrix_content(matrix: list[str], size: int) -> None:
65 """
66 Validates that the number of elements in the matrix matches the given size.
67
68 >>> validate_matrix_content(['aaaa', 'aaaa', 'aaaa', 'aaaa'], 3)
69 Traceback (most recent call last):
70 ...
71 ValueError: The matrix dont match with size.
72 >>> validate_matrix_content(['aa%', 'aaa', 'aaa'], 3)
73 Traceback (most recent call last):
74 ...
75 ValueError: Matrix rows can only contain letters and numbers.
76 >>> validate_matrix_content(['aaa', 'aaa', 'aaaa'], 3)
77 Traceback (most recent call last):
78 ...
79 ValueError: Each row in the matrix must have exactly 3 characters.
80 """
81 print(matrix)
82 if len(matrix) != size:
83 raise ValueError("The matrix dont match with size.")
84 for row in matrix:
85 if len(row) != size:
86 msg = f"Each row in the matrix must have exactly {size} characters."
87 raise ValueError(msg)
88 if not all(char.isalnum() for char in row):
89 raise ValueError("Matrix rows can only contain letters and numbers.")
90
91
92def validate_moves(moves: list[tuple[int, int]], size: int) -> None:

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected