zacker330
11/19/2014 - 3:28 AM

Convert integer to Ramon number

Convert integer to Ramon number

require_relative 'integer_patch'
require "test/unit"

class TestRoman < Test::Unit::TestCase

  def test_all

    eq(4.to_roman, "IV")
    eq(5.to_roman, "V")
    eq(9.to_roman, "IX")
    eq(19.to_roman, "XIX")
    eq(4.to_roman, "IV")
    eq(50.to_roman, "L")
    eq(49.to_roman, "XLIX")
    eq(90.to_roman, "XC")
    eq(100.to_roman, "C")
    eq(400.to_roman, "CD")
    eq(500.to_roman, "D")
    eq(900.to_roman, "CM")
    eq(1000.to_roman, "M")
    eq(1880.to_roman, "MDCCCLXXX")
    eq(3333.to_roman, "MMMCCCXXXIII")
    eq(3999.to_roman, "MMMCMXCIX")

  end

  def eq a, b
    assert_equal(a, b)
  end

end
class Integer
  def to_roman
    # TODO Must bigger than 0
    # TODO just valid to less than 3999
    result = ""
    for i in 1..self
      result += "I"
      result.gsub! /IXI$/, "X"
      result.gsub! /VI{4}$/, "IX"
      result.gsub! /IVI$/, "V"
      result.gsub! /I{4}$/, "IV"
      result.gsub! /LX{4}$/, "XC"
      result.gsub! /X{4}$/, "XL"
      result.gsub! /XLX$/, "L"
      result.gsub! /XCX$/, "C"
      result.gsub! /DC{4}$/, "CM"
      result.gsub! /C{4}$/, "CD"
      result.gsub! /CDC$/, "D"
      result.gsub! /CMC$/, "M"
    end
    result
  end

end