Skip to content

Instantly share code, notes, and snippets.

@paulghaddad
Created January 7, 2015 18:08
Show Gist options
  • Save paulghaddad/eb91bf983b2c4c165f49 to your computer and use it in GitHub Desktop.
Save paulghaddad/eb91bf983b2c4c165f49 to your computer and use it in GitHub Desktop.
Exercism: Roman Numerals

Roman Numerals

Write a function to convert from normal numbers to Roman Numerals: e.g.

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

See also: http://www.novaroma.org/via_romana/numbers.html

Source

The Roman Numeral Kata view source

class Fixnum
ROMAN_MAPPINGS = {
1 => 'I',
5 => 'V',
10 => 'X',
50 => 'L',
100 => 'C',
500 => 'D',
1000 => 'M',
}
EXCEPTIONS = {
/IIII/ => 'IV',
/VIV/ => 'IX',
/XXXX/ => 'XL',
/LXL/ => 'XC',
/CCCC/ => 'CD',
/DCD/ => 'CM',
}
def to_roman
input_number = self
roman_numeral = ''
until input_number.zero?
ROMAN_MAPPINGS.reverse_each do |numeral, roman_letter|
if input_number % numeral == 0
input_number -= numeral
roman_numeral.prepend(roman_letter)
break
end
end
end
check_for_exceptions(roman_numeral)
end
private
def check_for_exceptions(roman_numeral)
EXCEPTIONS.each do |exception, substitution|
roman_numeral.gsub!(exception, substitution)
end
roman_numeral
end
end
require 'minitest/autorun'
require_relative 'roman'
class RomanTest < MiniTest::Unit::TestCase
def test_1
assert_equal 'I', 1.to_roman
end
def test_2≈
assert_equal 'II', 2.to_roman
end
def test_3
assert_equal 'III', 3.to_roman
end
def test_4
assert_equal 'IV', 4.to_roman
end
def test_5
assert_equal 'V', 5.to_roman
end
def test_6
assert_equal 'VI', 6.to_roman
end
def test_9
assert_equal 'IX', 9.to_roman
end
def test_27
assert_equal 'XXVII', 27.to_roman
end
def test_48
assert_equal 'XLVIII', 48.to_roman
end
def test_59
assert_equal 'LIX', 59.to_roman
end
def test_93
assert_equal 'XCIII', 93.to_roman
end
def test_141
assert_equal 'CXLI', 141.to_roman
end
def test_163
assert_equal 'CLXIII', 163.to_roman
end
def test_402
assert_equal 'CDII', 402.to_roman
end
def test_575
assert_equal 'DLXXV', 575.to_roman
end
def test_911
assert_equal 'CMXI', 911.to_roman
end
def test_1024
assert_equal 'MXXIV', 1024.to_roman
end
def test_3000
assert_equal 'MMM', 3000.to_roman
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment