fork download
  1. numeralDict = {"I": 1, "V": 5, "X": 10} #Add some more numerals here if you want bigger numbers
  2.  
  3. class numeral:
  4. def __init__(self, numeralString):
  5. self.value = [""]
  6. lastValue = 0
  7. for letter in numeralString:
  8. letterValue = numeralDict[letter]
  9. if letterValue < lastValue:
  10. self.value.append(letter)
  11. else:
  12. self.value[-1] += letter
  13. lastValue = letterValue
  14.  
  15. def addOne(self):
  16. if self.value[-1] == "IV":
  17. self.value[-1] = "V"
  18. elif self.value[-1] == "III":
  19. self.value[-1] = "IV"
  20. else:
  21. self.value[-1] += "I"
  22. return self
  23.  
  24. def __repr__(self):
  25. return " ".join(self.value)
  26.  
  27. #Test
  28. nums = [
  29. numeral("XXIV"),
  30. numeral("XVII"),
  31. numeral("XIII"),
  32. numeral("X"),
  33. ]
  34.  
  35. print(nums)
  36. print([x.addOne() for x in nums])
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
[XX IV, X V II, X III, X]
[XX V, X V III, X IV, XI]