-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman_numeral.rb
51 lines (48 loc) · 1.01 KB
/
roman_numeral.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def solution(roman)
mapping = {
"M"=>1000,
"D"=>500,
"C"=>100,
"L"=>50,
"X"=>10,
"V"=>5,
"I"=>1
}
# split string into characters
roman.chars.map do |l|
mapping[l] # replace character with integer value
end.compact.chunk_while do |i,j|
i <= j #
end.map do |chunk|
if chunk.first < chunk.last
chunk.reverse.inject(:-) # handles numerals such as IIX with subtraction
else
chunk.sum # chunk is just a list of numerals such as III
end
end.sum # sums everything up
end
solution('I')
def to_roman(num)
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",
0 => "",
}
return '' if num == 0
roman_numbers.each do |value, letter|
return (letter * (num / value)) << to_roman(num % value) if value <= num
end
to_roman(num % value)
end
to_roman(4)