ruby - I'm attempting to convert my function parameters to a key within a hash but i am stuck -
i'm attempting convert function perimeters key within hash.
i want to_roman function take whatever parameters give , convert roman numerals. here waht have far:
def to_roman(num) result = "" numerals = { i: 1, v: 5, x: 10, l: 50, c: 100, d: 500, m: 1000 } numerals.each |roman, numbers| if num % numbers == 0 result << roman.to_s end end result end
i've reversed hash because want first calculate how many time highest number goes num , maintain working there.
def to_roman(num) result = "" numerals = { 'm'=> 1000, 'cm' => 900, 'd'=> 500, 'cd'=> 400, 'c'=> 100, 'xc'=>90, 'l'=> 50, 'xl'=>40, 'x'=> 10, 'ix'=> 9, 'v'=> 5, 'iv'=> 4, 'i'=> 1 } numerals.each |roman, numbers| result << (roman *(num/numbers)) num-= num/numbers*numbers end result end
ruby
No comments:
Post a Comment