fork download
  1. #!/usr/bin/env python3
  2.  
  3. def romanToInt(s):
  4. dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
  5.  
  6. sum = 0
  7. y = 0 # 比較用の変数
  8. for i in s[::-1]: # sは入力された文字列のこと
  9. if y <= dict[i] or y == 0:
  10. sum += dict[i]
  11. y = dict[i]
  12. else:
  13. sum -= dict[i]
  14. return sum
  15.  
  16. if __name__ == '__main__':
  17. print(romanToInt('MC'))
  18. print(romanToInt('IV'))
  19.  
Success #stdin #stdout 0.05s 9612KB
stdin
Standard input is empty
stdout
1100
4