Module: RomanNumeral
- Defined in:
- lib/fast_roman.rb,
lib/slow_roman.rb,
lib/roman_numeral.rb
Constant Summary collapse
- ROMANS =
(1...4000).reduce([nil],&->(a,i){a<<slow_i_to_roman(i).to_sym})
- INTS =
ROMANS.each_with_index.reduce({},&->(h,p){h[p[0]]=p[1];h})
- DIGIT_TO_I =
I_TO_DIGIT.invert
- I_TO_DIGIT =
{ 1 => :I, 2 => :II, 3 => :III, 4 => :IV, 5 => :V, 6 => :VI, 7 => :VII, 8 => :VIII, 9 => :IX, 10 => :X, 20 => :XX, 30 => :XXX, 40 => :XL, 50 => :L, 60 => :LX, 70 => :LXX, 80 => :LXXX, 90 => :XC, 100 => :C, 200 => :CC, 300 => :CCC, 400 => :CD, 500 => :D, 600 => :DC, 700 => :DCC, 800 => :DCCC, 900 => :CM, 1000 => :M, 2000 => :MM, 3000 => :MMM }
Class Method Summary collapse
- .high_digit_value(i) ⇒ Object
- .i_to_roman(i) ⇒ Object (also: slow_i_to_roman)
- .next_roman_digit(r) ⇒ Object
- .next_roman_digit_of_size(r, size) ⇒ Object
- .roman_to_i(r) ⇒ Object (also: slow_roman_to_i)
Class Method Details
.high_digit_value(i) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/roman_numeral.rb', line 36 def self.high_digit_value i return nil unless -1 < i return [0, 0] if 0 == i shift = 0 v = i while v > 9 v = v/10 shift += 1 end while shift > 0 v *= 10 shift -= 1 end [v, i - v] end |
.i_to_roman(i) ⇒ Object Also known as: slow_i_to_roman
8 9 10 11 12 |
# File 'lib/fast_roman.rb', line 8 def self.i_to_roman i sym = ROMANS[i.to_i] return nil unless sym sym.to_s end |
.next_roman_digit(r) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/slow_roman.rb', line 17 def self.next_roman_digit r result = next_roman_digit_of_size r, 4 return result if result result = next_roman_digit_of_size r, 3 return result if result result = next_roman_digit_of_size r, 2 return result if result next_roman_digit_of_size r, 1 end |
.next_roman_digit_of_size(r, size) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/slow_roman.rb', line 7 def self.next_roman_digit_of_size r, size len = r.length rn = r.slice(0, size) v = DIGIT_TO_I[rn.to_sym] return nil unless v tail = r.slice(size, len - size) tail = nil if '' == tail [v, tail] end |