Created
February 12, 2014 04:11
-
-
Save pzula/8949944 to your computer and use it in GitHub Desktop.
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
gem 'minitest' | |
require 'minitest/autorun' | |
require 'minitest/pride' | |
class Numeral | |
def self.convert(number) | |
accumulator, number = reduce_by("", number, 20, "XX") | |
accumulator, number = reduce_by(accumulator, number, 10, "X") | |
accumulator, number = reduce_by(accumulator, number, 5, "V") | |
(1..number).inject(accumulator) { |product| product << "I" } | |
end | |
def self.reduce_by(accumulator, number, reducer, numeral) | |
if number == reducer-1 | |
accumulator += ("I" + numeral) | |
number -= reducer | |
end | |
if number >= reducer | |
accumulator += numeral | |
number -= reducer | |
end | |
[accumulator, number] | |
end | |
end | |
describe Numeral do | |
it("converts 1 to I") { Numeral.convert(1).must_equal("I") } | |
it("converts 2 to II") { Numeral.convert(2).must_equal("II") } | |
it("converts 3 to III") { Numeral.convert(3).must_equal("III") } | |
it("converts 4 to IV") { Numeral.convert(4).must_equal("IV") } | |
it("converts 5 to V") { Numeral.convert(5).must_equal("V") } | |
it("converts 6 to VI") { Numeral.convert(6).must_equal("VI") } | |
it("converts 7 to VII") { Numeral.convert(7).must_equal("VII") } | |
it("converts 8 to VIII") { Numeral.convert(8).must_equal("VIII") } | |
it("converts 9 to IX") { Numeral.convert(9).must_equal("IX") } | |
it("converts 10 to X") { Numeral.convert(10).must_equal("X") } | |
it("converts 11 to XI") { Numeral.convert(11).must_equal("XI") } | |
it("converts 13 to XIII") { Numeral.convert(13).must_equal("XIII") } | |
it("converts 15 to XV") { Numeral.convert(15).must_equal("XV") } | |
it("converts 16 to XVI") { Numeral.convert(16).must_equal("XVI") } | |
it("converts 18 to XVIII") { Numeral.convert(18).must_equal("XVIII") } | |
it("converts 20 to XX") { Numeral.convert(20).must_equal("XX") } | |
it("converts 21 to XXI") { Numeral.convert(21).must_equal("XXI") } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment