rougier/numpy-100

There is a wrong answer in 16

atomAltera opened this issue · 7 comments

Two answers are given to 16, one is using np.pad and the second is using indexing.

Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)

# Using fancy indexing
Z[:, [0, -1]] = 0
Z[[0, -1], :] = 0
print(Z)

But the first one adds a new border and the second one is just replaces current border.

You're right, we should use Z[1:-1,1:-1] instead of Z with the pad function. Can you make a PR ?

Z[1:-1,1:-1] = 0 would not help here. As I know, using indexing we can only replace values in existing rows/columns/cells.
The questions is How to add a border (filled with 0's) around an existing array?. And I think this either could not be solved using indexing, or it would be too complex.

The question is ambiguous but we can take advantage of it by simply distinguishing internal and external border. What do you think?

The question is ambiguous but we can take advantage of it by simply distinguishing internal and external border. What do you think?

I think answer with pad function is more preferred because padding can’t/hard to be done with indexing. To me the question is clear: add a border to existing array. So, all existing data have to be preserved and an extra rows/columns have to be added

import numpy as np
x = np.ones((3,3))
print("Original array:")
print(x)
print("0 on the border and 1 inside in the array")
x = np.pad(x, pad_width=1, mode='constant', constant_values=0)
print(x)
#use this

Again I would distinguish external vs internal border in the answer. Can you make a PR?