fork download
  1. def int_to_roman(input):
  2. """
  3. Convert an integer to Roman numerals.
  4.  
  5. Examples:
  6. >>> int_to_roman(0)
  7. Traceback (most recent call last):
  8. ValueError: Argument must be between 1 and 3999
  9.  
  10. >>> int_to_roman(-1)
  11. Traceback (most recent call last):
  12. ValueError: Argument must be between 1 and 3999
  13.  
  14. >>> int_to_roman(1.5)
  15. Traceback (most recent call last):
  16. TypeError: expected integer, got <type 'float'>
  17.  
  18. >>> for i in range(1, 21): print int_to_roman(i)
  19. ...
  20. I
  21. II
  22. III
  23. IV
  24. V
  25. VI
  26. VII
  27. VIII
  28. IX
  29. X
  30. XI
  31. XII
  32. XIII
  33. XIV
  34. XV
  35. XVI
  36. XVII
  37. XVIII
  38. XIX
  39. XX
  40. >>> print int_to_roman(2000)
  41. MM
  42. >>> print int_to_roman(1999)
  43. MCMXCIX
  44. """
  45. if type(input) != type(1):
  46. raise TypeError, "expected integer, got %s" % type(input)
  47. if not 0 < input < 4000:
  48. raise ValueError, "Argument must be between 1 and 3999"
  49. ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
  50. nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
  51. result = ""
  52. for i in range(len(ints)):
  53. count = int(input / ints[i])
  54. result += nums[i] * count
  55. input -= ints[i] * count
  56. return result
  57.  
  58.  
  59.  
  60. def roman_to_int(input):
  61. """
  62. Convert a roman numeral to an integer.
  63.  
  64. >>> r = range(1, 4000)
  65. >>> nums = [int_to_roman(i) for i in r]
  66. >>> ints = [roman_to_int(n) for n in nums]
  67. >>> print r == ints
  68. 1
  69.  
  70. >>> roman_to_int('VVVIV')
  71. Traceback (most recent call last):
  72. ...
  73. ValueError: input is not a valid roman numeral: VVVIV
  74. >>> roman_to_int(1)
  75. Traceback (most recent call last):
  76. ...
  77. TypeError: expected string, got <type 'int'>
  78. >>> roman_to_int('a')
  79. Traceback (most recent call last):
  80. ...
  81. ValueError: input is not a valid roman numeral: A
  82. >>> roman_to_int('IL')
  83. Traceback (most recent call last):
  84. ...
  85. ValueError: input is not a valid roman numeral: IL
  86. """
  87. if type(input) != type(""):
  88. raise TypeError, "expected string, got %s" % type(input)
  89. input = input.upper()
  90. nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
  91. ints = [1000, 500, 100, 50, 10, 5, 1]
  92. places = []
  93. for c in input:
  94. if not c in nums:
  95. raise ValueError, "input is not a valid roman numeral: %s" % input
  96. for i in range(len(input)):
  97. c = input[i]
  98. value = ints[nums.index(c)]
  99. # If the next place holds a larger number, this value is negative.
  100. try:
  101. nextvalue = ints[nums.index(input[i +1])]
  102. if nextvalue > value:
  103. value *= -1
  104. except IndexError:
  105. # there is no next place.
  106. pass
  107. places.append(value)
  108. sum = 0
  109. for n in places: sum += n
  110. # Easiest test for validity...
  111. if int_to_roman(sum) == input:
  112. return sum
  113. else:
  114. raise ValueError, 'input is not a valid roman numeral: %s' % input
Success #stdin #stdout 0.01s 7200KB
stdin
XXL
stdout
Standard output is empty