-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
42 lines (29 loc) · 819 Bytes
/
Solution.rb
File metadata and controls
42 lines (29 loc) · 819 Bytes
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
# @param {Integer} num
# @return {String}
def int_to_roman(num)
# Construct a conversion map.
# valueMap = {1 => 'I', 5 => 'V', 10 => 'X', 50 => 'L', 100 => 'C', 500 => 'D', 1000 => 'M'}
valueMap = Array.new 4
valueMap[0] = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
valueMap[1] = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC']
valueMap[2] = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM']
valueMap[3] = ['', 'M', 'MM', 'MMM']
bits = Array.new 4
bits[0] = num % 10
num /= 10
bits[1] = num %10
num /= 10
bits[2] = num % 10
num /= 10
bits[3] = num % 10
index = 0
str = ''
while index < 4
str = valueMap[index][bits[index]] + str
index += 1
end
str
end
puts int_to_roman 1
puts int_to_roman 10
puts int_to_roman 3999