[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!

Overview

We will learn how to count the number of even or odd elements from an array.

table of contents

--Practice --Problem --Answer (commentary) - odd? - odd? + count

--Summary --References

odd?

This method determines whether the target number is odd. If it is odd, it returns true, otherwise it returns false.

even?

This method determines whether the target number is even. Returns true if it is even, false otherwise.

Array#count

Returns the number of elements in the array.

Practice

problem

Count an odd number from the values ​​in the array and output!

Answer (commentary)

odd?

answer


def count_odds(nums) #Array receipt
  count = 0           #Define a variable count to put the counted number

  nums.each do |num|  #Extract elements from the array one by one
    if num.odd?       #Is the extracted value an odd number?
      count += 1      #If the value is odd, add 1 to the variable count
    end
  end
  p count             #Output count when each process is completed
end

#Method call
count_odds([3, 5, 2, 2, 1])
count_odds([3, 1, 0])
count_odds([2, 4, 6])

#Terminal output result
# 3
# 2
# 0

odd? + count It's even easier to write than the above.

Another solution


def count_odds(nums)
  p nums.count { |num| num.odd? }
end

#Method call
count_odds([3, 5, 2, 2, 1])
count_odds([3, 1, 0])
count_odds([2, 4, 6])

#Terminal output result
# 3
# 2
# 0

Summary

--odd? Is a method that returns true if the target number is odd, false otherwise --even? Is a method that returns true if the target number is even, false otherwise --The number of elements can be returned by using the count method on the array.

References

-Ruby 3.0.0 Reference Manual (odd?) -Ruby 3.0.0 Reference Manual (even?) -Ruby 2.7.0 Reference Manual (count)

Recommended Posts

[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby] How to count even or odd numbers in an array
[Ruby] Count an even number in an array using the even? Method
How to retrieve the hash value in an array in Ruby
[Ruby] How to batch convert strings in an array to numbers
A program (Java) that outputs the sum of odd and even numbers in an array
Ruby length, size, count How to use
[Ruby] How to use gsub method and sub method
How to build the simplest blockchain in Ruby
[Ruby basics] How to use the slice method
Calculate the difference between numbers in a Ruby array
How to handle TSV files and CSV files in Ruby
[Ruby] How to get the tens place and the ones place
How to use Ruby return
Ruby: How to use cookies
How to use the getter / setter method (in object orientation)
How to change a string in an array to a number in Ruby
How to create a placeholder part to use in the IN clause
How to call and use API in Java (Spring Boot)
How to add the same Indexes in a nested array
How to use the link_to method
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use the include? method
How to use the form_with method
How to use EventBus3 and ThreadMode
How to iterate infinitely in Ruby
How to use the wrapper class
How to use Ruby on Rails
How to learn JAVA in 7 days
How to use equality and equality (how to use equals)
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
How to use InjectorHolder in OpenAM
How to use classes in Java?
How to use Ruby inject method
[Swift5] How to get an array and the complement of arrays
How to output the value when there is an array in the array
How to set and use profile in annotation-based Configuration in Spring framework
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to install the language used in Ubuntu and how to build the environment
How to get and add data from Firebase Firestore in Ruby
Things to remember and concepts in the Ruby on Rails tutorial
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Java] How to use the hasNext function
How to use named volume in docker-compose.yml
[Java] How to use the HashMap class
Convert numbers to Roman numerals in Ruby
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to set up and use kapt
[Ruby] How to use is_a? Differences from kind_of? and instance_of ?. How to check the type and return a boolean value.
How to change the maximum and maximum number of POST data in Spark