--Sig_AnIJjWoroPpZQ=6HS_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: >=20 Seems as if something got broken 8-| > On Thu, 6 Sep 2007 21:00:20 +0900 Ruby Quiz wrote: >=20 > > Over on rec.puzzles, Eric A. proposed a variant in which the letters of= a > > sentence are grouped, then "counted aloud", omitting the "s"s for the p= lural > > form. =20 >=20 > Here is my solution. The idea is as follows: >=20 > 1. Add an instance method "count" to "String" that builds a hash assigning > each letter present in the string its frequency. >=20 > 2. Add an instance method "say" to Fixnum and Hash that 'says' a number o= r a > hashes of the abovementioned structure. >=20 > 3. Push what is said until what is currently said has been said before. >=20 > 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. >=20 > #########################################################################= ###### > # Ruby Quiz 138, Josef 'Jupp' Schugt > #########################################################################= ###### >=20 > # String#count creates a hash where the keys are the letters 'a' > # through 'z' and the values are the frequencies of these letters. >=20 > class String > def count > a =3D Hash.new(0) > self.each_byte{|i| a[i.chr] +=3D 1 if ('a'..'z').member?(i.chr)} > a > end > end >=20 > # Hash#say "reads the hash aloud" >=20 > class Hash > def say > self.sort.map{|n,c| "#{c.say} #{n}"}.join(' ') > end > end >=20 > # Fixnum#say "reads the number aloud" >=20 > class Fixnum >=20 > # Lookup table for numbers from zero to nineteen > @@to20 =3D [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', > 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', > 'thirteen', 'fourteen', 'fivteen', 'sixteen', > 'seventeen', 'eighteen', 'nineteen' ] >=20 > # Looup table for tens with the first two values only being fillers. > @@tens =3D [ nil, nil, 'twenty', 'thirty', 'forty', 'fifty', 'sixty', > 'seventy', 'eighty', 'ninety' ] >=20 > def say > name =3D=20 > 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 >=20 > puts <<EOF >=20 > PLEASE ENTER A STRING TO START WITH AND NOTE THAT: >=20 > 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. >=20 > EOF >=20 > print "? " > s =3D gets.chomp.downcase.gsub(/\s+/, ' ').gsub(/[^a-z ]/, '') >=20 > arr =3D Array.new >=20 > until arr.member?(s) > arr.push s > s =3D s.count.say > end >=20 > puts <<EOF >=20 > VALUES BEFORE FIRST CYCLE >=20 > #{(0...arr.index(s)).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} >=20 > VALUES OF FIRST CYCLE >=20 > #{(arr.index(s)...arr.length).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} >=20 > SUMMARY >=20 > Initial value (step #{0}) is '#{arr.first}' >=20 > First cycle: >=20 > \tFirst step:\t#{arr.index(s)} > \tLast step:\t#{arr.length - 1} > \tPeriod (steps):\t#{arr.length - arr.index(s)} >=20 > EOF >=20 >=20 > Josef 'Jupp' Schugt Josef 'Jupp' Schugt --=20 Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/ PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/ --Sig_AnIJjWoroPpZQ=6HS_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= =PsBu -----END PGP SIGNATURE----- --Sig_AnIJjWoroPpZQ=6HS_rUPc6-- On Sun, 9 Sep 2007 05:37:02 +0900 Josef 'Jupp' Schugt wrote: >=20 Seems as if something got broken 8-| > On Thu, 6 Sep 2007 21:00:20 +0900 Ruby Quiz wrote: >=20 > > Over on rec.puzzles, Eric A. proposed a variant in which the letters of= a > > sentence are grouped, then "counted aloud", omitting the "s"s for the p= lural > > form. =20 >=20 > Here is my solution. The idea is as follows: >=20 > 1. Add an instance method "count" to "String" that builds a hash assigning > each letter present in the string its frequency. >=20 > 2. Add an instance method "say" to Fixnum and Hash that 'says' a number o= r a > hashes of the abovementioned structure. >=20 > 3. Push what is said until what is currently said has been said before. >=20 > 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. >=20 > #########################################################################= ###### > # Ruby Quiz 138, Josef 'Jupp' Schugt > #########################################################################= ###### >=20 > # String#count creates a hash where the keys are the letters 'a' > # through 'z' and the values are the frequencies of these letters. >=20 > class String > def count > a =3D Hash.new(0) > self.each_byte{|i| a[i.chr] +=3D 1 if ('a'..'z').member?(i.chr)} > a > end > end >=20 > # Hash#say "reads the hash aloud" >=20 > class Hash > def say > self.sort.map{|n,c| "#{c.say} #{n}"}.join(' ') > end > end >=20 > # Fixnum#say "reads the number aloud" >=20 > class Fixnum >=20 > # Lookup table for numbers from zero to nineteen > @@to20 =3D [ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', > 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', > 'thirteen', 'fourteen', 'fivteen', 'sixteen', > 'seventeen', 'eighteen', 'nineteen' ] >=20 > # Looup table for tens with the first two values only being fillers. > @@tens =3D [ nil, nil, 'twenty', 'thirty', 'forty', 'fifty', 'sixty', > 'seventy', 'eighty', 'ninety' ] >=20 > def say > name =3D=20 > 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 >=20 > puts <<EOF >=20 > PLEASE ENTER A STRING TO START WITH AND NOTE THAT: >=20 > 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. >=20 > EOF >=20 > print "? " > s =3D gets.chomp.downcase.gsub(/\s+/, ' ').gsub(/[^a-z ]/, '') >=20 > arr =3D Array.new >=20 > until arr.member?(s) > arr.push s > s =3D s.count.say > end >=20 > puts <<EOF >=20 > VALUES BEFORE FIRST CYCLE >=20 > #{(0...arr.index(s)).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} >=20 > VALUES OF FIRST CYCLE >=20 > #{(arr.index(s)...arr.length).map {|i| "#{i}:\t#{arr[i]}" }.join("\n")} >=20 > SUMMARY >=20 > Initial value (step #{0}) is '#{arr.first}' >=20 > First cycle: >=20 > \tFirst step:\t#{arr.index(s)} > \tLast step:\t#{arr.length - 1} > \tPeriod (steps):\t#{arr.length - arr.index(s)} >=20 > EOF >=20 >=20 > Josef 'Jupp' Schugt Josef 'Jupp' Schugt --=20 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) iD8DBQFG48ZWrhv7B2zGV08RAtdcAKDh5K12s/C00Z+thueKRMB7vpru6ACcD23Q t0u6tXfZn4hXO1swfDGnu2k= =PsBu -----END PGP SIGNATURE-----