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!

On 2013-Dec-17, at 15:25 , Johanna B. [email protected] wrote:

So now I have to make a function which returns the number of positive
end

puts “#{anzahl_positiv(a)}”

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

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 B.
[email protected]

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 B. [email protected]
To: [email protected]
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!

Rob’s message contains the exact code you need.

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:

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:

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

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.

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 B. way is the “right” way to go.

a.count {|e| e > 0}

Best regards,
Abinoam Jr.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs