Skip to content

Instantly share code, notes, and snippets.

@rymanalo
Created April 2, 2013 01:28
Show Gist options
  • Save rymanalo/5289241 to your computer and use it in GitHub Desktop.
Save rymanalo/5289241 to your computer and use it in GitHub Desktop.
require "test/unit"
class ArabicGenerator
TABLE = {
"I" => 1,
"V" => 5
}
def convert(roman_number)
arabic_num = 0
char = roman_number.split("")
char.each_with_index do |c, i|
if TABLE[char[i + 1]] == nil
arabic_num += TABLE[c]
elsif TABLE[char[i + 1]] > TABLE[c]
return arabic_num += (TABLE[char[i + 1]] - TABLE[c])
else
arabic_num += TABLE[c]
end
end
return arabic_num
end
end
class ArabicGeneratorTest < Test::Unit::TestCase
def setup
@converter = ArabicGenerator.new
end
def test_return_1
assert_equal 1, @converter.convert("I")
end
def test_return_2
assert_equal 2, @converter.convert("II")
end
def test_return_3
assert_equal 3, @converter.convert("III")
end
def test_return_4
assert_equal 4, @converter.convert("IV")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment