# AudioStakes's solution

## to Roman Numerals in the Ruby Track

Published at Nov 08 2019 · 0 comments
Instructions
Test suite
Solution

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

`````` 1  => I
10  => X
7  => VII
``````

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby roman_numerals_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride roman_numerals_test.rb
``````

## Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### roman_numerals_test.rb

``````require 'minitest/autorun'
require_relative 'roman_numerals'

# Common test data version: 1.2.0 3c78ac4
class RomanNumeralsTest < Minitest::Test
def test_1_is_a_single_i
# skip
assert_equal 'I', 1.to_roman
end

def test_2_is_two_i_s
skip
assert_equal 'II', 2.to_roman
end

def test_3_is_three_i_s
skip
assert_equal 'III', 3.to_roman
end

def test_4_being_5_1_is_iv
skip
assert_equal 'IV', 4.to_roman
end

def test_5_is_a_single_v
skip
assert_equal 'V', 5.to_roman
end

def test_6_being_5_1_is_vi
skip
assert_equal 'VI', 6.to_roman
end

def test_9_being_10_1_is_ix
skip
assert_equal 'IX', 9.to_roman
end

def test_20_is_two_x_s
skip
assert_equal 'XXVII', 27.to_roman
end

def test_48_is_not_50_2_but_rather_40_8
skip
assert_equal 'XLVIII', 48.to_roman
end

def test_49_is_not_40_5_4_but_rather_50_10_10_1
skip
assert_equal 'XLIX', 49.to_roman
end

def test_50_is_a_single_l
skip
assert_equal 'LIX', 59.to_roman
end

def test_90_being_100_10_is_xc
skip
assert_equal 'XCIII', 93.to_roman
end

def test_100_is_a_single_c
skip
assert_equal 'CXLI', 141.to_roman
end

def test_60_being_50_10_is_lx
skip
assert_equal 'CLXIII', 163.to_roman
end

def test_400_being_500_100_is_cd
skip
assert_equal 'CDII', 402.to_roman
end

def test_500_is_a_single_d
skip
assert_equal 'DLXXV', 575.to_roman
end

def test_900_being_1000_100_is_cm
skip
assert_equal 'CMXI', 911.to_roman
end

def test_1000_is_a_single_m
skip
assert_equal 'MXXIV', 1024.to_roman
end

def test_3000_is_three_m_s
skip
assert_equal 'MMM', 3000.to_roman
end
end``````
``````class Integer
ROMAN_HASH = {
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'
}
def to_roman
remaining = self
ROMAN_HASH.inject('') do |result, (arabic, roman)|
n, remaining = remaining.divmod(arabic.to_i)
result << roman * n
end
end
end``````