Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a wrong answer in 16 #125

Open
atomAltera opened this issue Jun 18, 2020 · 7 comments
Open

There is a wrong answer in 16 #125

atomAltera opened this issue Jun 18, 2020 · 7 comments

Comments

@atomAltera
Copy link

atomAltera commented Jun 18, 2020

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.

@rougier
Copy link
Owner

rougier commented Jun 19, 2020

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

@atomAltera
Copy link
Author

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.

atomAltera added a commit to atomAltera/numpy-100 that referenced this issue Jun 19, 2020
@rougier
Copy link
Owner

rougier commented Jun 19, 2020

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

1 similar comment
@rougier
Copy link
Owner

rougier commented Jun 19, 2020

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

@atomAltera
Copy link
Author

atomAltera commented Jun 19, 2020

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

@ghost
Copy link

ghost commented Sep 15, 2020

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

@rougier
Copy link
Owner

rougier commented Sep 15, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants