--Sig_AnIJjWoroPpZQ`S_rUPc6 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable On Sun, 9 Sep 2007 05:37:02 +0900 Josef 'Jupp' Schugt wrote: > Seems as if something got broken 8-| > On Thu, 6 Sep 2007 21:00:20 +0900 Ruby Quiz wrote: > > > Over on rec.puzzles, Eric A. proposed a variant in which the letters ofentence are grouped, then "counted aloud", omitting the "s"s for the plural > > form. > > Here is my solution. The idea is as follows: > > 1. Add an instance method "count" to "String" that builds a hash assigning > each letter present in the string its frequency. > > 2. Add an instance method "say" to Fixnum and Hash that 'says' a number or a > hashes of the abovementioned structure. > > 3. Push what is said until what is currently said has been said before. > > Saying numbers is only implemented for values from 0 through 999_999_999 and > without the use of "and". The latter is because I personally do not use an > "and". The former is because starting with 1_000_000_000 all hell breaks loose > and a crore of different ways to read the numbers enter the scene. > > ############################################################################### > # Ruby Quiz 138, Josef 'Jupp' Schugt > ############################################################################### > > # String#count creates a hash where the keys are the letters 'a' > # through 'z' and the values are the frequencies of these letters. > > class String > def count > a = Hash.new(0) > self.each_byte{|i| a[i.chr] += 1 if ('a'..'z').member?(i.chr)} > a > end > end > > # Hash#say "reads the hash aloud" > > class Hash > def say > self.sort.map{|n,c| "#{c.say} #{n}"}.join(' ') > end > end > > # Fixnum#say "reads the number aloud" > > class Fixnum > > # Lookup table for numbers from zero to nineteen > @@to20 = [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', > 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', > 'thirteen', 'fourteen', 'fivteen', 'sixteen', > 'seventeen', 'eighteen', 'nineteen' ] > > # Looup table for tens with the first two values only being fillers. > @@tens = [ nil, nil, 'twenty', 'thirty', 'forty', 'fifty', 'sixty', > 'seventy', 'eighty', 'ninety' ] > > def say > name = > case self > when 0...20: > @@to20[self] > when 20...100: > @@tens[self / 10 ] + '-' + @@to20[self % 10] > when 100...1000: > (self / 100).say + ' hundred ' + (self % 100).say > when 1000...1_000_000: > (self / 1000).say + ' thousand ' + (self % 1000).say > when 1_000_000...1_000_000_000: > (self / 1_000_000).say + ' million ' + (self % 1_000_000).say > else > raise ArgumentError, 'Only numbers from 0 to 999_999_999 are > supported' end > /[- ]zero$/.match(name) ? $~.pre_match : name > end > end > > puts <<EOF > > PLEASE ENTER A STRING TO START WITH AND NOTE THAT: > > 1. No output will be shown until processing is done. > 2. The string will be first downcased. > 3. All whitespace will be collapsed to single spaces. > 4. All characters except 'a' through 'z' and space are removed. > > EOF > > print "? " > s = gets.chomp.downcase.gsub(/\s+/, ' ').gsub(/[^a-z ]/, '') > > arr = Array.new > > until arr.member?(s) > arr.push s > s = s.count.say > end > > puts <<EOF > > VALUES BEFORE FIRST CYCLE > > #{(0...arr.index(s)).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} > > VALUES OF FIRST CYCLE > > #{(arr.index(s)...arr.length).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} > > SUMMARY > > Initial value (step #{0}) is '#{arr.first}' > > First cycle: > > \tFirst step:\t#{arr.index(s)} > \tLast step:\t#{arr.length - 1} > \tPeriod (steps):\t#{arr.length - arr.index(s)} > > EOF > > > Josef 'Jupp' Schugt 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/ --Sig_AnIJjWoroPpZQ`S_rUPc6 Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFG48ZWrhv7B2zGV08RAtdcAKDh5K12s/C00Z+thueKRMB7vpru6ACcD23Q t0u6tXfZn4hXO1swfDGnu2k Bu -----END PGP SIGNATURE----- --Sig_AnIJjWoroPpZQ`S_rUPc6--