[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Count and Say (#138

James Gray

9/6/2007 12:00:00 PM

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.rub...

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 Martin DeMello

Conway's "Look and Say" sequence
(http://en.wikipedia.org/wiki/Look-and-sa...) is a sequence of numbers in
which each term "reads aloud" the digits of the previous term. For instance, the
canonical L&S sequence starts off 1, 11, 21, 1211, 111221, ..., because:

* 1 is read off as "one 1" or 11.
* 11 is read off as "two 1's" or 21.
* 21 is read off as "one 2, then one 1" or 1211.
* 1211 is read off as "one 1, then one 2, then two 1's" or 111221.
* 111221 is read off as "three 1, then two 2, then one 1" or 312211.

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 plural
form. Thus, seeding the sequence with "LOOK AND SAY", we get:

0. LOOK AND SAY
1. TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y
2. ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W ONE Y
3. ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE T ONE V
THREE W ONE X ONE Y

and so on. (Note the difference between this and the L&S sequence--the letters
are counted rather than read in order). Eric wants to know when the sequence
enters a cycle, and how long that cycle is. Well?

18 Answers

Phrogz

9/6/2007 7:36:00 PM

0

Ruby Quiz wrote:
> 0. LOOK AND SAY
> 1. TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y
> 2. ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W ONE Y
> 3. ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE T ONE V
> THREE W ONE X ONE Y
>
> and so on. (Note the difference between this and the L&S sequence--the letters
> are counted rather than read in order). Eric wants to know when the sequence
> enters a cycle, and how long that cycle is. Well?

In case the string gets quite long, what is the canonical English
representation (for this quiz) of:
101 - "One hundred one" or "One hundred and one"? (Not "One Oh One", I
suppose.)

(From that answer I suppose I can extrapolate the answer for similar
edge cases, largely involving the presence of absence of the word
'and'.)

Alex LeDonne

9/6/2007 7:50:00 PM

0

On 9/6/07, Phrogz <phrogz@mac.com> wrote:
> Ruby Quiz wrote:
> > 0. LOOK AND SAY
> > 1. TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y
> > 2. ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W ONE Y
> > 3. ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE T ONE V
> > THREE W ONE X ONE Y
> >
> > and so on. (Note the difference between this and the L&S sequence--the letters
> > are counted rather than read in order). Eric wants to know when the sequence
> > enters a cycle, and how long that cycle is. Well?
>
> In case the string gets quite long, what is the canonical English
> representation (for this quiz) of:
> 101 - "One hundred one" or "One hundred and one"? (Not "One Oh One", I
> suppose.)
>
> (From that answer I suppose I can extrapolate the answer for similar
> edge cases, largely involving the presence of absence of the word
> 'and'.)

I was taught in the US that "and" in a spelled out number designates
the decimal point. So, "ONE HUNDRED ONE" would be 101, while "ONE
HUNDRED AND ONE TENTH" would be 100.1 - "ONE HUNDRED AND ONE" would
then be considered an incorrect designation. I think this is based on
check-writing/banking standards.

But I have heard that others learned by other standards both in the US
and elsewhere.

-A

Martin DeMello

9/6/2007 8:15:00 PM

0

On 9/7/07, Phrogz <phrogz@mac.com> wrote:
>
> In case the string gets quite long, what is the canonical English
> representation (for this quiz) of:
> 101 - "One hundred one" or "One hundred and one"? (Not "One Oh One", I
> suppose.)

I'd say "ONE HUNDRED AND ONE", but you could always add it as a parameter

martin

Chris Shea

9/6/2007 8:32:00 PM

0

On Sep 6, 1:35 pm, Phrogz <phr...@mac.com> wrote:
> In case the string gets quite long, what is the canonical English
> representation (for this quiz) of:
> 101 - "One hundred one" or "One hundred and one"? (Not "One Oh One", I
> suppose.)

I've always felt that the answer to the question "What is the smallest
positive integer spelled with the letter 'a'?" is 1,000.

Chris

James Gray

9/7/2007 1:23:00 AM

0

On Sep 6, 2007, at 2:40 PM, Phrogz wrote:

> Ruby Quiz wrote:
>> 0. LOOK AND SAY
>> 1. TWO A ONE D ONE K ONE L ONE N TWO O ONE S ONE Y
>> 2. ONE A ONE D SIX E ONE K ONE L SEVEN N NINE O ONE S TWO T TWO W
>> ONE Y
>> 3. ONE A ONE D TEN E TWO I ONE K ONE L TEN N NINE O THREE S THREE
>> T ONE V
>> THREE W ONE X ONE Y
>>
>> and so on. (Note the difference between this and the L&S sequence--
>> the letters
>> are counted rather than read in order). Eric wants to know when
>> the sequence
>> enters a cycle, and how long that cycle is. Well?
>
> In case the string gets quite long, what is the canonical English
> representation (for this quiz) of:
> 101 - "One hundred one" or "One hundred and one"? (Not "One Oh One", I
> suppose.)
>
> (From that answer I suppose I can extrapolate the answer for similar
> edge cases, largely involving the presence of absence of the word
> 'and'.)

This has come up in past quizzes. Here's a line of thought from and
oldy but goody:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

James Edward Gray II

werner

9/7/2007 7:18:00 AM

0

Il Thu, 6 Sep 2007 21:00:20 +0900, Ruby Quiz ha scritto:

> 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.

I hope what I'm writing doesn't break this rule ^^

A couple of newbie questions: I understand that Ruby hashes copy both the
key and the value in their internal table, is this right? And if it is, is
there a way to avoid copying that key, especially if the key is a String
(or a library that implements a different hash system?)

Second question: is the value always stored as a pointer to the existing
object, or is a new object also created for the hash table?

And third... (now you are all going to realize what kind of languages I
used to program in ^^;;) I understand that the object_id is somehow
connected to the pointer to the actual structure holding object data, if it
isn't the pointer itself. Is there a way to use it as an actual object
pointer? (I guess there isn't, but....)


Thanks in advance

--
Dawn is near, the time is short. (Human aircraft! Hard to port!)
We eight reindeers of the night work as one in silent flight.
Let not one damn house pass by. (Who taught that guy how to fly?)
Each year brings more girls and boys. How many of them can we bring toys?

Mark Thomas

9/8/2007 5:54:00 PM

0


> This has come up in past quizzes. Here's a line of thought from and
> oldy but goody:
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>
> James Edward Gray II

Hehe... I used that for Integer#to_english. That made solving this
Ruby Quiz fairly easy. Can I post my solution now?

James Gray

9/8/2007 6:59:00 PM

0

On Sep 8, 2007, at 12:55 PM, Mark Thomas wrote:

>
>> This has come up in past quizzes. Here's a line of thought from and
>> oldy but goody:
>>
>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>>
>> James Edward Gray II
>
> Hehe... I used that for Integer#to_english. That made solving this
> Ruby Quiz fairly easy. Can I post my solution now?

Sure can.

James Edward Gray II

Josef 'Jupp' Schugt

9/8/2007 8:37:00 PM

0



Josef 'Jupp' Schugt

9/9/2007 10:10:00 AM

0

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 of a
> > sentence 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-schu...
PGP key with id 6CC6574F available at http://wwwkeys.d...