roman numerals
** Write a method that takes arabic numerals and returns roman numerals. ** **
Each Roman numeral corresponds as follows.
roman num | arabic num |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Basically, the straightforward method is to divide by the Arabic numerals corresponding to various Roman numerals. However, Roman numerals behave a little specially at 4 and 9 in Arabic numerals. If you try to do something good with a formula, you will likely abuse the if statement.
This time, in order to make the program simple and beautiful, 4 and 9 in each digit of Arabic numerals are registered in the array in advance. However, if you want to prepare an array, you have to prepare two arrays, Roman numerals and Arabic numerals. This is no longer a simple and beautiful program.
Therefore, although it is not mentioned in the class, we will utilize the convenient hash unique to Ruby. A simple explanation of hash is the dictionary type in Python. If there is an irregular but correspondence between two arrays, you can use hash and each to make a simple program. What I learned when I was a mentor at a programming school can be used in such a place.
If you use an array
arabic_value = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
roman_value = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
If you use a hash, you can use a hash instead of two lines.
value_sets = {"1000"=>"M","900"=>"CM","500"=>"D","400"=>"CD","100"=>"C","90"=>"XC","50"=>"L","40"=>"XL","10"=>"X","9"=>"IX","5"=>"V","4"=>"IV","1"=>"I"}
It can be made into one line like this, and it becomes easy to understand visually and intuitively.
Now, let's implement a method to \ _roman that converts Arabic numerals to Roman numerals using hash. Since the hash key is an Arabic numeral, divide it by the key in order from the beginning, and substitute the Roman numeral value for the number of quotations. This can be repeated with each from the beginning of the hash, so the program is as follows.
def to_roman(num)
str = ""
value_sets = {"1000"=>"M","900"=>"CM","500"=>"D","400"=>"CD","100"=>"C","90"=>"XC","50"=>"L","40"=>"XL","10"=>"X","9"=>"IX","5"=>"V","4"=>"IV","1"=>"I"}
value_sets.each do |arabic,roman|
quotient = num/arabic.to_i
num = num%arabic.to_i
str << roman*quotient
end
str
end
puts to_roman(ARGV[0].to_i)
The program was shorter than I had imagined.
Extend Integer class to
999.to_roman #=>CMXCIX
I want to return Roman numerals by executing like this.
It's easy when you get here.
Like this.
class Integer
def to_roman
num = self
str = ""
value_sets = {"1000"=>"M","900"=>"CM","500"=>"D","400"=>"CD","100"=>"C","90"=>"XC","50"=>"L","40"=>"XL","10"=>"X","9"=>"IX","5"=>"V","4"=>"IV","1"=>"I"}
value_sets.each do |arabic,roman|
quotient = num/arabic.to_i
num = num%arabic.to_i
str << roman*quotient
end
str
end
end
puts ARGV[0].to_i.to_roman
Don't forget to check the operation.
$ ruby roman_numerals.rb 23
XXIII
$ ruby roman_numerals.rb 169
CLXIX
$ ruby roman_numerals.rb 1997
MCMXCVII
$ ruby roman_numerals.rb 2020
MMXX
There seems to be no problem.
Click here for the page I referred to this time. roman numerals
Recommended Posts