[RUBY] Get the value from the array and find out what number it is included in

In the array,

input = [1, 3, 5, 6, 10, 14, 17, 21, 27, 33, 39, 41, 48]

Is included. You can use the serch method to return the result of what number the value is included in. Example)

search(17, input)
#Result of processing
#=>6th

search(1, input)
#Result of processing
#=>That number is not included

The usage example looks like this.

I would like to create this search method using the each_with_index method. The each_with_index method is a method built into Ruby as standard, and it can indicate the number of processing of the element at the same time as repeating the element. reference: Ruby 3.0.0 Reference Manual, each_with_index

Example)

fruits = ["Strawberry","Mandarin orange"."Grape"]

fruits.each_with_index do |item, i|
  puts "#{i}The second fruit is#{item}is."
end

#Output result

#The 0th fruit is melon.
#The first fruit is banana.
#The second fruit is apple.

You can use it like this.

Well then, I would like to get into the main subject. First, I would like to think about how to call the search method. Let's define an array and set some number as an actual argument when calling it. This time I would like to set a value of 17.

def search(target_num, input)

end

input = [1, 3, 5, 6, 10, 14, 17, 21, 27, 33, 39, 41, 48]
search(17, input)

In the search method called by search (17, input), the value set by the actual argument is received by the formal argument (target_num, input). This time target_num receives 17 and input receives input (the contents are assigned an array).

Next, I would like to consider the processing inside the search method. Use the each_with_index method to retrieve the contents of the array one by one and get the subscripts assigned to each element at the same time.

def search(target_num, input)

  input.each_with_index do|num, index|
  end
end

Finally, we will use conditional bifurcation processing to describe the values ​​in the array and the processing when they are not. When combined, the description is as follows.

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

input = [1, 3, 5, 6, 10, 14, 17, 21, 27, 33, 39, 41, 48]
search(17, input)

When num and target_num are equal, puts "# {index + 1} is at the th" is output. The reason for index +1 is that the program counts from 0.

Since humans basically count from 1, index +1 is used to fill in the error in counting programs.

Recommended Posts

Get the value from the array and find out what number it is included in
The value is taken out from the double hash and output by the getter.
[Android development] Get an image from the server in Java and set it in ImageView! !!
How to output the value when there is an array in the array
Find out what many threads are doing from the jstack results
Get the type of an array element to determine if it is an array
Gradle automatically generates version number from git and uses it in Java
How to get the setting value (property value) from the database in Spring Framework
I want to find out what character the character string appears from the left
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
Is it possible to put the library (aar) in the Android library (aar) and use it?
[Beginner] What is Docker in the first place? Easy-to-understand explanation from the basics!
How to create your own annotation in Java and get the value
What is the main method in Java?
[Swift] How to get the number of elements in an array (super basic)
If the parameter is an array, how to include it in Stopara's params.permit
Get YouTube video information with Retrofit and keep it in the Android app.
[Java] Program example to get the maximum and minimum values from an array
Find a value that is convenient to have a method and make it a ValueObject
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
Retrieve the first day of week in current locale (what day of the week is it today?)
What is ... (3 dots) found in the Java source? Variadic arguments to know from
What is Pullback doing in The Composable Architecture
What is different from the PHP language. [Note]
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Java] Get a random value from an array
Get your version number in the Android app
I want to get the value in Ruby
[Java] How to get the key and value stored in Map by iterative processing
Find the greatest common divisor and least common multiple of any number of integers in Ruby
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
Output the maximum value from the array using Java standard output
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
[JAVA] What is the difference between interface and abstract? ?? ??
21 Load the script from a file and execute it
What is the difference between skip and pending? [RSpec]
Find the approximate value of log (1 + x) in Swift
What is the difference between Java EE and Jakarta EE?
Why the get method is needed in the Calendar class
I tried to find out what changed in Java 9
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
Filter the Java No. 2 Optional list that was useful in business and get the first value
How to solve the problem when the value is not sent when the form is disabled in rails and sent
In Swift, when JSON purge, it is passed in the snake case and troublesome coping method
Register a DLNA compatible TV in SORACOM Inventory and operate it from the Web console