Skip to content

Instantly share code, notes, and snippets.

@huntwelch
Created September 19, 2016 16:48
Show Gist options
  • Select an option

  • Save huntwelch/1c40cff74a6e0c7b88bce2f5874f67ea to your computer and use it in GitHub Desktop.

Select an option

Save huntwelch/1c40cff74a6e0c7b88bce2f5874f67ea to your computer and use it in GitHub Desktop.
f = open('/usr/share/dict/words')
words = f.readlines()
f.close()
words = [x.strip() for x in words if x.islower()]
check = set(words)
inputword = 'dick'
def spoonit(word):
spoon_1 = ''.join([word[:1], inputword[1:]])
spoon_2 = ''.join([inputword[:1], word[1:]])
if spoon_1 not in check: return False
if spoon_2 not in check: return False
return ' '.join([spoon_1, spoon_2])
spoons = map(spoonit, words)
view = [x for x in spoons if x]
print '\n'.join(view)
@jsbronder
Copy link

spoons = [
    '%s%s %s%s' % (w[:1], inputword[1:], inputword[:1], w[1:])
    for w in words
    if w[:1] + inputword[1:] in check and inputword[:1] + w[1:] in check]

The last part might be faster as
if set([w[:1] + inputword[1:], inputword[:1] + word[1:]]).issubset(check)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment