/roman_numerals.rb Secret
Created
August 31, 2014 09:06
-
Star
(0)
You must be signed in to star a gist -
Fork
(135)
You must be signed in to fork a gist
-
-
Save nextacademy-private/2139a7792364775ca9eb to your computer and use it in GitHub Desktop.
Roman Numerals
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
def to_roman(num) | |
# Your code here | |
end | |
# Drive code... this should print out trues. | |
puts to_roman(1) == "I" | |
puts to_roman(3) == "III" | |
puts to_roman(6) == "VI" | |
# TODO: what other cases could you add to ensure your to_roman method is working? |
def to_roman(arabic_numeral)
result = ""
key_value = {
1000 =>"M",
900 =>"CM",
500 =>"D",
400 =>"CD",
100 =>"C",
90 =>"XC",
50 =>"L",
40 =>"XL",
10 =>"X",
9 =>"IX",
5 =>"V",
4 =>"IV",
1 =>"I"
}
key_value.each do |arabic_key,roman_value|
if(arabic_numeral/arabic_key) > 0
result << roman_value * (arabic_numeral/arabic_key)
arabic_numeral %= arabic_key
end
end
return result
end
puts "please key in arabic numeral"
a = gets.chomp
to_roman(a.to_i)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def to_roman(n)
roman_numbers = {
1000 => 'M',
900 => 'CM',
end
p to_roman (5432)
p to_roman (32)
p to_roman (1997)