Perl Weekly Challenge 038
$ perl perl/ch-1.pl
What I Did
First we have to validate the input, which seemed to be best done with a regex. I don't write regexes often enough to keep one this long in my head so I usually use a tool like Regexer to help write and debug a regex of this length. The function is_valid returns a list of values, the first one being a boolean indicating whether or not the input is valid. If this value is true we pass the other values, which are the fields of interest, to the function which formats the output as required.
$ perl perl/ch-2.pl
LETTERS: B, H, T, X, E, C, U
BUTCH has a score of 20.
These letters were used: B, H, T, C, U.
What I Did
First off I considered that we are really only asked to perform this for a single set of letters. In that spirit I did not include some optimizations. Specifically, back in previous challenges which involved anagrams I used an approach with the Fundamental Theorem of Arithmetic which allowed for quick finding of words by assigning each word a value based on assigning each letter a unique prime number value. This is skipped here, generating a comprehensive lookup table would be unnecessary effort for evaluating a single set of letters. If interested please see, for example, my description of Challenge 005.
The approach here can be summarized as follows:
- Create Readonly hashes for character frequencies and values.
- Use the frequency hash to generate a random set of letters.
- For each word in the system dictionary see if it can be made up of some or all of our random letters.
- Every word that meets this criteria is computed a score using the character value hash.
- The highest word score, the word itself, and the letters used are returned and displayed.