Skip to content

Instantly share code, notes, and snippets.

@nextacademy-private
Created August 31, 2014 09:06
Show Gist options
  • Save nextacademy-private/2139a7792364775ca9eb to your computer and use it in GitHub Desktop.
Save nextacademy-private/2139a7792364775ca9eb to your computer and use it in GitHub Desktop.
Roman Numerals
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?
@peihongtan
Copy link

def to_roman(n)
roman_numbers = {
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',
}
to_roman = ""



roman_numbers.each do |value, letter|
    to_roman << letter*(n / value)
    n %= value
end
return to_roman 

end

p to_roman (5432)
p to_roman (32)
p to_roman (1997)

@nech0701
Copy link

nech0701 commented Mar 2, 2016

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