Skip to content

Instantly share code, notes, and snippets.

@alainravet
Created June 5, 2016 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alainravet/ed707a80114c552c0deb286f1ecd91ab to your computer and use it in GitHub Desktop.
Save alainravet/ed707a80114c552c0deb286f1ecd91ab to your computer and use it in GitHub Desktop.
class Fixnum
def to_roman
[].tap do |res|
a, b = self.divmod(10) ; res << ([''] + %w(I II III IV V VI VII VIII IX))[b]
a, b = a.divmod(10) ; res << ([''] + %w(X XX XXX XL L LX LXX LXXX XC))[b]
a, b = a.divmod(10) ; res << ([''] + %w(C CC CCC CD D DC DCC DCCC CM))[b]
a, b = a.divmod(10) ; res << ([''] + %w(M MM MMM Mv v vM vMM vMMM Mx))[b]
a, b = a.divmod(10) ; res << ([''] + %w(x xx xxx xl l lx lxx lxxx xc))[b]
a, b = a.divmod(10) ; res << ([''] + %w(c cc ccc cd d dc dcc dccc cm))[b]
end.reverse.join
end
end
# class Fixnum
# def to_roman
# raise "too big" if self > 10_999
# roman_additive_to_roman_substractive(self.to_roman_additive)
# end
#
# def to_roman_additive
# tens, units = self .divmod(10)
# hundreds, tens = tens .divmod(10)
# thousands, hundreds = hundreds.divmod(10)
#
# 'M'*thousands + 'C'*hundreds + 'X'*tens + 'I'*units
# end
#
# def roman_additive_to_roman_substractive(s)
# s.
# gsub('IIIIIIIII', 'IX').
# gsub('IIIII', 'V').
# gsub('IIII', 'IV').
# gsub('XXXXXXXXX', 'XC').
# gsub('XXXXX', 'L').
# gsub('XXXX', 'XL').
# gsub('CCCCCCCCC', 'CM').
# gsub('CCCCC', 'D').
# gsub('CCCC', 'CD')
# end
# end
require 'minitest/autorun'
require_relative 'fixnum_to_roman'
class RomanTest < MiniTest::Unit::TestCase
def test_0
assert_equal '', 0.to_roman
end
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_10
assert_equal 'X', 10.to_roman
end
def test_27
assert_equal 'XXVII', 27.to_roman
end
def test_50
assert_equal 'L', 50.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_1100
assert_equal 'MC', 1100.to_roman
end
def test_1024
assert_equal 'MXXIV', 1024.to_roman
end
def test_3000
assert_equal 'MMM', 3000.to_roman
end
def test_5001_to_8888
assert_equal 'vI', 5001.to_roman
assert_equal 'vDLV', 5555.to_roman
assert_equal 'vMMMDCCCLXXXVIII', 8888.to_roman
end
def test_10_000_to_39_999
assert_equal 'x', 10_000.to_roman
assert_equal 'xxxMxCMXCIX', 39_999.to_roman
end
def test_50_000
assert_equal 'l', 50_000.to_roman
assert_equal 'lxxx', 80_000.to_roman
assert_equal 'xc', 90_000.to_roman
end
def test_100_000_to_
assert_equal 'c', 100_000.to_roman
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment