Re: Numbers Can Be Words (#133)

I found this Quiz to be extremely easy. I wrote this solution in a
minute or two:

dict = File.open(“wordlist.txt”){|f| f.readlines.reject {|word|
(‘A’…‘Z’) === word[0,1]}}

base = ARGV[0].to_i

puts dict.select{|word| word.unpack(“C*”).select{|char|
char > ?a+base-11}.empty?}

----- Original Message ----
From: Ruby Q. [email protected]
To: ruby-talk ML [email protected]
Sent: Friday, August 3, 2007 8:01:18 AM
Subject: [QUIZ] Numbers Can Be Words (#133)

The three rules of Ruby Q.:

  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 Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

  1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion. Please reply to the original quiz
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Morton G.

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)?

____________________________________________________________________________________Ready
for the edge of your seat?
Check out tonight’s top picks on Yahoo! TV.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

James K. wrote:

I found this Quiz to be extremely easy. I wrote this solution in a
minute or two:

dict = File.open(“wordlist.txt”){|f| f.readlines.reject {|word|
(‘A’…‘Z’) === word[0,1]}}

base = ARGV[0].to_i

puts dict.select{|word| word.unpack(“C*”).select{|char|
char > ?a+base-11}.empty?}

Indeed. Here’s my two-liner. ARGV[0] is the base to be used, ARGV[1] is
the file to be processed.

p = /^[#{“0123456789abcdefghijklmnopqrstuvwxyz”[0…ARGV[0].to_i]}]+$/i
puts File.open(ARGV[1]).readlines.reject!{|l| l !~ p}

I do not reject words that contain digits. That would reject valid
Internet slang like “l8er” or “gn8”, leetspeak like “1337” or “H4X0R”,
and acronyms like I18N or L10N.

Josef ‘Jupp’ Schugt


Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/
PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFGtfavrhv7B2zGV08RAg8MAJ0a592lTGVyH636SdZzcK8qDP3AogCgnIBZ
+CcWYwTWoMIP7XmSwqYG3OE=
=rKWG
-----END PGP SIGNATURE-----