GolfScript, 27 chars, 7 correct outputs ⇒ score ≈ 25.926
n%(\:w;' '%{$:c;w{$c=}?}%n*
GolfScript doesn't have a file I/O operator, so I had to get a bit clever with the input. Specifically,
- the scrambled words should be given on the first input line, separated by spaces, and
- the wordlist should be given on the subsequent input lines, one word per line.
The scrambled words and the wordlist need to have the same letter case.
Here's an example invocation from a Unix shell command line, assuming that the code above has been saved as unscramble.gs, the GolfScript interpreter as golfscript.rb and the wordlist as wordlist.txt:
(echo BBULEB LWRFOE KAEC RTAWE COKR KMLI PCAEE; cat wordlist.txt) \
| ruby golfscript.rb unscramble.gs
This will produce the following output:
BUBBLE
FLOWER
CAKE
WATER
CORK
MILK
PEACE
The way it works is simply by sorting the letters in each input word and selecting the first word in the wordlist that produces the same string when its letters are sorted. Nearly half of the code is actually just for input parsing (and output formatting): all the real work is done in the {$:c;w{$c=}?}% loop.
Here's a de-golfed version with comments showing how it works:
n % # split the input on newlines
( \ :w ; # pop the first line off the list of lines and assign the rest to w
' ' % # split the first line on spaces
# apply the following map to each scrambled word:
{
$ :c ; # sort the letters in the word and assign the sorted word to c
w { $ c = } ? # find the first word in w that, sorted, equals c
} %
n * # join the results with newlines for output
Scoring is suboptimal.
say 'bubble flower cake water rock milk peace'scores hard-to-beat 213 and yet isn't very interesting. – J B – 2012-03-13T10:17:14.1936Many scrambles have multiple correct unscrambles...I mean "opst" could easily be "stop" or "pots" or "post" or "tops". – dmckee --- ex-moderator kitten – 2012-03-13T14:01:16.527
1I notice that all answers so far have loaded the wordlist from the web. Is there any requirement that the file cannot be local? – Steven Rumbalski – 2012-03-13T19:36:48.597
@StevenRumbalski No, you can either use it from the web or locally. – 3aw5TZetdf – 2012-03-13T20:29:45.223
I want to repeat dmckee's question: "bbuleb lwrfoe kaec rtawe cokr kmli pcaee" can be decoded as: bubble fowler cake water cork milk peace. Is this be considered correct output? If it isn't, it seems that some hard-coding of the requirements into my program would be needed. – Steven Rumbalski – 2012-03-13T21:04:12.997
You state that "Word case doesn't matter", does that mean that we can assume our inputs are in the same case as the word file (because currently they aren't). – Steven Rumbalski – 2012-03-13T21:11:45.780
1@StevenRumbalski fowler and cork is considered correct output and the inputs can be the same case as the word life. (Will edit the post about the correct outputs) – 3aw5TZetdf – 2012-03-14T01:17:12.373
If the word source is avaliable, why (Amount of correct outputs) in score? – l4m2 – 2018-05-04T02:09:57.260