Skip to content

Instantly share code, notes, and snippets.

@polycarpou
Last active December 25, 2015 06:49
Show Gist options
  • Save polycarpou/6935435 to your computer and use it in GitHub Desktop.
Save polycarpou/6935435 to your computer and use it in GitHub Desktop.
Roman Numerals conversion - day 15 morning todo.
class Fixnum
def set_digits
number_array = self.to_s.chars
#result = ""
digit = []
4.times do |i|
digit[i+1] = number_array.pop.to_i
end
digit
end
def to_roman
result = ""
digit = set_digits
if digit[4]
result += "M" * (digit[4])
end
if digit[3]
if digit[3] == 9
result +="CM"
digit[3] -= 9
end
if digit[3] == 4
result +="CD"
digit[3] -= 4
end
if digit[3] >= 5
result += "D"
digit[3] -= 5
end
result += "C" * (digit[3])
end
if digit[2]
if digit[2] == 9
result +="XC"
digit[2] -= 9
end
if digit[2] == 4
result +="XL"
digit[2] -= 4
end
if digit[2] >= 5
result += "L"
digit[2] -= 5
end
result += "X" * (digit[2])
end
if digit[1]
if digit[1] == 9
result +="IX"
digit[1] -= 9
end
if digit[1] == 4
result +="IV"
digit[1] -= 4
end
if digit[1] >= 5
result += "V"
digit[1] -= 5
end
result += "I" * (digit[1])
end
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment