Skip to content

Instantly share code, notes, and snippets.

@bootcoder
Created May 13, 2014 00:31
Show Gist options
  • Save bootcoder/343544d27a1556d25cfe to your computer and use it in GitHub Desktop.
Save bootcoder/343544d27a1556d25cfe to your computer and use it in GitHub Desktop.
roman_nums with aki
# ORIGINAL CODE (NOT ALTERED)
# def to_roman(num)
# roman_values = { 'M'=>1000,
# 'D'=>500,
# 'C'=>100,
# 'L'=>50,
# 'X'=>10,
# 'V'=>5,
# 'I'=>1}
# previous_val = {}
# translated = ""
# roman_values.each do |k,v|
# previous_val = {k => v}
# if (num.to_s[0] == 4) || (num.to_s[0] == 9)
# translated << k + previous_val.keys[0]
# else
# (num/v).times {translated << k}
# end
# num %= v
# end
# translated
# end
# def to_roman(num)
# # roman_values = { 'M'=>1000,
# # 'D'=>500,
# # 'C'=>100,
# # 'L'=>50,
# # 'X'=>10,
# # 'IX'=>9,
# # 'V'=>5,
# # 'IV'=>4,
# # 'I'=>1}
# end
=begin
PSEUDO
METHOD to_roman take arg integer
roman_values hash lookup table
variable roman_string = empty string
sample_num = 3540
push_value = num / 1000
roman_string << "M" * push_value
sample_num %= 1000
push_value = num / 100
roman_string << "C" * push_value
sample_num %= 100
push_value = num / 10
roman_string << "X" * push_value
sample_num %= 10
roman_string << "I" * sample_num
return roman_string
OUTPUT string with roman values
=end
# translated = ""
# roman_values.each {|k,v|
# (num/v).times {translated << k}
# num %= v
# }
# translated
# translated = ""
# (num/1000).times {translated << "M"}
# num %= 1000
# (num/500).times {translated << "D"}
# num %= 500
# (num/100).times {translated << "C"}
# num %= 100
# (num/50).times {translated << "L"}
# num %= 50
# (num/10).times {translated << "X"}
# num %= 10
# (num/5).times {translated << "V"}
# num %= 5
# (num/1).times {translated << "I"}
# return translated
# # Your code here
# Drive code... this should print out trues.
# puts to_roman(1) == "I"
# puts to_roman(3) == "III"
# puts to_roman(6) == "VI"
# puts to_roman(4) == "IV"
# puts to_roman(90) == "XC"
# puts to_roman(49) == "XLIX"
# puts to_roman(1)
# puts to_roman(3)
# puts to_roman(6)
# puts to_roman(4)
# puts to_roman(90)
# puts to_roman(49)
# TODO: what other cases could you add to ensure your to_roman method is working?
# pseudo code
# create an empty string that we are going to return at the end
# create times loop starting from 1000 down to 1 for each roman numeral value
#
def to_roman(input)
num = input
roman_string = ""
until num < 1000
num -= 1000
roman_string << "M"
end
until num < 500
if num >= 900
roman_string << "CM"
num -= 900
else
roman_string << "D"
num -= 500
end
end
until num < 100
if num >= 400
roman_string << "CD"
num -= 400
else
roman_string << "C"
num -= 100
end
end
until num < 50
if num >= 90
roman_string << "XC"
num -= 90
else
roman_string << "L"
num -= 50
end
end
until num < 10
if num >= 40
roman_string << "XL"
num -= 40
else
roman_string << "X"
num -= 10
end
end
until num < 5
if num == 9
roman_string << "IX"
num -= 9
else
roman_string << "V"
num -= 5
end
end
until num < 1
if num == 4
roman_string << "IV"
num -= 4
else
roman_string << "I"
num -= 1
end
end
roman_string
end
puts to_roman(1) == "I"
puts to_roman(3) == "III"
puts to_roman(6) == "VI"
puts to_roman(4) == "IV"
puts to_roman(90) == "XC"
puts to_roman(49) == "XLIX"
puts to_roman(1000) == "M"
puts to_roman(924) == "CMXXIV"
puts to_roman(498) == "CDXCVIII"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment