Try using each_with_index method

As the title says. Let's write the code using the each_with_index method.

What is each_with_index method?

A method that can indicate the number of processing of an element at the same time as iterating the element. You can use each method if it is an iterative process of elements, The feature of each_with_index method is that it can also display what number was processed. Write as follows.

Array name.each_with_index do |item, i|
  #processing
end

I tried using it (1)

fruits = ["Mandarin orange", "pineapple", "Apple"]

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

The output result is as follows.

The 0th fruit is mandarin orange.
The first fruit is pineapple.
The second fruit is an apple.

I tried using it (Part 2)

A method that searches if an array contains a certain value and outputs the result Let's write it using each_with_index method. ~~ You can use binary search without using the each_with_index method ~~

The array is input = [3, 6, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93] Suppose you want to assign the value you want to search to the variable target_num. Make sure you get the following search results.

#When the value you want to search for is in the array. For example, if the value you want to search for is 12.
Fourth from the left

#When the value you want to search is not in the array
That number is not included

Answer example

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

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

target_num = gets.to_i
search(target_num, input)

Commentary

This time, the method that performs the search process is defined as the search method.

In the search method, use each_with_index method The values stored in the array ʻinput are fetched one by one as num and numbered in the order they are fetched. That number goes into ʻindex.

Compare if num and target_num (which contains the value you want to search for) are the same. If they are the same, we want to output the number of the value counting from the left of the array. puts" # {index + 1} th from the left ". And since the search results are already available and no further searches are needed, end with return.

If all the values stored in the array are not the same as the one you want to find Let's say puts" that number is not included ".

end

that's all. It's a little off the subject, but it's quite difficult to write the commentary. Thank you for reading to the end.

Recommended Posts

Try using each_with_index method
Training using each_with_index method
Try using IBM Java method tracing
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using Talend Part 2
Try using Talend Part 1
Try using F # list
Try using Spring JDBC
Try using RocksDB in Java
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try scraping using java [Notes]
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
[Swift] Try using Collection View
Try using Spring Boot Security
[Rails] Try using Faraday middleware
[Programming Encyclopedia] §2 Try using Ruby
People using docker Try using docker-compose
Method
Java comparison using the compareTo () method
Try using Redis with Java (jar)
Confirm name input using include? method
[Java] Try to implement using generics
Try to extract java public method
Try using the messaging system Pulsar
[Rails 6] destroy using the resources method
[Ruby] Search problem using index method
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Try using || instead of the ternary operator
[Ruby] Obtaining even values ​​using the even? Method
Try using the Stream API in Java
Try using the Rails API (zip code)
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try using Reladomo's MT Loader (Multi-Threaded Matcher Loader)
Try using JobScheduler's REST-API --Java RestClient implementation--
Get the error message using the any? method
Try using Kong + Konga with Docker Compose.
Try using the Emotion API from Android
Try using the Wii remote with Java
Try using simple_form / edit even child models
Try implementing GraphQL server using grahpql-java-tools (+ kotlin)
Try to make a timeline report of method execution time using JFR API