[GO] Learning Ruby with AtCoder 6 [Contest 168 Therefore]

Introduction

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.

problem

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

Answer ①

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.

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

Answer ② case sentence

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.

case statement

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.

Finally

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.

Recommended Posts

Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Ruby Learning # 8 Working With String
Ruby learning 4
Ruby learning 5
Ruby learning 3
Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby learning 2
Ruby learning 6
Ruby learning 1
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Solve with Ruby AtCoder ABC177 D UnionFind
Ruby Learning # 25 Comments
Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
AtCoder Beginner Contest 168
Ruby Learning # 15 Methods
Solving with Ruby and Crystal AtCoder ABC 129 D
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
Install Ruby 3.0.0 with asdf
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Solving with Ruby, Perl and Java AtCoder ABC 128 C
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
AtCoder Beginner Contest 170 A, B, C up to ruby
Ruby Learning # 31 Object Methods
Learning Ruby with AtCoder 14 [3rd Algorithm Practical Test Sprinkler] Hash creation, key / value addition
Getting Started with Ruby
Ruby Learning # 27 Writing Files
[Rails] Learning with Rails tutorial
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
11th, Classification with Ruby
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Evolve Eevee with Ruby
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Learn Ruby with AtCoder Beginners Selection [Coins] Answer with short code
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
AtCoder Beginner Contest 182 Participation Article
Ruby Learning # 2 Drawing a Shape
Ruby on rails learning record -2020.10.03
Ruby version switching with rbenv
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby Learning # 18 If Statements (Con't)
Ruby: Send email with Starttls