[Ruby] Find numbers in arrays

A word

I've been busy for some reason recently ... I don't have a lot of work, but ... why Since the contract expires, I will move the office if I prepare to move. What timing

So the main subject The title is reminiscent of someone wearing red and white border clothes with glasses ...

Find any number in the array

Challenge the tasks you did before in the morning study today (second time)

array=[1,3,5,6,9,10,13,20,26,31]

Put it out like this I forgot a little and saw the explanation without understanding. A method of taking out the data in the middle, comparing the size on the left and right, narrowing the range by iterative processing, and finally guessing which number. The so-called binary search

So, I was ashamed to say that I learned the word "linear search" for the first time in the content I commented on yesterday's article, and I posted it with the intention of trying this method.

def linear_search(array, num)
  array.each_with_index do |n, index|
    if n == num
      return index
    end
  end
  return
end
array=[1,3,5,6,9,10,13,20,26,31]

puts "Please enter the number you want to search"
num = gets.to_i

result = linear_search(array, num)

if result.nil?
  puts "#{num}Does not exist in the array"
else
  puts "#{num}Is an array#{result+1}Exists second"
end

* The content pointed out in the comments has been corrected.

Now you can get similar output results. I wasn't aware of words such as linear search, but it felt like I was doing it all the time, so it was easier for me personally.

Congratulations

Recommended Posts

[Ruby] Find numbers in arrays
Find Roman numerals in Ruby
Convert numbers to Roman numerals in Ruby
About Ruby arrays
Heavy in Ruby! ??
[Understanding] Differences between hashes and arrays in Ruby
Enumerate subsets of arrays given in Ruby (+ α)
Safe numbers (ruby edition)
Combine arrays in Java
About eval in Ruby
Ruby Joins multiple arrays
Fast popcount in Ruby
Calculate the difference between numbers in a Ruby array
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Basic methods of Ruby arrays
Read design patterns in Ruby
Write class inheritance in Ruby
Integer unified into Integer in Ruby 2.4
Cannot find javax.annotation.Generated in Java 11
Multiplication in a Ruby array
About regular expressions in Ruby
Find a subset in Java
Calculate prime numbers in Java
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
Try using gRPC in Ruby
Microbenchmark for integer power of floating point numbers in Ruby
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
[Ruby] How to batch convert strings in an array to numbers
[Ruby] How to count even or odd numbers in an array
Sorting hashes in a Ruby array
How to find May'n in XPath
Basics of sending Gmail in Ruby
How to iterate infinitely in Ruby
Achieve 3-digit delimited display in Ruby
Encoding when getting in Windows + Ruby
Run GraphQL Ruby resolver in parallel
Ruby on Rails Japanese-English support i18n
[Ruby] Extracting double hash in array
String output method memo in Ruby
Implement a gRPC client in Ruby
Write keys and values in Ruby
[Super Introduction] About Symbols in Ruby
Hanachan in Ruby (non-destructive array manipulation)
Manipulating data in GCS during Ruby
Is there no type in Ruby?
openssl version information in ruby OPENSSL_VERSION
Ruby methods often used in Rails
How arrays work in Java (illustration)
How to find Java prime numbers
Make Ruby segfault in two lines
Find the maximum and minimum of the five numbers you entered in Java
[Ruby] Returns characters in a pyramid shape according to the entered numbers