Last active
January 6, 2026 14:10
-
-
Save pythonhacker/757dac70b09e79664596519f810fef34 to your computer and use it in GitHub Desktop.
Word search using prefix search with the bisect module
This file contains hidden or 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
| def search_word(filename, prefix): | |
| """ Search a file containing a wordlist and print matching words by prefix """ | |
| wordlist = sorted(filter(None, [item.lower().strip() for item in open(filename)])) | |
| words = [] | |
| idx = bisect.bisect_left(wordlist, prefix) | |
| for idx in range(idx, len(wordlist)): | |
| word = wordlist[idx] | |
| if word.startswith(prefix): | |
| words.append(word) | |
| return words |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment