Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression


As a part of Ruby learning, we will challenge "algorithm practical test". We will output what we have learned in the learning for that purpose. This time, from the first question (double check) of the "1st Algorithm Practical Test". 1st Algorithm Practical Test Past Question

I will introduce my answer and the expressions and methods I learned while answering.


Create a program that receives a 3-digit integer, doubles it, and outputs it. However, the entered string S may be mixed with lowercase letters. In that case, an error is output. If S is a 3-digit integer (including those starting with '0'), output an integer that is twice that number, otherwise output'error'.

Constraint ・ S is a character string of length 3 ・ Each letter of S is a number or lowercase letter

The input is given in the following form.


Input example ①
Input example ②
Output example
#Input example ①678
=> 678
#Input example ② 4g4
=> error


First of all, from the answer I submitted.

s = gets.chomp
print /^[0-9]+$/ === s ? s.to_i * 2 : "error"

The received character string is judged by using a regular expression whether the character string of each digit is 0 to 9, and it is determined. If it is true, it is converted to Integer type and then doubled, and if it is false, it is output as error.

Regular expressions

Here, I would like to reconfirm what a regular expression is. At first, the word "regular expression" itself was difficult to understand, After reading this article, I somehow fell in love with it. What is a regular expression?

What is a regular expression? ** "Expression method that expresses various character strings with one character string" ** And ** "useful for finding and replacing strings" **.

There may be others, but "other" will be remembered when you meet that "other".

Judge with a regular expression that represents an integer

Now that we have defined what a regular expression is, let's take a look at the regular expression used this time.

/^[0-9]+$/ === s

The ones related to the regular expression used here are as follows

=== Determines if the regular expression string pattern on the left side is included in the string on the right side. The left side and the right side must not be interchanged.

/ / //で囲むことで正規表現オブジェクトを生成することが出来ます。 In other words, it is a declaration that "what is enclosed in // is a regular expression".

[ ] The part between the square brackets [] is called the character class. An enumeration of one or more characters that matches any one character (1 of 0-9 above).

-(hyphen) Indicates the range of characters. If it is [A-C], it will be one of A, B, C, and if it is [0-9], it will be one of 0 to 9.

^(/^xxx/) The beginning of the line. Matches the beginning of the string or the position immediately after the newline character.

+ Represents one or more repetitions.

****(/xxx/) The end of the line. Matches the end of the string or just before the newline character.

Therefore, the meaning of the following description is It means that "s is a character string consisting of all characters from 0 to 9".

/^[0-9]+$/ === s


The above is a summary of the regular expressions learned while solving the first question (double check) of the "1st Algorithm Practical Test". I think it's important to get used to regular expressions by solving problems. In situations where it seems to be usable, I would like to actively investigate and use it.

If you have any mistakes, I would be grateful if you could point them out.

Recommended Posts

Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
Learning Ruby with AtCoder 14 [3rd Algorithm Practical Test Sprinkler] Hash creation, key / value addition
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Ruby regular expression
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Extract a string starting with a capital letter with a regular expression (Ruby)
Integer check method with ruby
Ruby Learning # 8 Working With String
Learning Ruby with AtCoder 11 How to receive frequently used standard input