Created
December 3, 2012 19:47
-
-
Save loisaidasam/4197457 to your computer and use it in GitHub Desktop.
JUMBLE solver
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
'''Script for solving those pesky newspaper jumbles | |
Sample usage: | |
$ python jumble_solver.py SWARLP | |
sprawl | |
''' | |
import random | |
import sys | |
def jumble(word): | |
used = [] | |
while len(used) != len(word): | |
pos = random.randint(0, len(word)-1) | |
if pos not in used: | |
used.append(pos) | |
result = '' | |
for i in used: | |
result += word[i] | |
return result | |
def solve(word): | |
fp = open('/usr/share/dict/words', 'r') | |
words = fp.read() | |
fp.close() | |
words = words.split("\n") | |
word = word.lower() | |
while True: | |
j = jumble(word) | |
if j in words: | |
return j | |
def main(): | |
word = sys.argv[1] | |
print solve(word) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment