Created
April 24, 2016 13:19
-
-
Save vijithmv499/78feac9602a1bdbe71a823d7cb7aed41 to your computer and use it in GitHub Desktop.
Exercism assignment 8 - Number to Roman nemerals conversion (http://exercism.io/exercises/ruby/roman-numerals/readme)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Fixnum | |
| VERSION = 1 | |
| NUMERALS = { | |
| 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 | |
| compute(self) | |
| end | |
| def compute(num,res = '') | |
| NUMERALS.keys.each do |x| | |
| quo,mod = num.divmod(x) | |
| res << NUMERALS[x] * quo | |
| num = mod | |
| end | |
| return res | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment