Participating in competitive programming as part of learning Ruby and algorithms. Here, we will output what we have learned in the learning for that purpose. This time, I would like to summarize the "how to receive standard input" that I used to solve the problem.
Input example
Ruby
s = gets.chomp
# print s
# => Ruby
The chomp method returns a string with the line feed code removed from the end of the string. It is safe to attach it so that you can receive only the character string you want to receive.
Input example
64
s = gets.to_i
# print s
# => 64
The standard input received by the gets method is received as a String type. Converts the received character string to Integer type by adding the to_i method to the end. In this case, the chomp method is not needed.
Depending on the problem, there are quite a few cases where you can get closer to the answer by decomposing the elements. Here are some of the examples.
Input example
Ruby
ary = gets.split("")
ary = gets.split(//) #Regular expressions
# print ary
# => ["R", "u", "b", "y"]
The split method is a method that splits a character string into an array. You can specify the delimiter as the first argument in (). If you want to split each character, you can use ("") or (//).
Input example
RubyRuby
ary = gets.scan(/../) #①
ary = gets.scan("R") #②
ary = gets.scan(/Ru./) #③
# print ary
#① => ["Ru", "by", "Ru", "by"]
#② => ["R", "R"]
#③ => ["Rub", "Rub"]
The scan method repeatedly matches the pattern specified in the argument and returns the matched substring as an array. Therefore, it can be used when you want to divide the input according to a certain rule and handle it. It may be a little difficult until you get used to expressing the rules with regular expressions.
It is a pattern that receives multiple elements at once, separated by a half-width space.
Input example
Ruby PHP
ary = gets.split(" ")#Space between double colons is a half-width space
# print ary
# => ["Ruby", "PHP"]
By specifying a half-width space as above in the first argument of the split method, You can receive multiple elements separated by a half-width space at once.
Input example
Ruby PHP
a,b = gets.split(" ")
# print a
# => "Ruby"
# print b
# => "PHP"
If you have a limited number of elements to receive, it may be more convenient to receive each in a variable. In such a case, you can receive each by separating the variables with "," as described above.
Input example
123 456
ary = gets.split(" ").map(&:to_i) #① Receive in an array
a,b = gets.split(" ").map(&:to_i) #② Receive as a variable
# print ary
# => [123, 456]
# print a
# => 123
# print b
# => 456
The map method iterates through the blocks as many times as there are elements in the array and returns the resulting array. When you want to apply a method to each element (this time you want to convert all elements to integer type), You can abbreviate it as above using &.
In the issue of competitive programming, First, "the number of elements given after that" is entered, There are many problems with the pattern that the "elements necessary for the answer" are given from the next line. In that case, you can use the times method to receive the elements together.
Input example
3 #Number of elements given after this
4 8
8 2
5 2
#When receiving as an integer type
n = gets.to_i #Number of elements given in the first line
ary = n.times.map{gets.split.map(&:to_i)}
# print ary
# => [[2, 2], [8, 2], [5, 2]]
#When receiving as a string type
n = gets.to_i
ary = n.times.map{gets.split}
# print ary
# => [["2", "2"], ["8", "2"], ["5", "2"]]
With a combination of times method and map method Inputs are organized as a two-dimensional array, which is convenient for making various judgments.
This is how to receive standard input that you often use. If I come across another method while solving the problem, I would like to add it each time.
If you have any mistakes, I would be grateful if you could point them out.
Recommended Posts