Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
I think this is pretty elegant, probably not "pythonic" though... solution in Clear category for Roman Numerals by nathan.l.cook
def checkio(data):
dec = [1, 5, 10, 50, 100, 500, 1000]
rom = ['I', 'V', 'X', 'L', 'C', 'D', 'M']
str_data = str(data)
str_data = str_data[::-1]
num_digits = len(str_data)
ans = ""
rom_pointer = 0
for place in range(num_digits):
if str_data[place] in ["0", "1", "2", "3"]:
ans = rom[rom_pointer] * int(str_data[place]) + ans
elif str_data[place] in ["4"]:
ans = rom[rom_pointer] + rom[rom_pointer + 1] + ans
elif str_data[place] in ["5", "6", "7", "8"]:
ans = rom[rom_pointer + 1] + rom[rom_pointer] * (int(str_data[place]) - 5) + ans
elif str_data[place] in ["9"]:
ans = rom[rom_pointer] + rom[rom_pointer + 2] + ans
rom_pointer += 2
return ans
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
Oct. 14, 2014
Comments: