Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MIDDLE = 5
- REM_TO_REVERSE = 4
- SYMBOLS = %w(I V X L C D M)
- # @param {Integer} num
- # @return {String}
- def int_to_roman(num)
- num.to_s.reverse.chars.each_with_object(i: 0, result: '') do |val, memo|
- memo[:result] = num_to_roman(val.to_i, memo[:i] * 2) + memo[:result]
- memo[:i] += 1
- end[:result]
- end
- def num_to_roman(num, order = 0)
- return '' if num == 0
- int = num / MIDDLE
- rem = num % MIDDLE
- return SYMBOLS[order] + SYMBOLS[order+int+1] if rem == REM_TO_REVERSE
- return (int > 0 ? SYMBOLS[order+1] : '') + SYMBOLS[order] * rem
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement