Forum: Ruby newbie question..

Posted by Zebulon Bowles (zebulono)
on 2013-01-29 21:40
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?

numbers = ARGV

   for number in numbers
        if number > 0 && < 9
         ???????????????????

end

puts "#{number} was mentioned #{count} times?

AHHHGH!!!!
Posted by Wayne Brisette (Guest)
on 2013-01-29 21:51
(Received via mailing list)
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?

numbers = ARGV

   for number in numbers
        if number > 0 && < 9
         ???????????????????

end

puts "#{number} was mentioned #{count} times?

AHHHGH!!!!
Posted by Ryan Victory (Guest)
on 2013-01-29 21:53
(Received via mailing list)
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
Posted by Scott RubyGuy (scottr)
on 2013-01-29 21:54
There are tons of examples on the web (and this forum) as to how to 
count the frequency of an enumerable, string, etc...

Here's a hint - look at the Hash type. Understand key-value pairs and 
how to setup default values.
Posted by Wayne Brisette (Guest)
on 2013-01-29 22:02
(Received via mailing list)
And the minute I saw hash, a new answer dawned on me... ;) That's why I 
enjoy
reading this list, I'm always reminded of easier ways to do things.


----- Original Message ----
From: Scott RubyGuy <lists@ruby-forum.com>


There are tons of examples on the web (and this forum) as to how to
count the frequency of an enumerable, string, etc...

Here's a hint - look at the Hash type. Understand key-value pairs and
how to setup default values.
Posted by Zebulon Bowles (zebulono)
on 2013-01-29 22:12
I guess it's time for me to jump ahead of our current skill-set!
Posted by Joel Pearson (virtuoso)
on 2013-01-30 00:24
There's always the option of handy methods like split, uniq, scan, and 
length. I did it one line using a combination of those (and sort for 
neatness).
Posted by tamouse mailing lists (Guest)
on 2013-01-30 03:46
(Received via mailing list)
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?
Posted by tamouse mailing lists (Guest)
on 2013-01-30 03:56
(Received via mailing list)
On Tue, Jan 29, 2013 at 8:45 PM, tamouse mailing lists
<tamouse.lists@gmail.com> wrote:
>
> #inject/#reduce/#fold are accumulators, but how does the Hash get
> passed from round to round?

Never mind, I got it...
Posted by Ryan Victory (Guest)
on 2013-01-30 05:09
(Received via mailing list)
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
Posted by Prasadhnc C (Guest)
on 2013-01-30 07:04
(Received via mailing list)
u can try this,

string = 'hello'
string.split('').each.inject(Hash.new(0)) { |h, char| h[char] += 1; h }

Prasad C
Posted by tamouse mailing lists (Guest)
on 2013-01-31 05:26
(Received via mailing list)
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.

I forgot one of ruby's basic things. It always returns the last value
in the block.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.