From: John Carter Date: 2005-10-07T05:25:29+09:00 Subject: Re: Ruby Golf on Lazy T9 Words. On Thu, 6 Oct 2005, wannes wrote: > On 06/10/05, John Carter wrote: > If you wanted this to be a rubyquiz, you better could have send it to > James Edward Gray II. Didn't even occur to me. Just thought it was a nice simple problem to which I personally wanted the answer. Since I thought it would be fun, I wondered if anyone else would have fun with it. RubyQuiz must have been somewhere in my subconscious since I borrowed his "wish to accept" tag line. > A similar quiz has allready been done in the > past. Check out http://rubyquiz.com and look around. There was a quiz to design a "better than multitap" algorithm, not given T9, what is the longest word with shortest taps. I started crafting a solution to my problem when I got lost with wheels in wheels in wheels and stopped. I then went for a walk to buy milk and had the notion that regexes would be good.... ========================================================= a=Hash.new('') IO.read('/usr/share/dict/words').scan(/[a-z]+/i){|m| x={} m.upcase.tr( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '22233344455566677778889999').each_byte{|c| x[c]=c } z=x.keys.size i=a[z] a[z]=m if i.size < m.size } p a ======================================================= But than gave just the one example, so I extended it to give me all words of the maximum length.. ============================================================= # I love the flexibility of the Ruby hash constructor. I use that # feature all the time. a=Hash.new(['']) # Hah! I didn't even know about ARGF. Ah well, I learn something # new every day! # I should of used \w instead of [a-z], that would have been shorter. IO.read('/usr/share/dict/words').scan(/[a-z]+/i){|m| x={} # I chose upcase instead of downcase, saved me 2 chars. ;-) # Hmm, perhaps I should have done the tr before the scan. m.upcase.tr( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '22233344455566677778889999').each_byte{|c| x[c]=c } # Somehow I sure if I was smarter there would be a shorter way of # finding the number of uniq chars in a string. Especially # as I could make those chars whatever I like. z=x.keys.size i=a[z] if i[0].size < m.size a[z]=[m] elsif i[0].size == m.size # Now this is actually quite subtle. # Who says ruby doesn't have pointers? # I have another fun problem in my head that relates to that. i << m end } a.keys.sort.each{|k| puts "#{k}\n\t#{a[k].sort.uniq.join("\n\t")}\n"} The result is... 1 deeded 2 blackball blackjack depressed depresses preferred redressed redresses repressed represses 3 shepherdesses 4 presumptuousness 5 transubstantiation 6 contradistinctions disenfranchisement disproportionating hypersensitivities misinterpretations misrepresentations 7 counterrevolutionaries electroencephalographs 8 counterrevolutionary uncharacteristically It is interesting that the 8 key words are shorter than the 7. John Carter The Cybernetic Entomologist - cyent@xtra.co.nz http://geocities.yahoo.com/cy_ent I'm becoming less and less convinced of humans as rational beings. I suspect we are merely meme collectors, and the reason meme is only kept on to help count our change. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Carter's Clarification of Murphy's Law. "Things only ever go right so that they may go more spectacularly wrong later." From this principle, all of life and physics may be deduced.