Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Roman Numerals by ClaireLucyJones
def checkio(data):
romanString = ""
numeralNames = ["M","D","C","L","X","V","I"]
numeralValues = [1000, 500, 100, 50, 10, 5, 1]
#Start by getting roman numerals without any replacements of e.g. IV for IIII
for i in range(len(numeralNames)):
while data >= numeralValues[i]:
romanString = romanString + numeralNames[i]
data = data-numeralValues[i]
#Now make the substitutions to abridge the roman numerals
#Replace DCCCC with CM
romanString = romanString.replace("DCCCC","CM")
#Replace CCCC with CD
romanString = romanString.replace("CCCC","CD")
#Replace LXXXX with XC
romanString = romanString.replace("LXXXX","XC")
#Replace XXXX with XL
romanString = romanString.replace("XXXX","XL")
#Replace VIIII with IX
romanString = romanString.replace("VIIII","IX")
#Replace IIII with IV
romanString = romanString.replace("IIII","IV")
return romanString
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'
print('Done! Go Check!')
Dec. 20, 2019