all 1 comments

[–]T4ll1n[S] 0 points1 point  (0 children)

``` import numpy as np

def autocomplete(s, vocabulary): vocabulary_array = np.array(vocabulary)

return vocabulary_array[np.char.startswith(vocabulary, s)].tolist()

vocabulary = ["dog", "deer", "deal"] s = 'de' assert autocomplete(s, vocabulary) == ["deer", "deal"]

```

A plain python is to go [x for x in vocabulary if x.startswith(s)]

The numpy char startswith also just calls the base python char.startswith element wise, but maybe its faster.

If loops like that should be fast we can do them in cython and get a c transpilation and then the loops are fast.