William James wrote: > John Carter wrote: > > Assuming you are running Linux and have a list of valid English words in > > the file /usr/share/dict/words or someplace or something similar. > > > > Assume you have a cellphone with T9 predictive text input. > > > > For those that don't, cellphones have each key overloaded to mean several > > things. eg. On my Sharp GX-15 > > 2 also means A,B,C > > 3 also means D,E,F > > 4 also means G,H,I > > 5 also means J,K,L > > 6 = M,N,O > > 7 = P,Q,R,S > > 8 = T,U,V > > 9 = W,X,Y,Z > > > > In T9 mode I can type in 43556 and it only finds one word in its > > dictionary that can be made with combination > > {G,H,I}{D,E,F}{J,K,L}{J,K,L}{M,N,O} and that is > > HELLO > > > > Which is a lost faster than typing 4433555555666. > > > > How ever, some words like "high", can be entered by just bouncing on one > > key ... 4444. > > > > Your mission, should you choose to accept, is to write the shortest ruby > > program that will find the longest word in the dictionary that can be > > produced by... > > * repeatedly typing one key > > * Can be produced by just using only two keys. > > * Can be produced by just using only three keys. > > * Can be produced by just using only four keys. > > * .... > > Assumes that ambiguity is disallowed; rejects words with apostrophes. > > h=Hash.new(0) > a=ARGF.grep(/^\w+$/).sort_by{|x|-x.size}.map{|w|w.chomp!.downcase! > c=w.split('').map{|c|2+"abc def ghi jkl mno pqrstuv wxyz".index(c)/4} > h[c]+=1;[w,c]}.reject{|w,c|h[c]>1} > (1..8).each{|n|p [n,a.find{|w,c|n==c.uniq.size}[0]]} > > > [1, "deeded"] > [2, "blackjack"] > [3, "openendedness"] > [4, "prepossessingness"] > [5, "contemporaneousness"] > [6, "microspectrophotometric"] > [7, "antidisestablishmentarianism"] > [8, "microspectrophotometrically"] Shorter: h=Hash.new(0) a=ARGF.grep(/^\w+$/).sort_by{|x|-x.size}.map{|w|w.chomp!.downcase! c=w.split('').map{|c|2+"abc def ghi jkl mno pqrstuv wxyz".index(c)/4} h[c]+=1;[w,c]} (1..8).each{|n|p [n,a.find{|w,c|1==h[c]&&n==c.uniq.size}[0]]}