[RUBY] Training using each_with_index method

Use the each_with_index method to find the element you want to find in the array, Let's create a method to find out what number the element is.

Let's search the number in the following array and create a search method that shows the result of the number included.

input = [3, 5, 9, 12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]

In the search method Check if a certain number is included in this input array If it is included → Output the number of elements. If it is not included → It is not included. Is output. It is possible that such a description is necessary.

First, to know what number element it is if it contains the number you want to look up You need to use the each_with_index method.

each_with_index Each method can only retrieve an element of an array, not the number of that element. However, with the each_with_index method, you can retrieve both the "array element" and the "number element" as block variables.

Description example

input.each_with_index do |item, i|
~
end

item is an element included in the input array, and in the input array, it is the value itself of 3,5,9,12 ... 93. i is the element number (subscript) and takes values up to 0,1,2,3, ... 15 in order from the left in the input array. Since it is each method, it will be repeated until the end of the array elements. For example

example = [3, 5, 9, 12]
example.each_with_index do |item, i|
  puts "#{item}Is#{i}The second number."
end

When you output with this,

3 is the 0th number.
5 is the first number.
8 is the second number.
12 is the third number.

Is output. It turns out that the each_with_index method repeats up to the last element of the array, and the element (item) and the number element (i) can be retrieved together.

Now, using the each_with_index method, write a method to find out the value you want to find and the number in which that value is included. As a procedure input.each_with_Block variable with index|item, i|Take out. Write a conditional expression to see if the retrieved value matches the value you want to find. When the conditions are met → Display the number of the element. When the conditions are not met → Display that the number is not included.

def search(target_num, input)
  input.each_with_index do |num, index|
    if num == target_num
      puts "#{num}Is#{index + 1}The second number."
      return
    end
    puts "That number is not included."
   end
  end
end
input = [3, 5, 9, 12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(42, input)

Specify target_num and input as arguments to the method called search. target_num is the number you want to look for, and input is the array you are looking for. input.each_with_index do |num, index| In the part of, the element num and the number of the element (index) are extracted from the input array up to the last value of the array. if num == target_num Here, I am writing a conditional expression to see if the elements (numbers) of the array and the numbers I want to find (this time I will check 42 at the end) match. If there is a match, the number and number of elements you want to find in the puts method are displayed. Complete!

I was wondering during the creation. Why not write a description that displays if it is included in if, and a description that displays it if it is not included in else? I thought that I made the following description.

def search(target_num, input)
  input.each_with_index do |num, index|
    if num == target_num
      puts "#{num}Is#{index + 1}The second number."
    else
    puts "That number is not included."
    end
  end
end

Let's display the result in this case.

input = [3, 5, 9, 12]

If you put it

search(9, input)

When

That number is not included.
That number is not included.
9 is the third number.
That number is not included.

It will be displayed like this, and all the results will be displayed. In other words, the more elements in the array, the more useless results. It is completed only by the processing when it is included there.

   if num == target_num
      puts "#{num}Is#{index + 1}The second number."
      return
   end

Even if it is included, the process ends there because return is described, so the process of "the number is not included" can be output without being read.

Recommended Posts

Training using each_with_index method
Try using each_with_index method
Method
Java comparison using the compareTo () method
Confirm name input using include? method
[Rails 6] destroy using the resources method
Try using IBM Java method tracing
[Ruby] Search problem using index method
[Ruby] Obtaining even values ​​using the even? Method
Newcomer training using the Web-Basic programming using Java-
Get the error message using the any? method