Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by Cjkjvfnby
def calc_number(val, three_letters):
current_letter, next_letter, next_next_letter = three_letters
if val == 4:
return '%s%s' % (current_letter, next_letter)
elif val == 9:
return '%s%s' % (current_letter, next_next_letter)
elif val >= 5:
return '%s%s' % (next_letter, current_letter * (val - 5))
else:
return current_letter * val
def checkio(data):
result = ''
letters = ['M ', 'CDM', 'XLC', 'IVX']
values = [1000, 100, 10, 1]
for letter, value in zip(letters, values):
val = data // value
data %= value
result += calc_number(val, letter)
return result
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(6) == 'VI', '6'
assert checkio(76) == 'LXXVI', '76'
assert checkio(499) == 'CDXCIX', '499'
assert checkio(3888) == 'MMMDCCCLXXXVIII', '3888'
Nov. 5, 2013
Comments: