Learning Ruby with AtCoder 11 How to receive frequently used standard input


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.

Receive one element


Input example
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
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.

Divide one element into any shape

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.

Divide the string into characters and make it an array

Input example
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 (//).

Divide the string according to any rule and make it an array

Input example
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.

Receive multiple elements from one line

It is a pattern that receives multiple elements at once, separated by a half-width space.

Receive as an array (string)

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.

Receive in variable (string)

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.

Receive an integer

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 &.

Receive elements that span multiple lines at once

times method

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

Learning Ruby with AtCoder 11 How to receive frequently used standard input
[ruby] How to receive values from standard input?
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
From terminal to Ruby (standard input / output)
[Java] How to receive numerical data separated by spaces in standard input
Ruby standard input
[Java] How to get and output standard input
AtCoder ABC127 D hash to solve with Ruby 2.7.1
[Ruby] How to use standard output in conditional branching
[Ruby] Summary of how to get values from standard input [Paiza skill check measures]
[Java] How to encrypt with AES encryption with standard library
How to output standard from an array with forEach
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
I immediately stumbled on the standard input method with AtCoder.
How to deal with different versions of rbenv and Ruby
[Java] I want to test standard input & standard output with JUnit
How to use Ruby return
[Ruby] How to comment out
How to number (number) with html.erb
Ruby Learning # 10 Getting User Input
How to update with activerecord-import
Ruby: How to use cookies
Ruby Learning # 8 Working With String
[Ruby] How to write blocks
[ruby] How to receive values from standard input?
Ruby input / output
From terminal to Ruby (standard input / output)
Ruby standard input
From Java to Ruby !!
Ruby receives multi-line input
Use C program from Ruby
Use Face API from Ruby
Ruby Learning # 10 Getting User Input
CHATBOT (Dialogflow) used from Ruby
Input to the Java console
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Ruby standard input and various methods
[Ruby] Escape from multiple loops [Nest]
Introduction to Ruby (from other languages)
How to display 0 on the left side of the standard input value
Design patterns to enjoy with frequently used Java libraries --Factory pattern
How to get started with slim
How to iterate infinitely in Ruby
Ruby standard input and various methods
How to install ruby through rbenv
How to enclose any character with "~"
How to use Ruby on Rails
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
How to get along with Rails
How to use Ruby inject method
Introduction to Docker (1) Frequently used commands
How to execute Ruby irb (interactive ruby)
How to start Camunda with Docker
Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
[Rails] How to apply the CSS used in the main app with Administrate
How to insert processing with any number of elements in iterative processing in Ruby