roman numerals (I tried to simplify it with hash)

roman numerals

problem

** 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

solution

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.

Two arrays in Hash

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.

Implementation

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.

Development issues

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.

Implementation

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

Operation check

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.

Reference page

Click here for the page I referred to this time. roman numerals


Recommended Posts

roman numerals (I tried to simplify it with hash)
I tried to interact with Java
I tried to explain Active Hash
roman numerals
roman numerals
roman numerals
roman numerals
roman numerals
Roman Numerals
roman numerals
roman numerals
EX1: roman numerals
Let's solve the roman numerals
Find Roman numerals in Ruby
Convert numbers to Roman numerals in Ruby
roman numerals (I tried to simplify it with hash)
I tried to get started with WebAssembly
I tried to implement ModanShogi with Kinx
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
I tried to break a block with java (1)
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
I tried what I wanted to try with Stream softly.
I tried to implement file upload with Spring MVC
I tried to read and output CSV with Outsystems
[Java 11] I tried to execute Java without compiling with javac
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
I tried to draw animation with Blazor + canvas API
I tried to implement Stalin sort with Java Collector
I called YouTube video from DB with haml and tried to embed and display it
When I tried to support IPv6 easily with Docker-proxy, I couldn't do it before I knew it.
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried DI with Ruby
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried to create a java8 development environment with Chocolatey
I tried to increase the processing speed with spiritual engineering
[Rails] I tried to create a mini app with FullCalendar
I tried to link chat with Minecraft server with Discord API
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to create a padrino development environment with Docker
I tried to get started with Swagger using Spring Boot
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried to be able to pass multiple objects with Ractor
When I tried to run Azure Kinect DK with Docker, it was blocked by EULA
I tried to solve the problem of "multi-stage selection" with Ruby
I tried using JOOQ with Gradle
I tried morphological analysis with MeCab
I tried to build the environment of PlantUML Server with Docker
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to summarize iOS 14 support
I tried to implement the image preview function with Rails / jQuery
I tried to build an http2 development environment with Eclipse + Tomcat
I tried to implement flexible OR mapping with MyBatis Dynamic SQL
I tried connecting to Oracle Autonomous Database 21c with JDBC Thin
I tried to explain the method
I tried to reimplement Ruby Float (arg, exception: true) with builtin
Since the Rspec command is troublesome, I tried to make it possible to execute Rspec with one Rake command
I tried to make an Android application with MVC now (Java)
I tried to check the operation of gRPC server with grpcurl
I tried GraphQL with Spring Boot
I tried to make it an arbitrary URL using routing nesting
I tried to summarize Java learning (1)
[Introduction to JSP + Servlet] I played with it for a while ♬
I tried to understand nil guard
I tried Flyway with Spring Boot
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried customizing slim with Scaffold