Created
August 30, 2013 00:53
-
-
Save broguinn/6385198 to your computer and use it in GitHub Desktop.
roman_numerals
This file contains 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
def roman_numerals(number) | |
if number < 0 || number > 3000 | |
raise TypeError.new 'Must be any number from 0 to 3000' | |
end | |
ones = {0 => '', 1 => 'I', 2 => 'II', 3 => 'III', 4 => 'IV', 5 => 'V', 6 => 'VI', 7 => 'VII', 8 => 'VIII', 9 => 'IX'} | |
tens = {0 => '', 1 => 'X', 2 => 'XX', 3 => 'XXX', 4 => 'XL', 5 => 'L', 6 => 'LX', 7 => 'LXX', 8 => 'LXXX', 9 => 'XC'} | |
hundreds = {0 => '', 1 => 'C', 2 => 'CC', 3 => 'CCC', 4 => 'CD', 5 => 'D', 6 => 'DC', 7 => 'DCC', 8 => 'DCCC', 9 => 'CM'} | |
thousands = {0 => '', 1 => 'M', 2=> 'MM', 3 => 'MMM'} | |
ones_numeral = ones[number % 10] | |
tens_numeral = tens[(number % 100) / 10] | |
hundreds_numeral = hundreds[(number % 1000) / 100] | |
thousands_numeral = thousands[(number % 10000) / 1000] | |
thousands_numeral + hundreds_numeral + tens_numeral + ones_numeral | |
end |
This file contains 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 'rspec' | |
require 'roman_numerals' | |
describe 'roman_numerals' do | |
it 'raises an exception for an integer less than 0 or greater than 3000' do | |
expect {roman_numerals(-3)}.to raise_exception | |
expect {roman_numerals(3001)}.to raise_exception | |
expect {roman_numerals(1.5)}.to raise_exception | |
end | |
it 'raises an exception for a non-number input' do | |
expect {roman_numerals("foo")}.to raise_exception | |
expect {roman_numerals(false)}.to raise_exception | |
expect {roman_numerals([1, 2, 3])}.to raise_exception | |
end | |
it 'translates single digits into roman numerals' do | |
roman_numerals(1).should eq "I" | |
roman_numerals(2).should eq "II" | |
roman_numerals(3).should eq "III" | |
roman_numerals(4).should eq "IV" | |
roman_numerals(5).should eq "V" | |
roman_numerals(6).should eq "VI" | |
roman_numerals(7).should eq "VII" | |
roman_numerals(8).should eq "VIII" | |
roman_numerals(9).should eq "IX" | |
end | |
it 'translates double digits into roman numerals' do | |
roman_numerals(10).should eq "X" | |
roman_numerals(20).should eq "XX" | |
roman_numerals(30).should eq "XXX" | |
roman_numerals(40).should eq "XL" | |
roman_numerals(50).should eq "L" | |
roman_numerals(60).should eq "LX" | |
roman_numerals(70).should eq "LXX" | |
roman_numerals(80).should eq "LXXX" | |
roman_numerals(90).should eq "XC" | |
end | |
it 'translates three-digit numbers digits into roman numerals' do | |
roman_numerals(100).should eq "C" | |
roman_numerals(200).should eq "CC" | |
roman_numerals(300).should eq "CCC" | |
roman_numerals(400).should eq "CD" | |
roman_numerals(500).should eq "D" | |
roman_numerals(600).should eq "DC" | |
roman_numerals(700).should eq "DCC" | |
roman_numerals(800).should eq "DCCC" | |
roman_numerals(900).should eq "CM" | |
end | |
it 'translates numbers above 999 into roman numerals' do | |
roman_numerals(1000).should eq "M" | |
roman_numerals(2000).should eq "MM" | |
roman_numerals(3000).should eq "MMM" | |
end | |
it 'translates any number from 0 to 3000' do | |
roman_numerals(0).should eq "" | |
roman_numerals(2222).should eq "MMCCXXII" | |
roman_numerals(345).should eq "CCCXLV" | |
roman_numerals(2006).should eq "MMVI" | |
roman_numerals(634).should eq "DCXXXIV" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment