Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
roman_numerals1 solution in Clear category for Roman Numerals by DJO3
def checkio(data):
nums = [('M', 1000), ('D', 500), ('C', 100), ('L', 50), ('X', 10), ('V', 5), ('I', 1)]
numeral = str()
while data > 0:
for roman, value in nums:
if data - value >= 0:
data -= value
numeral = numeral + roman
break
for item in range(len(nums)):
roman = nums[item][0]
last_roman = nums[item-1][0]
if numeral.count(roman) == 4:
numeral = numeral.replace(roman*4, roman + last_roman)
for item in range(len(numeral)):
if item >= 2 and item < len(numeral):
first = numeral[item-2]
second = numeral[item-1]
third = numeral[item]
if first == third:
val = dict(nums)[first] * 2
for letter in nums:
if letter[1] == val:
roman = letter[0]
numeral = numeral.replace(first + second + third, second + roman)
return numeral
#replace this for solution
return ""
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'
May 17, 2015