Skip to content

Instantly share code, notes, and snippets.

@craigjbass
Created August 21, 2019 11:50
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 craigjbass/cac079b2ecca8fa40bcea1dd7872100a to your computer and use it in GitHub Desktop.
Save craigjbass/cac079b2ecca8fa40bcea1dd7872100a to your computer and use it in GitHub Desktop.
class RomanNumerals
def self.to_roman(num)
('I' * num)
.gsub('IIIII', 'V')
.gsub('IIII', 'IV')
.gsub('VV', 'X')
.gsub('VIV', 'IX')
.gsub('XXXXX', 'L')
.gsub('XXXX', 'XL')
.gsub('LL', 'C')
.gsub('LXL', 'XC')
.gsub('CCCCC', 'D')
.gsub('CCCC', 'CD')
.gsub('DD', 'M')
.gsub('DCD', 'CM')
end
end
describe RomanNumerals do
def expect_roman_numerals_to_be(expected_roman_numerals, input)
expect(RomanNumerals.to_roman(input)).to eq(expected_roman_numerals)
end
it 'can romanise numbers' do
expect_roman_numerals_to_be('I', 1)
expect_roman_numerals_to_be('II', 2)
expect_roman_numerals_to_be('III', 3)
expect_roman_numerals_to_be('IV', 4)
expect_roman_numerals_to_be('V', 5)
expect_roman_numerals_to_be('VI', 6)
expect_roman_numerals_to_be('IX', 9)
expect_roman_numerals_to_be('X', 10)
expect_roman_numerals_to_be('XI', 11)
expect_roman_numerals_to_be('XIV', 14)
expect_roman_numerals_to_be('XL', 40)
expect_roman_numerals_to_be('L', 50)
expect_roman_numerals_to_be('XLIV', 44)
expect_roman_numerals_to_be('XLVI', 46)
expect_roman_numerals_to_be('LI', 51)
expect_roman_numerals_to_be('XC', 90)
expect_roman_numerals_to_be('C', 100)
expect_roman_numerals_to_be('XCVI', 96)
expect_roman_numerals_to_be('MMMCMXCIX', 3999)
expect_roman_numerals_to_be('D', 500)
expect_roman_numerals_to_be('CD', 400)
expect_roman_numerals_to_be('CM', 900)
expect_roman_numerals_to_be('M', 1000)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment