-
-
Save jaderdev/ffcd8afdc65eb397ae88ca1ed1034fcf to your computer and use it in GitHub Desktop.
Convert from Decimal to Roman Numerals in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RomanNumerals | |
RN = { | |
1000 => 'M', | |
500 => 'D', | |
100 => 'C', | |
50 => 'L', | |
10 => 'X', | |
5 => 'V', | |
1 => 'I' | |
} | |
def self.to_roman(decimal) | |
return nil if decimal > 3000 || decimal < 1 | |
return to_roman_loop(decimal) | |
# return to_roman_recursive(decimal, 1000) | |
end | |
def self.to_roman_loop(dec) | |
roman_base = 1000 | |
result = '' | |
while (dec > 0) do | |
factor = dec/roman_base | |
result << multiple_of(factor, roman_base) | |
dec -= (factor*roman_base) | |
roman_base /= 10 | |
end | |
result | |
end | |
def self.to_roman_recursive(dec, roman_base) | |
return '' if dec < 1 || roman_base < 1 | |
return multiple_of(dec/roman_base, roman_base) + to_roman_recursive(dec % roman_base, roman_base/10) | |
end | |
def self.multiple_of(factor, roman_base) | |
# p "#{factor}, #{roman_base}" | |
case factor | |
when 0 | |
'' | |
when 1..3 | |
RN[roman_base]*factor | |
when 4 | |
RN[roman_base]+RN[roman_base*5] | |
when 5 | |
RN[roman_base*5] | |
when 6..8 | |
RN[roman_base*5]+(RN[roman_base]*(factor-5)) | |
when 9 | |
RN[roman_base] + RN[roman_base*10] | |
else | |
raise "Nonsense! Factor is #{factor}. Should be in (0..9)." | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'roman' | |
describe RomanNumerals, '#to_roman' do | |
it 'returns nil for numbers not in (1..3000)' do | |
RomanNumerals.to_roman(0).should be_nil | |
RomanNumerals.to_roman(3001).should be_nil | |
end | |
it 'handles powers of ten' do | |
RomanNumerals.to_roman(1000).should eq 'M' | |
RomanNumerals.to_roman(100).should eq 'C' | |
RomanNumerals.to_roman(10).should eq 'X' | |
RomanNumerals.to_roman(1).should eq 'I' | |
end | |
it 'handles 5x powers of ten' do | |
RomanNumerals.to_roman(500).should eq 'D' | |
RomanNumerals.to_roman(50).should eq 'L' | |
RomanNumerals.to_roman(5).should eq 'V' | |
end | |
it 'handles (1..10) -> (I..X)' do | |
RomanNumerals.to_roman(1).should eq 'I' | |
RomanNumerals.to_roman(2).should eq 'II' | |
RomanNumerals.to_roman(3).should eq 'III' | |
RomanNumerals.to_roman(4).should eq 'IV' | |
RomanNumerals.to_roman(5).should eq 'V' | |
end | |
it 'is not afraid of larger, more complex numbers' do | |
RomanNumerals.to_roman(1221).should eq 'MCCXXI' | |
RomanNumerals.to_roman(43).should eq 'XLIII' | |
RomanNumerals.to_roman(55).should eq 'LV' | |
RomanNumerals.to_roman(2222).should eq 'MMCCXXII' | |
RomanNumerals.to_roman(5).should eq 'V' | |
end | |
it "knows how to party like it's 1999" do | |
RomanNumerals.to_roman(1999).should eq 'MCMXCIX' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment