Skip to content

Instantly share code, notes, and snippets.

@cody-code-wy
Last active May 26, 2016 01:29
Show Gist options
  • Save cody-code-wy/dd636885e347019f949fdb845f1247d8 to your computer and use it in GitHub Desktop.
Save cody-code-wy/dd636885e347019f949fdb845f1247d8 to your computer and use it in GitHub Desktop.
@ids = [[1,'I'],[5,'V'],[10,'X'],[50,'L'],[100,'C'],[500,'D'],[1000,'M']]
def to_roman(num)
str = ''
@ids.reverse.each { |id|
until num / id[0] == 0
str += id[1]
num -= id[0]
end
if num == id[0]-1 && check[0] != 1
str += "I" + id[1]
num -= id[0]-1
end
}
return str
end
def err_msg(num,expected)
return "Error! #{num} => #{to_roman(num).inspect} Expected #{num} => #{expected.inspect}"
end
def do_test(num,expected)
return to_roman(num) == expected ? true : err_msg(num,expected)
end
tests = [[2,"II"],[5,"V"],[9,"IX"],[10,"X"],[11,"XI"],[15,"XV"],[19,"XIX"]]
tests += [[50,"L"],[60,"LX"],[65,"LXV"],[69,"LXIX"],[100,"C"],[600,"DC"]]
tests << [1669,"MDCLXIX"]
puts "Testing Identities"
@ids.each { |id| puts do_test(id[0],id[1]) }
puts "Testing Non Identities"
tests.each { |test| puts do_test(test[0],test[1]) }
@cody-code-wy
Copy link
Author

By changing the ids array you can create now identities (which will automatically be tested!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment