Created
May 4, 2022 10:53
-
-
Save les-peters/b5cd23f74f6e88eb4030318fab350d87 to your computer and use it in GitHub Desktop.
SimpleAutocomplete
This file contains 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
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