Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Roman-numerals-Encode/Ruby/roman-numerals-encode-1.rb
Normal file
14
Task/Roman-numerals-Encode/Ruby/roman-numerals-encode-1.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Symbols = { 1=>'I', 5=>'V', 10=>'X', 50=>'L', 100=>'C', 500=>'D', 1000=>'M' }
|
||||
Subtractors = [ [1000, 100], [500, 100], [100, 10], [50, 10], [10, 1], [5, 1], [1, 0] ]
|
||||
|
||||
def roman(num)
|
||||
return Symbols[num] if Symbols.has_key?(num)
|
||||
Subtractors.each do |cutPoint, subtractor|
|
||||
return roman(cutPoint) + roman(num - cutPoint) if num > cutPoint
|
||||
return roman(subtractor) + roman(num + subtractor) if num >= cutPoint - subtractor and num < cutPoint
|
||||
end
|
||||
end
|
||||
|
||||
[1990, 2008, 1666].each do |i|
|
||||
puts "%4d => %s" % [i, roman(i)]
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Symbols = [ [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'] ]
|
||||
|
||||
def arabic_to_roman(arabic)
|
||||
return '' if arabic.zero?
|
||||
Symbols.each { |arabic_rep, roman_rep| return roman_rep + arabic_to_roman(arabic - arabic_rep) if arabic >= arabic_rep }
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
Symbols = [ [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'] ]
|
||||
|
||||
def to_roman(num)
|
||||
Symbols.reduce "" do |memo, (divisor, letter)|
|
||||
div, num = num.divmod(divisor)
|
||||
memo + letter * div
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue