Created
April 5, 2016 10:22
-
-
Save robintw/67d5ba1633a329009e59ee152d1c91ef to your computer and use it in GitHub Desktop.
Example code for interview with Robin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.signal import convolve2d | |
def func(arr): | |
""" | |
Arguments: | |
arr: a numpy array (any numeric dtype, any dimensions) | |
Return value: | |
a single floating point number | |
""" | |
counts, bins = np.histogram(arr) | |
i = np.argmax(counts) | |
l = bins[i] | |
u = bins[i + 1] | |
return (l + u) / 2.0 | |
def f(arr): | |
""" | |
Arguments: | |
arr: 2D numpy array, with dtype=bool | |
Return value: | |
numpy array of the same size and dtype as `arr` | |
""" | |
kernel = np.array([[1, 1, 1], | |
[1, 0, 1], | |
[1, 1, 1]]) | |
result = np.bitwise_and(convolve2d(arr, kernel, mode='same') == 1, | |
arr) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment