1
votes

Trying to make my code more efficient and readable and i'm stuck. Assume I want to build something like a chess board, with alternating black and white colors on an 8x8 grid. So, using numpy, I have done this:

import numpy as np
board = np.zeros((8,8), np.int32)
for ri in range(8):
    for ci in range(8):
            if (ci + ri) % 2 == 0:
                    board[ri,ci] = 1

Which nicely outputs:

array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]], dtype=int32)

That I can then parse as white squares or black squares. However, in practice my array is much larger, and this way is very inefficient and unreadable. I assumed numpy already has this figured out, so I tried this:

board = np.zeros(64, np.int32)
board[::2] = 1
board = board.reshape(8,8)

But that output is wrong, and looks like this:

array([[1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0]], dtype=int32)

Is there a better way to achieve what I want that works efficiently (and preferably, is readable)?

Note: i'm not attached to 1's and 0's, this can easily be done with other types of values, even True/False or strings of 2 kinds, as long as it works

5

5 Answers

4
votes

Here's one approach using slicing with proper starts and stepsize of 2 in two steps -

board = np.zeros((8,8), np.int32)
board[::2,::2] = 1
board[1::2,1::2] = 1

Sample run -

In [229]: board = np.zeros((8,8), np.int32)
     ...: board[::2,::2] = 1
     ...: board[1::2,1::2] = 1
     ...: 

In [230]: board
Out[230]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]], dtype=int32)

Other tricky ways -

1) Broadcasted comparison :

In [254]: r = np.arange(8)%2

In [255]: (r[:,None] == r)*1
Out[255]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]])

2) Broadcasted addition :

In [279]: r = np.arange(8)

In [280]: 1-(r[:,None] + r)%2
Out[280]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]])
2
votes

Just found out an alternative answer by myself, so posting it here for future reference to anyone who's interested:

a = np.array([[1,0],[0,1]])
b = np.tile(a, (4,4))

Results:

array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]])
1
votes

I think the following is also a good way of doing it for a variable input

import sys
lines = sys.stdin.readlines()
n = int(lines[0])

import numpy as np
a = np.array([[1,0], [0,1]],dtype=np.int)
outputData= np.tile(a,(n//2,n//2))

print(outputData)
0
votes

You can achieve this for single even input number n

import numpy as np
i = np.eye(2)
i = i[::-1]
k = np.array(i, dtype = np.int)
print(np.tile(k,(n//2,n//2)))
0
votes

I tried and found this to be shorter one for any giver number:

n = int(input())

import numpy as np

c = np.array([[0,1], [1, 0]])

print(np.tile(c, reps=(n//2, n//2)))