Just a simple regex, the rest is just option parsing.
#!/usr/bin/env ruby -wKU
require "optparse"
options = {
:base => 16,
:min_length => 1,
:word_file => "/usr/share/dict/words",
:case_insensitive => false
}
ARGV.options do |opts|
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [OPTIONS]"
opts.separator ""
opts.separator "Specific Options:"
opts.on( "-b", "--base BASE", Integer,
"Specify base (default #{options[:base]})" ) do |base|
options[:base] = base
end
opts.on( "-l", "--min-word-length LENGTH", Integer,
"Specify minimum length" ) do |length|
options[:min_length] = length
end
opts.on( "-w", "--word-file FILE",
"Specify word file",
"(default #{options[:word_file]})" ) do |word_file|
options[:word_file] = word_file
end
opts.on( "-i", "--ignore-case",
"Ignore case distinctions in word file." ) do |i|
options[:ignore_case] = true
end
opts.separator "Common Options:"
opts.on( "-h", "--help",
"Show this message." ) do
puts opts
exit
end
begin
opts.parse!
rescue
puts opts
exit
end
end
last_letter = (options[:base] - 1).to_s(options[:base])
letters = ("a"..last_letter).to_a.join
exit if letters.size.zero?
criteria = Regexp.new("^[#{letters}]{#{options[:min_length]},}$",
options[:ignore_case])
open(options[:word_file]).each do |word|
puts word if word =~ criteria
end
On Aug 3, 6:01 am, Ruby Quiz <ja... / grayproductions.net> 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 Morton Goldberg
>
> When working with hexadecimal numbers it is likely that you've noticed some hex
> numbers are also words. For example, 'bad' and 'face' are both English words and
> valid hex numbers (2989 and 64206, respectively, in decimal). I got to thinking
> that it would be interesting to find out how many and which hex numbers were
> also valid English words. Of course, almost immediately I started to think of
> generalizations. What about other bases? What about languages other than
> English?
>
> 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.
>
> I used `/usr/share/dict/words` for my word list. Participants who don't have
> that list on their system or want a different one can go to Kevin's Word List
> Page (http://wordlist.sourceforge.net/) as a source of other word lists.
>
> Some points you might want to consider: Do you want to omit short words like 'a'
> and 'ad'? (I made word length a parameter). Do you want to allow capitalized
> words (I prohibited them)? Do you want to restrict the bases allowed (I didn't)?