Created
February 28, 2012 02:16
-
-
Save zeroeth/1928604 to your computer and use it in GitHub Desktop.
Roman Numerals Kata
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
# Feb, 27th, 2012 | |
# Kevin and Jared at Mad Railers | |
# run with rspec | |
require 'rubygems' | |
require 'rspec' | |
def roman(number) | |
output = "" | |
conversion_table = { 1000 => "M", 900 => "CM", 500 => "D", 400 => "CD", 100 => "C", 90 => "XC", 50 => "L", 40 => "XL", 10 => "X", 9 => "IX", 5 => "V", 4 => "IV", 1 => "I" } | |
leftover = number | |
conversion_table.each do |arabic, numeral| | |
quotient = leftover / arabic | |
remainder = leftover % arabic | |
if quotient > 0 | |
output << numeral * quotient | |
end | |
leftover = remainder | |
end | |
output | |
end | |
describe 'roman numerals' do | |
it "returns 1" do | |
roman(1).should == "I" | |
end | |
it "returns 2" do | |
roman(2).should == "II" | |
end | |
it "returns 3" do | |
roman(3).should == "III" | |
end | |
it "returns 4" do | |
roman(4).should == "IV" | |
end | |
it "returns 5" do | |
roman(5).should == "V" | |
end | |
it "returns 6" do | |
roman(6).should == "VI" | |
end | |
it "returns 7" do | |
roman(7).should == "VII" | |
end | |
it "returns 9" do | |
roman(9).should == "IX" | |
end | |
it "returns 10" do | |
roman(10).should == "X" | |
end | |
it "returns 11" do | |
roman(11).should == "XI" | |
end | |
it "returns 14" do | |
roman(14).should == "XIV" | |
end | |
it "returns 17" do | |
roman(17).should == "XVII" | |
end | |
it "returns 37" do | |
roman(37).should == "XXXVII" | |
end | |
it "returns 40" do | |
roman(40).should == "XL" | |
end | |
it "returns 42" do | |
roman(42).should == "XLII" | |
end | |
it "returns 51" do | |
roman(51).should == "LI" | |
end | |
it "returns 100" do | |
roman(100).should == "C" | |
end | |
it "returns 101" do | |
roman(101).should == "CI" | |
end | |
it "returns 556" do | |
roman(556).should == "DLVI" | |
end | |
it "returns 1205" do | |
roman(1205).should == "MCCV" | |
end | |
it "returns 88" do | |
roman(88).should == "LXXXVIII" | |
end | |
it "returns 999" do | |
roman(999).should == "CMXCIX" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment