Home > AI > Language > Python >

(interview) Removing Boundaries

## Removing Boundaries

Using only `numpy`, create a function that takes an array and a binary mask and produces a cropped array based on the binary mask.

```py
import numpy as np

def boundary_cropping(a, m):
    pass

a1 = np.array([[0,0,0,0,0], [0,0,0,0,0], [0,1,0,1,1], [0,0,0,0,0]])
a2 = np.array([[ [0,0,0], [0,1,0], [0,1,0] ], [ [0,0,0], [0,1,0], [0,0,0] ], [ [0,0,0], [0,1,0], [0,0,0] ]])

print(boundary_cropping(a1, a1 != 0))
# [[1 0 1 1]]
print(boundary_cropping(a2, a2 != 0))
# [[[1] [1]] [[1] [0]] [[1] [0]]]
```

Leave a Reply