AtCoder ABC168 A case expression solved in Ruby and Python

Introduction

This theme

AtCoder Beginner Contest A - ∴ (Therefore) Difficulty: 2

This theme, case expression Ruby In the main battle, it is solved by the ʻif` formula, but in * editorial *

As an aside, in Ruby, the case statement corresponds to this, and it is more sophisticated.

Since it is written, I would like to write it in case. ~~ I have never written a case ~~ First of all, ʻif`

ruby.rb


n = gets.to_i
n %= 10
if n == 3
  puts "bon"
elsif n == 0 || n == 1 || n == 6 || n == 8
  puts "pon"
else
  puts "hon"
end

last.rb


n %= 10

I get the 1's place by the remainder divided by 10, but I can also make it a string as gets.chomp and make itn [-1].

case.rb


n = gets.chomp
case n[-1]
when "3" then
  puts "bon"
when "0", "1", "6", "8" then
  puts "pon"
else
  puts "hon"
end

python.py


n = int(input()) % 10
if n == 3:
    print("bon")
elif n in {0, 1, 6, 8}:
    print("pon")
else:
    print("hon")

Python doesn't seem to have a case, so I tried to make it look similar. Python's ʻin is similar to SQL's ʻin.

Summary

Referenced site

Recommended Posts

AtCoder ABC168 A case expression solved in Ruby and Python
AtCoder ARC080 D simulation solved in Ruby and Python
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
AtCoder ABC 177 Python (A ~ E)
AtCoder ABC 178 Python (A ~ E)
Atcoder ABC164 A-C in Python
AtCoder ABC 176 Python (A ~ E)
Atcoder ABC167 A-D in Python
Atcoder ABC166 A-E in Python
AtCoder ABC 182 Python (A ~ D)
Atcoder ABC169 A-E in Python
AtCoder ABC177 A-D in python
Solve Atcoder ABC176 (A, B, C, E) in Python
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solve Atcoder ABC169 A-D in Python
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solve ABC036 A ~ C in Python
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solved AtCoder ABC 114 C-755 with Python3
Template AtCoder ABC 179 Python (A ~ E)
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solve ABC037 A ~ C in Python
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Use print in a Python2 lambda expression
Differences between Ruby and Python in scope
Solve AtCoder ABC168 with python (A ~ D)
Solve ABC165 A, B, D in Python
AtCoder ABC 174 Python
AtCoder ABC187 Python
AtCoder ABC188 Python
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Organize python modules and packages in a mess
I wrote a class in Python3 and Java
Regular expression symbolic group name in Python / Ruby
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
case class in python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python