import numpy as np def longest_common_prefix(xs, ys): min_len = min(len(xs), len(ys)) idxs = (xs[:min_len] != ys[:min_len]).nonzero()[0] if len(idxs) > 0: return idxs[0] else: return min_len # Like np.cumsum, but with a leading zero. def cumsum0(x, axis): pad_width = len(x.shape) * [(0,0)] pad_width[axis] = (1,0) return np.cumsum(np.pad(x, pad_width), axis=axis)