Created
April 23, 2015 09:54
-
-
Save show0k/adb88a380eba3d437cd9 to your computer and use it in GitHub Desktop.
Bruteforce word with a specified separator
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 find_words(instring, words, prefix=''): | |
| """ Quick and dirty snippet to split string without space | |
| to a list of known words. | |
| Used to pass over a bug of Snap!""" | |
| if not instring: | |
| return [] | |
| if (not prefix) and (instring in words): | |
| return [instring] | |
| prefix, suffix = prefix + instring[0], instring[1:] | |
| solutions = [] | |
| # Case 1: prefix in solution | |
| if prefix in words: | |
| try: | |
| solutions.append([prefix] + __find_words__(suffix, words, '')) | |
| except ValueError: | |
| pass | |
| # Case 2: prefix not in solution | |
| try: | |
| solutions.append(__find_words__(suffix, words, prefix)) | |
| except ValueError: | |
| pass | |
| if solutions: | |
| return sorted(solutions, | |
| key=lambda solution: [len(word) for word in solution], | |
| reverse=True)[0] | |
| else: | |
| return instring |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment