The problem is the following:
chequerboard(n, m, offset_h, offset_w), all arguments >=1, returns an n by m chequerboard where each (region) is of size offset_h by offset_w, the top left cell is always filled, and only the right hand and bottom edge may contain incomplete chequers.
Examples:
chequerboard(6, 8, 2, 2) returns
[['#', '#', ' ', ' ', '#', '#', ' ', ' '],
['#', '#', ' ', ' ', '#', '#', ' ', ' '],
[' ', ' ', '#', '#', ' ', ' ', '#', '#'],
[' ', ' ', '#', '#', ' ', ' ', '#', '#'],
['#', '#', ' ', ' ', '#', '#', ' ', ' '],
['#', '#', ' ', ' ', '#', '#', ' ', ' ']]
chequerboard(5, 7, 2, 3) returns
[['#', '#', '#', ' ', ' ', ' ', '#'],
['#', '#', '#', ' ', ' ', ' ', '#'],
[' ', ' ', ' ', '#', '#', '#', ' '],
[' ', ' ', ' ', '#', '#', '#', ' '],
['#', '#', '#', ' ', ' ', ' ', '#']]
chequerboard(6, 8, 2, 2) returns
[['#', '#', ' ', ' ', '#', '#', ' ', ' '],
['#', '#', ' ', ' ', '#', '#', ' ', ' '],
[' ', ' ', '#', '#', ' ', ' ', '#', '#'],
[' ', ' ', '#', '#', ' ', ' ', '#', '#'],
['#', '#', ' ', ' ', '#', '#', ' ', ' '],
['#', '#', ' ', ' ', '#', '#', ' ', ' ']]
chequerboard(5, 7, 2, 3) returns
[['#', '#', '#', ' ', ' ', ' ', '#'],
['#', '#', '#', ' ', ' ', ' ', '#'],
[' ', ' ', ' ', '#', '#', '#', ' '],
[' ', ' ', ' ', '#', '#', '#', ' '],
['#', '#', '#', ' ', ' ', ' ', '#']]
How can I solve it without Numpy? i.e., using only vanilla python?
>Solution :
You could utilize the modulo operator (%):
def print_chequerboard(board: list[list[str]]) -> None:
for row in board:
print(row)
def chequerboard(n: int, m: int, offset_h: int, offset_w: int) -> list[list[str]]:
board = [[' ' for _ in range(m)] for _ in range(n)]
for i in range(n):
for j in range(m):
if i // offset_h % 2 == 0 and j // offset_w % 2 == 0 or \
i // offset_h % 2 == 1 and j // offset_w % 2 == 1:
board[i][j] = '#'
return board
def main() -> None:
print('chequerboard(6, 8, 2, 2): ')
print_chequerboard(chequerboard(6, 8, 2, 2))
print('chequerboard(5, 7, 2, 3): ')
print_chequerboard(chequerboard(5, 7, 2, 3))
print('chequerboard(6, 8, 2, 2): ')
print_chequerboard(chequerboard(6, 8, 2, 2))
print('chequerboard(5, 7, 2, 3): ')
print_chequerboard(chequerboard(5, 7, 2, 3))
if __name__ == '__main__':
main()
Output:
chequerboard(6, 8, 2, 2):
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
[' ', ' ', '#', '#', ' ', ' ', '#', '#']
[' ', ' ', '#', '#', ' ', ' ', '#', '#']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
chequerboard(5, 7, 2, 3):
['#', '#', '#', ' ', ' ', ' ', '#']
['#', '#', '#', ' ', ' ', ' ', '#']
[' ', ' ', ' ', '#', '#', '#', ' ']
[' ', ' ', ' ', '#', '#', '#', ' ']
['#', '#', '#', ' ', ' ', ' ', '#']
chequerboard(6, 8, 2, 2):
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
[' ', ' ', '#', '#', ' ', ' ', '#', '#']
[' ', ' ', '#', '#', ' ', ' ', '#', '#']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
['#', '#', ' ', ' ', '#', '#', ' ', ' ']
chequerboard(5, 7, 2, 3):
['#', '#', '#', ' ', ' ', ' ', '#']
['#', '#', '#', ' ', ' ', ' ', '#']
[' ', ' ', ' ', '#', '#', '#', ' ']
[' ', ' ', ' ', '#', '#', '#', ' ']
['#', '#', '#', ' ', ' ', ' ', '#']