Jumble
March 1, 2019
Our algorithm is to sort the letters of the target, creating a signature, then read a dictionary, calculate the signature of each word in the dictionary, and compare:
(define (jumble target) (with-input-from-file "words" (lambda () (let ((target (sort charlist target)))) (let loop ((word (read))) (unless (eof-object? word) (let ((signature (sort charlist (symbol->string word))))) (if (equal? target signature) word (loop (read))))))))))
And here is the program in action:
> (jumble "lteade") elated
I wrote that program in less time than it took to solve the puzzle; I’m not sure what that says about me. You can run the program at https://ideone.com/jxf1Dc.
I was really happy to get this one right…. although I came up with an archaic one as there is also a solution starting with “d”…
delate (verb)
Quick one in Ruby.
Outputs
A Haskell version.
Has an option to specify what the word starts with.
Here’s a solution in Python.
Output:
Solution with gawk, posix awk doesn’t have asort, BYO to make it more portable.
#! /usr/bin/gawk -f
jumble - solves a jumbled word puzzle
usage: jumble [-v puzzle="XXX"] [path/to/dictionary]
BEGIN {
IGNORECASE = 1
if( puzzle == "" ) puzzle = "LTEADE"
split(puzzle, puzzle_arr, "")
puzzle_len = asort(puzzle_arr)
}
{
if( length($0) != puzzle_len ) next
split($0, test_arr, "")
asort(test_arr)
for( i = 1 ; i <= puzzle_len ; i++ )
if( test_arr[i] != puzzle_arr[i] ) next
print FILENAME ": " $0
}
In action:
./jumble.awk -v puzzle="LTEADE" /usr/share/dict/*
/usr/share/dict/american-english: elated
/usr/share/dict/british: elated
/usr/share/dict/british-english: elated
/usr/share/dict/catala: delate
/usr/share/dict/catalan: delate
/usr/share/dict/german: adelte
/usr/share/dict/german: dealte
/usr/share/dict/german: tadele
/usr/share/dict/ngerman: adelte
/usr/share/dict/ngerman: dealte
/usr/share/dict/ngerman: tadele
/usr/share/dict/usa: elated
/usr/share/dict/words: elated
[…] exercise was posted over at ProgrammingPraxis called […]
Klong version (#5)
lteade:
elated
dbeia:
abide
groegj:
jogger
slenet:
nestle
nargo:
argon
groan
organ
“** Done **”