Count numbers - please help

Hello, can you please help me :)?

I have an array like this:

a=[1,-8,4,6]

there a positive and negative numbers in.

but now i only want to count the positive.

So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.

This is what I got so far:

def anzahl_positiv(a)

return a.count

end

puts "#{anzahl_positiv(a)}"

Would be nice if you would help me :). Thank you!

···

=======================

--
Posted via http://www.ruby-forum.com/.

You're going to have to count them. a positive number is 0 or greater, use that as your check. You could do an if statement and count the positives and elsif to count negatives, and have a nice routine to use at anytime. :wink:

Wayne

···

________________________________
From: Johanna Bettina <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Sent: Tuesday, December 17, 2013 2:25 PM
Subject: Count numbers - please help

Hello, can you please help me :)?

I have an array like this:

a=[1,-8,4,6]

there a positive and negative numbers in.

but now i only want to count the positive.

So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.

This is what I got so far:

def anzahl_positiv(a)

return a.count

end

puts "#{anzahl_positiv(a)}"

Would be nice if you would help me :). Thank you!

--
Posted via http://www.ruby-forum.com/.

Count takes a block and only counts the elements for which it is true:

irb1.8.7> a = [ 1, 2, -4, 8 ]
#1.8.7 => [1, 2, -4, 8]
irb1.8.7> a.count
#1.8.7 => 4
irb1.8.7> a.count {|e| e > 0}
#1.8.7 => 3

-Rob

Rob Biedenharn
rob.biedenharn@gmail.com

···

On 2013-Dec-17, at 15:25 , Johanna Bettina <lists@ruby-forum.com> wrote:

Hello, can you please help me :)?

I have an array like this:

a=[1,-8,4,6]

there a positive and negative numbers in.

but now i only want to count the positive.

So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.

This is what I got so far:

def anzahl_positiv(a)

return a.count

end

puts "#{anzahl_positiv(a)}"

Would be nice if you would help me :). Thank you!

--

Dear Wayne,

could you please give me the function?

I understand what you mean,but I don´t understand how to do it.

Thank you :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Rob's message contains the exact code you need.

···

--
Posted via http://www.ruby-forum.com/.

Yes, but we have to do it in a loop in a function.

So it should be:

a=[1,-5,3,6]

def anzahl (a)

and here i need the loop whicht only counts the positive numbers.

how can i do this?Would be nice to help me :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

One way to do it would be to initialise a local variable outside the
loop:

count = 0

Increment it inside the loop:

a.each { |number| count+=1 if number >= 0 }

And then return the count.

···

--
Posted via http://www.ruby-forum.com/.

Thank you so much :). That helped me! Now I understand :).

···

--
Posted via http://www.ruby-forum.com/.

a.inject(0) { |count, element| element >= 0 ? count+1 : count }

At the first iteration, count is equal to the argument of inject ( = 0 )
At each iteration count is equal to the last value returned.
The block returns the same value (count) or the incremented (count+1)
if the element is >=0.

Look at

But, the Rob Biedenharn way is the "right" way to go.

a.count {|e| e > 0}

Best regards,
Abinoam Jr.

···

On Wed, Dec 18, 2013 at 1:12 PM, Johanna Bettina <lists@ruby-forum.com> wrote:

Thank you so much :). That helped me! Now I understand :).

--
Posted via http://www.ruby-forum.com/\.