Skip to content

Instantly share code, notes, and snippets.

@BrianJoyce
Created October 6, 2012 05:40
Show Gist options
  • Save BrianJoyce/3844085 to your computer and use it in GitHub Desktop.
Save BrianJoyce/3844085 to your computer and use it in GitHub Desktop.
Arne roman numerals part 2
class RomanNumerals
def initialize
@reference = {"1"=> "I", "5"=> "V", "10" => "X", "50"=> "L", "100" => "C", "500"=> "D", "1000"=> "M",
"4"=> "IV", "9"=>"IX","40" => "XL", "90" => "XC", "400"=> "CD", "900" => "CM"}
end
def to_roman(number,long_string = '')
@reference.keys.map{|i| i.to_i}.sort.reverse.each do |i|
count,number = difference(number,i.to_i)
long_string = create_string(count,long_string,@reference[i.to_s])
end
return long_string
end
def create_string(times,string,character)
return string if times == 0
create_string(times -=1, string<<character, character)
end
def difference(number,denom,count = 0)
return [count,number] if (number - denom) < 0
difference((number - denom),denom,count+=1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment