So I'm taking a class on Ruby and it seems as though the teacher has
thrown something over my head.
List how many times each number from 0 to 9 was mentioned.
Example input:
> ruby most-common.rb 4 2 1 1 1 2
Example output:
1 was mentioned 3 time(s).
2 was mentioned 2 time(s).
4 was mentioned 1 time(s).
The numbers in the command-line arguments must always be between 0 and
9, inclusive.
Do I loop over each element of an array of ARGV's and test to see if
each new element of the array is equal to any of the others. And if so,
do I set up a count?
Since this is classwork, I won't give you the answer, but. Here's some things I
would do (and trust me there are *much* smarter people on this list), but I
would do probably use the case statement here. Seems to be a logical holding
point for things.
def sort_numbers(argv_input)
case = argv_input
when "1" one =+1
etc...
end
numbers = ARGV.split(" ")
numbers.each do |num|
sort_numbers(num)
### put your print routine here ###
end
Anyhow, that's how I would tackle it. But I'm sure there are better ways to code
this type of thing up.
Wayne
···
----- Original Message ----
From: Zebulon Bowles <lists@ruby-forum.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Tue, January 29, 2013 2:41:28 PM
Subject: newbie question..
So I'm taking a class on Ruby and it seems as though the teacher has
thrown something over my head.
List how many times each number from 0 to 9 was mentioned.
Example input:
> ruby most-common.rb 4 2 1 1 1 2
Example output:
1 was mentioned 3 time(s).
2 was mentioned 2 time(s).
4 was mentioned 1 time(s).
The numbers in the command-line arguments must always be between 0 and
9, inclusive.
Do I loop over each element of an array of ARGV's and test to see if
each new element of the array is equal to any of the others. And if so,
do I set up a count?
For the same reason, I won't give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash...think about it and ask questions. We are all here to
help, but we wouldn't want it to be too easy for you
-Ryan Victory
···
On 1/29/13 2:50 PM, Wayne Brisette wrote:
Since this is classwork, I won't give you the answer, but. Here's some things I
would do (and trust me there are *much* smarter people on this list), but I
would do probably use the case statement here. Seems to be a logical holding
point for things.
def sort_numbers(argv_input)
case = argv_input
when "1" one =+1
etc...
end
numbers = ARGV.split(" ")
numbers.each do |num|
sort_numbers(num)
### put your print routine here ###
end
Anyhow, that's how I would tackle it. But I'm sure there are better ways to code
this type of thing up.
Wayne
----- Original Message ----
From: Zebulon Bowles <lists@ruby-forum.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Tue, January 29, 2013 2:41:28 PM
Subject: newbie question..
So I'm taking a class on Ruby and it seems as though the teacher has
thrown something over my head.
List how many times each number from 0 to 9 was mentioned.
Example input:
> ruby most-common.rb 4 2 1 1 1 2
Example output:
1 was mentioned 3 time(s).
2 was mentioned 2 time(s).
4 was mentioned 1 time(s).
The numbers in the command-line arguments must always be between 0 and
9, inclusive.
Do I loop over each element of an array of ARGV's and test to see if
each new element of the array is equal to any of the others. And if so,
do I set up a count?
Ok, I admit I'm stumped -- I don't get how to use a Hash inside an
Array.inject -- I'm thinking it must be able to be done that way, but
I just cannot grok it.
#inject/#reduce/#fold are accumulators, but how does the Hash get
passed from round to round?
···
On Tue, Jan 29, 2013 at 2:52 PM, Ryan Victory <ryan@raptormail.net> wrote:
For the same reason, I won't give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash...think about it and ask questions. We are all here to
help, but we wouldn't want it to be too easy for you
On Tue, Jan 29, 2013 at 8:45 PM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:
On Tue, Jan 29, 2013 at 2:52 PM, Ryan Victory <ryan@raptormail.net> wrote:
For the same reason, I won't give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash...think about it and ask questions. We are all here to
help, but we wouldn't want it to be too easy for you
Ok, I admit I'm stumped -- I don't get how to use a Hash inside an
Array.inject -- I'm thinking it must be able to be done that way, but
I just cannot grok it.
#inject/#reduce/#fold are accumulators, but how does the Hash get
passed from round to round?
Out of curiosity, what did you do to get it working? I played around in
irb after sending that (it was a quick guess, not anything I tried
first) and ended up using two lines, both with
Enumerable#each...couldn't get the hash to be used as the accumulator.
-Ryan Victory
···
On 1/29/13 8:55 PM, tamouse mailing lists wrote:
On Tue, Jan 29, 2013 at 8:45 PM, tamouse mailing lists > <tamouse.lists@gmail.com> wrote:
On Tue, Jan 29, 2013 at 2:52 PM, Ryan Victory <ryan@raptormail.net> wrote:
For the same reason, I won't give you any answers, but your problem
could probably be solved using about two lines with the Array.inject
method and a Hash...think about it and ask questions. We are all here to
help, but we wouldn't want it to be too easy for you
Ok, I admit I'm stumped -- I don't get how to use a Hash inside an
Array.inject -- I'm thinking it must be able to be done that way, but
I just cannot grok it.
#inject/#reduce/#fold are accumulators, but how does the Hash get
passed from round to round?
On Wed, Jan 30, 2013 at 9:38 AM, Ryan Victory <ryan@raptormail.net> wrote:
Out of curiosity, what did you do to get it working? I played around in
irb after sending that (it was a quick guess, not anything I tried
first) and ended up using two lines, both with
Enumerable#each...couldn't get the hash to be used as the accumulator.
-Ryan Victory
On 1/29/13 8:55 PM, tamouse mailing lists wrote:
> On Tue, Jan 29, 2013 at 8:45 PM, tamouse mailing lists > > <tamouse.lists@gmail.com> wrote:
>> On Tue, Jan 29, 2013 at 2:52 PM, Ryan Victory <ryan@raptormail.net> > wrote:
>>> For the same reason, I won't give you any answers, but your problem
>>> could probably be solved using about two lines with the Array.inject
>>> method and a Hash...think about it and ask questions. We are all here
to
>>> help, but we wouldn't want it to be too easy for you
>>
>> Ok, I admit I'm stumped -- I don't get how to use a Hash inside an
>> Array.inject -- I'm thinking it must be able to be done that way, but
>> I just cannot grok it.
>>
>> #inject/#reduce/#fold are accumulators, but how does the Hash get
>> passed from round to round?
> Never mind, I got it...
>
I forgot one of ruby's basic things. It always returns the last value
in the block.
···
On Tue, Jan 29, 2013 at 10:08 PM, Ryan Victory <ryan@raptormail.net> wrote:
Out of curiosity, what did you do to get it working? I played around in
irb after sending that (it was a quick guess, not anything I tried
first) and ended up using two lines, both with
Enumerable#each...couldn't get the hash to be used as the accumulator.