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.
S
Input example ①
678
Input example ②
4g4
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.
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".
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.
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