Challenge - Psycho Parent¶
Eager to make your mark on the PTA, you decide the best way to hide eggs for the upcoming Easter egg hunt is to use NumPy. You represent the field as a 10x10 array of 0s.
In [76]:
import numpy as np
field = np.zeros(shape = (10, 10))
print(field)
# [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
In [85]:
import sys
np.set_printoptions(linewidth=999)
np.set_printoptions(threshold=sys.maxsize)
generator = np.random.default_rng(seed=123)
random_integers = generator.normal(loc=10, scale=3, size=20).astype(int)
flat_indices = generator.choice(field.shape[0]*field.shape[1], size=20, replace=False)
coords = np.unravel_index(flat_indices, field.shape)
coords = np.transpose(coords)
field[coords[:,0], coords[:, 1]] = random_integers
field
Out[85]:
array([[12., 7., 0., 0., 0., 0., 0., 14., 0., 0.], [ 0., 0., 0., 13., 0., 0., 8., 0., 8., 0.], [ 7., 0., 0., 0., 11., 0., 0., 0., 0., 0.], [ 0., 0., 11., 0., 0., 0., 0., 10., 0., 0.], [ 0., 0., 9., 0., 5., 0., 0., 0., 0., 0.], [ 0., 10., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 11., 0., 13., 0., 0., 0., 0., 10.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 9., 8., 9., 0., 0., 0., 0., 0., 13., 0.]])