-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter9.rb
More file actions
56 lines (50 loc) · 1018 Bytes
/
Copy pathchapter9.rb
File metadata and controls
56 lines (50 loc) · 1018 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def old_roman_numeral number
conversion = {
1 => 'I',
5 => 'V',
10 => 'X',
50 => 'L',
100 => 'C',
500 => 'D',
1000 => 'M'
}
convert_to_roman_numeral conversion, number
end
def roman_numeral number
conversion = {
1 => 'I',
4 => 'IV',
5 => 'V',
9 => 'IX',
10 => 'X',
40 => 'XL',
50 => 'L',
90 => 'XC',
100 => 'C',
400 => 'CD',
500 => 'D',
900 => 'CM',
1000 => 'M'
}
convert_to_roman_numeral conversion, number
end
# Found Algorythms
def convert_to_roman_numeral conversion, number
result = ''
conversion.keys.reverse.each do |decimal|
while number >= decimal
number -= decimal
result += conversion[decimal]
end
end
result
end
def convert_to_roman_numeral2 conversion, number
result = ''
conversion.keys.each do |divisor|
quotient, modulus = number.divmod(divisor)
result << conversion[divisor] * quotient
number = modulus
end
result
end