Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created May 4, 2022 10:53
Show Gist options
  • Save les-peters/b5cd23f74f6e88eb4030318fab350d87 to your computer and use it in GitHub Desktop.
Save les-peters/b5cd23f74f6e88eb4030318fab350d87 to your computer and use it in GitHub Desktop.
SimpleAutocomplete
question = """
Implement a simple version of autocomplete, where given an input string s and a
dictionary of words dict, return the word(s) in dict that partially match s
(or an empty string if nothing matches).
Example:
let dict = ['apple', 'banana', 'cranberry', 'strawberry']
$ simpleAutocomplete('app')
$ ['apple']
$ simpleAutocomplete('berry')
$ ['cranberry', 'strawberry']
$ simpleAutocomplete('fart')
$ []
"""
import re
def simpleAutocomplete(partial_word):
m = re.compile(partial_word)
dict = ['apple', 'banana', 'cranberry', 'strawberry']
matches = list(filter(lambda x: m.search(x), dict))
print(matches)
simpleAutocomplete('app')
simpleAutocomplete('berry')
simpleAutocomplete('fart')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment