Skip to content

Instantly share code, notes, and snippets.

@robintw
Created April 5, 2016 10:22
Show Gist options
  • Save robintw/67d5ba1633a329009e59ee152d1c91ef to your computer and use it in GitHub Desktop.
Save robintw/67d5ba1633a329009e59ee152d1c91ef to your computer and use it in GitHub Desktop.
Example code for interview with Robin
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