numeralDict = {"I": 1, "V": 5, "X": 10} #Add some more numerals here if you want bigger numbers
class numeral:
def __init__(self, numeralString):
self.value = [""]
lastValue = 0
for letter in numeralString:
letterValue = numeralDict[letter]
if letterValue < lastValue:
self.value.append(letter)
else:
self.value[-1] += letter
lastValue = letterValue
def addOne(self):
if self.value[-1] == "IV":
self.value[-1] = "V"
elif self.value[-1] == "III":
self.value[-1] = "IV"
else:
self.value[-1] += "I"
return self
def __repr__(self):
return " ".join(self.value)
#Test
nums = [
numeral("XXIV"),
numeral("XVII"),
numeral("XIII"),
numeral("X"),
]
print(nums)
print([x.addOne() for x in nums])
bnVtZXJhbERpY3QgPSB7IkkiOiAxLCAiViI6IDUsICJYIjogMTB9ICNBZGQgc29tZSBtb3JlIG51bWVyYWxzIGhlcmUgaWYgeW91IHdhbnQgYmlnZ2VyIG51bWJlcnMKCmNsYXNzIG51bWVyYWw6CglkZWYgX19pbml0X18oc2VsZiwgbnVtZXJhbFN0cmluZyk6CgkJc2VsZi52YWx1ZSA9IFsiIl0KCQlsYXN0VmFsdWUgPSAwCgkJZm9yIGxldHRlciBpbiBudW1lcmFsU3RyaW5nOgoJCQlsZXR0ZXJWYWx1ZSA9IG51bWVyYWxEaWN0W2xldHRlcl0KCQkJaWYgbGV0dGVyVmFsdWUgPCBsYXN0VmFsdWU6CgkJCQlzZWxmLnZhbHVlLmFwcGVuZChsZXR0ZXIpCgkJCWVsc2U6CgkJCQlzZWxmLnZhbHVlWy0xXSArPSBsZXR0ZXIKCQkJbGFzdFZhbHVlID0gbGV0dGVyVmFsdWUKCglkZWYgYWRkT25lKHNlbGYpOgoJCWlmIHNlbGYudmFsdWVbLTFdID09ICJJViI6CgkJCXNlbGYudmFsdWVbLTFdID0gIlYiCgkJZWxpZiBzZWxmLnZhbHVlWy0xXSA9PSAiSUlJIjoKCQkJc2VsZi52YWx1ZVstMV0gPSAiSVYiCgkJZWxzZToKCQkJc2VsZi52YWx1ZVstMV0gKz0gIkkiCgkJcmV0dXJuIHNlbGYKCglkZWYgX19yZXByX18oc2VsZik6CgkJcmV0dXJuICIgIi5qb2luKHNlbGYudmFsdWUpCgojVGVzdApudW1zID0gWwoJbnVtZXJhbCgiWFhJViIpLAoJbnVtZXJhbCgiWFZJSSIpLAoJbnVtZXJhbCgiWElJSSIpLAoJbnVtZXJhbCgiWCIpLApdCgpwcmludChudW1zKQpwcmludChbeC5hZGRPbmUoKSBmb3IgeCBpbiBudW1zXSk=