Skip to content

Chic-fil-A Solution


dists = np.abs(np.linspace([17,32], [28,36], num=3, axis=1) - 30)
dists[[0,1], [1,1]]
# array([7.5, 4. ])

Explanation

The trick here is to make use of NumPy's linspace() function. We can represent the location of the first three ads via

np.linspace(start=17, stop=28, num=3)
# array([17. , 22.5, 28. ])

Similarly, we can represent the location of the last three ads via

np.linspace(start=32, stop=36, num=3)
# array([32., 34., 36.])

The linspace() function lets us do both of these operations at once, resulting in a 2-d array.

np.linspace(start=[17,32], stop=[28,36], num=3, axis=1)
# array([[17. , 22.5, 28. ],
#        [32. , 34. , 36. ]])

If we subtract 30 from this array, we'll get the distance between each ad and the restaurant.

np.linspace([17,32], [28,36], num=3, axis=1) - 30
# array([[-13. ,  -7.5,  -2. ],
#        [  2. ,   4. ,   6. ]])

..except distances should be non-negative, so we'll wrap that in np.abs()

np.abs(np.linspace([17,32], [28,36], num=3, axis=1) - 30)
# array([[13. ,  7.5,  2. ],
#        [ 2. ,  4. ,  6. ]])

Now we have a representation of the distance each billboard is from the restaurant. The "B" ads are located at positions (0,1) and (1,1). To fetch these elements, we can do

dists = np.abs(np.linspace([17,32], [28,36], num=3, axis=1) - 30)
dists[[0,1], [1,1]]
# array([7.5, 4. ])

See the problem