"Ruby Quiz" <james / grayproductions.net> wrote in message > The three rules of Ruby Quiz: > > by Morton Goldberg > > Your mission is to pick a word list in some language (it will have be one > that > uses roman letters) and write Ruby code to filter the list to extract all > the > words which are valid numbers in a given base. For many bases this isn't > an > interesting task--for bases 2-10, the filter comes up empty; for bases > 11-13, > the filter output is uninteresting (IMO); for bases approaching 36, the > filter > passes almost everything (also uninteresting IMO). However, for bases in > the > range from 14 to about 22, the results can be interesting and even > surprising, > especially if one constrains the filter to accept only words of some > length. > Here are my 4 solutions :) (all use ?, so they will not work in 1.9) # solution #1 - Simple one-liner p File.read(ARGV[0]).split("\n").reject{|w| w !~ %r"^[a-#{(?a-11+ARGV[1].to_i).chr}]+$"}.sort_by{|w| [w.length,w]} if (?a...?z)===?a-11+ARGV[1].to_i # solution #2 - Non-hackery substs, like Olaf p File.read(ARGV[0]).split("\n").reject{|w| w !~ %r"^[a-#{(?a-11+ARGV[1].to_i).chr}|lO]+$"}.sort_by{|w| [w.length,w]} if (?a...?k)===?a-11+ARGV[1].to_i # solution #3 - c001 hackerz p File.read(ARGV[0]).split("\n").reject{|w| w !~ %r"^[a-#{(?a-11+ARGV[1].to_i).chr}|lo]+$"i}.map{|w| w.downcase.gsub('o','0').gsub('l','1')}.sort_by{|w| [w.length,w]} if (?a...?k)===?a-11+ARGV[1].to_i # solution #4 - B16 5H0UT1N6 HACKER2 base=ARGV[1].to_i base_=base+?a-11 raise "Bad base: [#{base}]" if base<1 || base_>?z sub0=base_ < ?o sub1=base>1 && base_ < ?l sub2=base>2 && base_ < ?z sub5=base>5 && base_ < ?s sub6=base>6 && base_ < ?g sub8=base>8 && base_ < ?b reg="^[" reg<<'O' if sub0 reg<<'I' if sub1 reg<<'Z' if sub2 reg<<'S' if sub5 reg<<'G' if sub6 reg<<'B' if sub8 reg<<"|a-#{base_.chr}" if base>10 reg<<']+$' result=File.read(ARGV[0]).split("\n").reject{|w| w !~ %r"#{reg}"i}.map{|w| w.upcase}.sort_by{|w| [w.length,w]} result.map!{|w| w.gsub('O','0')} if sub0 result.map!{|w| w.gsub('I','1')} if sub1 result.map!{|w| w.gsub('Z','2')} if sub2 result.map!{|w| w.gsub('S','5')} if sub5 result.map!{|w| w.gsub('G','6')} if sub6 result.map!{|w| w.gsub('B','8')} if sub8 result.reject!{|w| w !~ /[A-Z]/} # NUM8ER5-0NLY LIKE 61885 ARE N0T READA8LE p result