Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to generate a 2d array forming a chequerboard without numpy?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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): 
['#', '#', '#', ' ', ' ', ' ', '#']
['#', '#', '#', ' ', ' ', ' ', '#']
[' ', ' ', ' ', '#', '#', '#', ' ']
[' ', ' ', ' ', '#', '#', '#', ' ']
['#', '#', '#', ' ', ' ', ' ', '#']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading