Created
April 26, 2012 04:20
-
-
Save ckazu/2495785 to your computer and use it in GitHub Desktop.
RomanNumerals
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
| module RomanNumerals | |
| extend self | |
| CARDINALS = | |
| { 1 => %w|I V X|, | |
| 10 => %w|X L C|, | |
| 100 => %w|C D M|, | |
| 1000 => 'M' | |
| } | |
| def to_roman(num) | |
| return '' unless (1...4000).include? num | |
| ("%04d" % num).each_char.with_index.inject('') do |ret, obj| | |
| char, i = *obj | |
| ret << convert(char.to_i, 10 ** (3 - i)) | |
| end | |
| end | |
| private | |
| def convert(num, base) | |
| min, mid, max = CARDINALS[base] | |
| case remainder = num % 10 | |
| when 1..3; min * remainder | |
| when 4; "#{min}#{mid}" | |
| when 5; mid | |
| when 6..8; "#{mid}#{min * (remainder - 5)}" | |
| when 9; "#{min}#{max}" | |
| else '' | |
| 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 './roman_numerals' | |
| describe RomanNumerals do | |
| subject { RomanNumerals.to_roman(number) } | |
| shared_examples_for 'change to roman' do |number, roman| | |
| let(:number) { number } | |
| it { should == roman } | |
| end | |
| it_behaves_like 'change to roman', -1, '' | |
| it_behaves_like 'change to roman', 0, '' | |
| it_behaves_like 'change to roman', 1, 'I' | |
| it_behaves_like 'change to roman', 2, 'II' | |
| it_behaves_like 'change to roman', 3, 'III' | |
| it_behaves_like 'change to roman', 4, 'IV' | |
| it_behaves_like 'change to roman', 5, 'V' | |
| it_behaves_like 'change to roman', 6, 'VI' | |
| it_behaves_like 'change to roman', 7, 'VII' | |
| it_behaves_like 'change to roman', 8, 'VIII' | |
| it_behaves_like 'change to roman', 9, 'IX' | |
| it_behaves_like 'change to roman', 10, 'X' | |
| it_behaves_like 'change to roman', 49, 'XLIX' | |
| it_behaves_like 'change to roman', 50, 'L' | |
| it_behaves_like 'change to roman', 51, 'LI' | |
| it_behaves_like 'change to roman', 99, 'XCIX' | |
| it_behaves_like 'change to roman', 100, 'C' | |
| it_behaves_like 'change to roman', 101, 'CI' | |
| it_behaves_like 'change to roman', 499, 'CDXCIX' | |
| it_behaves_like 'change to roman', 500, 'D' | |
| it_behaves_like 'change to roman', 501, 'DI' | |
| it_behaves_like 'change to roman', 999, 'CMXCIX' | |
| it_behaves_like 'change to roman', 1000, 'M' | |
| it_behaves_like 'change to roman', 1001, 'MI' | |
| it_behaves_like 'change to roman', 1999, 'MCMXCIX' | |
| it_behaves_like 'change to roman', 2000, 'MM' | |
| it_behaves_like 'change to roman', 2001, 'MMI' | |
| it_behaves_like 'change to roman', 3999, 'MMMCMXCIX' | |
| it_behaves_like 'change to roman', 4000, '' | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment