fork download
  1. nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
  2. roman = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
  3.  
  4. def from_roman_numeral(roman_numeral):
  5. result = 0
  6. if roman_numeral in roman:
  7. result += nums[roman.index(roman_numeral)]
  8. else:
  9. arr = list(roman_numeral)
  10. for i in range(len(arr) - 1):
  11. if arr[i] >= arr[i + 1]:
  12. result += nums[roman.index(arr[i])]
  13. else:
  14. result -= nums[roman.index(arr[i])]
  15. print(result)
  16. return result
  17.  
  18. from_roman_numeral("V")
  19. from_roman_numeral("XX")
  20. from_roman_numeral("DCCC")
  21. from_roman_numeral("MMMM")
Success #stdin #stdout 0.02s 7308KB
stdin
Standard input is empty
stdout
5
10
700
3000