Challenge - Peanut Butter¶
Given peanut, a 4x5 array of 0s, and butter, a 5-element 1-d array of indices, fill the rows of peanut with 1s starting from the column indices given by butter.
In [63]:
import numpy as np
peanut = np.zeros(shape = (4, 5))
butter = np.array([3, 0, 4, 1])
print(peanut)
# [[0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0.]]
print(butter)
# [3 0 4 1]
[[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] [3 0 4 1]
In [66]:
ones = butter[:, None] <= np.arange(peanut.shape[1])
np.where(ones, 1, 0)
Out[66]:
array([[0, 0, 0, 1, 1], [1, 1, 1, 1, 1], [0, 0, 0, 0, 1], [0, 1, 1, 1, 1]])