Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. res = 0
  2. c = False
  3.  
  4. CONV_TABLE = ((1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'),
  5. (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'),
  6. (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I'))
  7.  
  8.  
  9. def roman_to_arab(txt):
  10. txt = txt.upper()
  11. ret = 0
  12. for arab, roman in CONV_TABLE:
  13. while txt.startswith( roman ):
  14. ret += arab
  15. txt = txt[ len( roman ): ]
  16. return ret
  17.  
  18. x = [i for i in input()]
  19. for i in range(len(x)):
  20. if c:
  21. c = False
  22. continue
  23. elif i == len(x):
  24. res += roman_to_arab(x[i])
  25.  
  26.  
  27. if x[i] == 'I':
  28. if x[i + 1] == 'V':
  29. c = True
  30. res += 4
  31. elif x[i + 1] == 'X':
  32. c = True
  33. res += 9
  34.  
  35. elif x[i] == 'X':
  36. if x[i + 1] == 'L':
  37. res += 40
  38. c = True
  39. elif x[i + 1] == 'C':
  40. res += 90
  41. c = True
  42. elif x[i] == 'C':
  43. if x[i + 1] == 'D':
  44. res += 400
  45. c = True
  46. elif x[i + 1] == 'M':
  47. res += 900
  48. c = True
  49. else:
  50. res += roman_to_arab(x[i])
  51. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement