Skip to content

Instantly share code, notes, and snippets.

@show0k
Created April 23, 2015 09:54
Show Gist options
  • Save show0k/adb88a380eba3d437cd9 to your computer and use it in GitHub Desktop.
Save show0k/adb88a380eba3d437cd9 to your computer and use it in GitHub Desktop.
Bruteforce word with a specified separator
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