Skip to content

Instantly share code, notes, and snippets.

@kikito
Created January 2, 2011 16:39
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 kikito/762635 to your computer and use it in GitHub Desktop.
Save kikito/762635 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rspec'
class Fixnum
ROMANS = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }
def to_roman
ROMANS.detect do |roman, arabic|
diff = self - arabic
return "#{roman}#{diff.to_roman}" if diff >= 0
end
end
end
describe "Roman numerals" do
# Is this wrong?
TRANSFORMATIONS = {
I: 1, II: 2, III: 3, IV: 4, V: 5, VI: 6, VII: 7, VIII: 8, IX: 9, X: 10, XI: 11, XII: 12, XIV: 14, XV: 15,
XIX: 19, XXXIX: 39, XL: 40, XLI: 41, L: 50, LXXXIX: 89, XC: 90, XCIX: 99, C: 100, CCCXCIX: 399, CD: 400,
D: 500, DCCCXCIX: 899, CM: 900, M: 1000, MMXI: 2011
}
TRANSFORMATIONS.each do |roman, arabic|
it "transforms #{arabic} to #{roman}" do
arabic.to_roman.should == roman.to_s
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment