As part of learning Ruby, we will challenge "competitive programming (competitive professional)". We will output what we have learned in the learning for that purpose. This time from the first question (Therefore) of "At Coder Beginners Contest 168". https://atcoder.jp/contests/abc168/tasks/abc168_a
I will summarize it based on the solution I used and the commentary published from the official.
When counting pencils in Japanese, "book" is added as a classifier after the number. How to read this classifier differs depending on what number it is attached to. For integers less than or equal to 999, how to read "book" when saying "N books" is
・ When the 1st place of N is "2,4,5,7,9",'hon' ・ When the 1st place of N is "0,1,6,8",'pon' ・ When the 1st place of N is "3", it is'bon'
Given N, print out the corresponding "book" reading.
Constraint N is a positive integer less than or equal to 999
The input is given in the following form.
N #Example 16
In the above example, it can be divided up to 2 times.
Output example #In the case of the above example => pon
First is the code I wrote first.
a = gets.to_i.modulo(10) if [2,4,5,7,9].include?(a) print "hon" elsif [0,1,6,8].include?(a) print "pon" else print "bon" end
Use the modulo method to receive only the ones digit from the input Judgment is made by the if statement and the include? Method. include? As for the method, I wrote about the following article once, so [Ruby learning [Some Sums] with AtCoder Beginners Selection](https://qiita.com/jag_507/items/cd3ad74d77ad933897b7#include%E3%83%A1%E3%82%BD%E3%83%83 % E3% 83% 89range% E3% 82% AF% E3% 83% A9% E3% 82% B9) Here, I will touch on the modulo method.
Returns the remainder divided by the specified number. In the answer, the ones digit is taken out as a remainder by dividing the integer by 10.
#Example 13.modulo(4) => 1 42.modulo(10) => 2
In the commentary distributed after the contest, A more intuitive way to write with a case statement is introduced.
In the following, we will introduce the case statement after answering with the case statement.
n = gets.to_i.modulo(10) case n when 2,4,5,7,9 print 'hon' when 0,1,6,8 print 'pon' else print 'bon' end
In the first answer, I put the condition in an array and made a judgment with the include? Method, By using the case statement, you can write the code as if you were thinking "If the first place is 2,4,5,7,9 ...". Certainly this one seems to be better.
This is useful when searching for a match from multiple candidates for a single value. The "===" operator determines whether it matches what is specified by when.
case object when value1 # object ===Statement to be executed when value1 when value2 # object ===Statement to be executed when value2 else #Statement to execute if all are not matched end
The "===" operator that appears here is If the object mentioned above is a character string or a numerical value, set it as "==". For regular expressions, use "= ~" In the case of a range, it seems to judge whether the value is included in it like include? It seems to be flexible and easy to use.
So far, we have introduced the methods learned from the first question (Therefore) of "AtCoder Beginners Contest 168".
If you have any mistakes, I would be grateful if you could point them out.