From: Bob Showalter Date: 2005-11-02T04:10:12+09:00 Subject: [QUIZ][SOLUTION] TumbleDRYer (#53) My approach was to look for repeated "phrases" made up of one or more "words" within a line. Running my solution against the sample input data produces the following output: class WashingMachine def initialize @id = '`id` int(11) NOT NULL auto_increment,' @va = 'varchar(50) NOT NULL default \'\',' @in = 'int(11) NOT NULL default \'0\',' @no = 'NOT NULL default' @ty = ') TYPE=MyISAM' @de = '`description`' @cr = 'CREATE TABLE' @pr = 'PRIMARY KEY' end def output print <= MIN_PHRASE phr[p] ||= 0 phr[p] += 1 end end # get the longest phrase that occurs the most times longest = phr.sort_by { |k,v| -(k.length * 1000 + v) }.find { |k,v| v >= MIN_OCCUR } or break phrase, occurs = longest # save the phrase, and then blank it out of the input data # so we can search for more phrases phrases << phrase lines.each { |line| line.gsub!(phrase, ' ' * phrase.length) } end # now we have all the phrases we want to replace. # find unique abbreviations for each phrase. temp = Hash.new phrases.each do |phrase| key = phrase.scan(/\w+/).flatten.to_s.downcase key = '_' + key unless key =~ /^[_a-zA-Z]/ key += '_' while temp.has_key? key temp[key] = phrase end temp.keys.abbrev.sort.each do |s, key| phrase = temp[key] abbr[phrase] = s if abbr[phrase].nil? || abbr[phrase].length < MIN_ABBR end # generate the output class puts "class WashingMachine" puts " def initialize" phrases.each do |phrase| puts ' @' + abbr[phrase] + " = '" + phrase.gsub("'", "\\\\'") + "'" @input.gsub!(phrase, '#{@' + abbr[phrase] + '}') end puts " end\n" puts " def output\nprint <