My Mom always does these anagram puzzles in her morning papers, so I took it upon myself to create a solution finder for her! Now I know that most of you probably aren't interested in what tmps Mother does or solving anagram problems, but you might find the code I made somewhat interesting.
What I am doing is taking a scrambled input string and checking to see if any combination of the string exists in the dictionary file (is it a valid english word or not). Here is what the program looks like in action:
bash-3.00$ time puzzle.sh adegln
Using anagram: adegln
Sorting adegln alphabetically... adegln
Scanning database...
Found 1 result(s):
angled
Found 2 result(s):
dangle
real 0m0.343s
As you can see, it works pretty quickly ;P The
time program tells us less than a second, anyways.
How it works:
I started off with just a dictionary of all english words (thanks iago). I created script.sh which takes every line in the wordlist and rearranges each
character alphabetically, keeping all of the lines in the original order. Now you have two wordlists, both parallel on a line to line basis. Then I created afilter.c.
afilter (once compiled) takes data from standard input and rearranges the data alphabetically, then spits out the arrangement, in classical unix fashion ;P. Then I wrote
puzzle.sh, a bash script that takes a command line argument (the scrambled word), pipes it to
afilter wich outputs an alphabetically arranged version of the anagram. That output is
grep'd from the second (alphabetized) wordlist. When a match is found, the line number is extracted via
awk and
awk is used again to scan the first (original) wordlist for that very line number, which is parallel to the second word list, thus giving you the matching (english) word.
Here is the link to the source (script.sh, afilter.c, puzzle.sh, both wordlists) if anybody is interested:
www.javaop.com/~tmp/puzzle.tar.gzIt is 600k, 95% of which is the dictionary file included.