Skip to content

Instantly share code, notes, and snippets.

@Andsbf
Created March 5, 2015 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andsbf/136ffa690fb39520b469 to your computer and use it in GitHub Desktop.
Save Andsbf/136ffa690fb39520b469 to your computer and use it in GitHub Desktop.
Roman Numerals Exercise
require 'pry'
# require 'pry-nav'
def to_roman(num)
@roman_num = ""
@num = num
roman_M(@num)
roman_D(@num)
roman_C(@num)
roman_L(@num)
roman_X(@num)
roman_V(@num)
roman_I(@num)
@roman_num
end
def roman_M(num)
if num >= 900 && num <1_000
(@roman_num << "DM")
@num -=900
else
@roman_num << "M"*(num/1000)
@num = num % 1000
end
# binding.pry
end
def roman_D(num)
if num >= 400 && num <500
(@roman_num << "CD")
@num -=400
else
@roman_num << "D"*(num/500)
@num = num % 500
end
# binding.pry
end
def roman_C(num)
if num >= 90 && num <100
(@roman_num << "XC")
@num -=90
else
@roman_num << "C"*(num/100)
@num = num % 100
end
# binding.pry
end
def roman_L(num)
if num >= 40 && num <50
(@roman_num << "XL")
@num -=40
else
@roman_num << "L"*(num/50)
@num = num % 50
end
# binding.pry
end
def roman_X(num)
if num == 9
(@roman_num << "IX")
@num -=9
else
@roman_num << "X"*(num/10)
@num = num % 10
end
# binding.pry
end
def roman_V(num)
if num == 4
(@roman_num << "IV")
@num -=4
else
@roman_num << "V"*(num/5)
@num = num % 5
end
# binding.pry
end
def roman_I(num)
num.times do
@roman_num << "I"
end
# binding.pry
end
# Drive code... this should print out trues.
# puts to_roman(1) == "I"
# puts to_roman(3) == "III"
# puts to_roman(6) == "VI"
# TODO: what other cases could you add to ensure your to_roman method is working?
puts "4 in roman is " + to_roman(4)
puts "9 in roman is " + to_roman(9)
binding.pry
puts "13 in roman is " + to_roman(13)
puts "14 in roman is " + to_roman(14)
puts "44 in roman is " + to_roman(44)
puts "944 in roman is " + to_roman(944)
puts "1454 in roman is " + to_roman(1454)
puts "1646 in roman is " + to_roman(1646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment