Ruby Quiz wrote: > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! > > Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone > on Ruby Talk follow the discussion. Please reply to the original quiz message, > if you can. > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > by Ben Bleything > > This is a riff on the Jumble puzzle found in many (US) newspapers. More > specifically, it's based on the game TextTwist[1], made by GameHouse[2] and > published in various places around the web. > > The mechanic of TextTwist is simple. The player is given six letters and is > tasked with unscrambling those letters into as many words as possible. If the > player can use all six letters in a word, they proceed to the next round. > > Your task is to build the back-end engine to run a TextTwist clone. Effectively, > this means that you must generate a list of three- to six-letter words that can > all be constructed from the same six letters. This list must contain at least > one six-letter word. > > Bonus points for building a completely functional game! > > [1]: http://games.yahoo.com/games/texttwist.html (just one example, java) > [2]: http://www.gamehouse.com/ class String def chars split("") end def sorted chars.sort.join end end # Generate combinations. def comb array, n, str = "", &blk 0.upto(array.size - n){|i| if 1 == n yield str + array[i] else comb array[i+1..-1], n-1, str+array[i], &blk end } end word_groups = Hash.new {[]} shorts = Hash.new {[]} while word = gets do next unless (word=word.downcase.delete('^a-z')).size.between?(3,6) if 6 == word.size word_groups[word.sorted] += [ word ] else shorts[word.sorted] += [ word ] end end word_groups.each_key{|key| 3.upto(5){|n| combinations = [] comb( key.chars, n ){|s| combinations << s} combinations.uniq.each{|s| word_groups[key] += shorts[s] }}}