1 # Functions to convert between integers and Roman numerals. Doctest examples included.
3 # http://code.activestate.com/recipes/81611-roman-numerals/
4 # PSFL (GPL compatible)
5 from __future__ import print_function, unicode_literals
10 def int_to_roman(input):
12 Convert an integer to Roman numerals.
16 Traceback (most recent call last):
17 ValueError: Argument must be between 1 and 3999
20 Traceback (most recent call last):
21 ValueError: Argument must be between 1 and 3999
23 >>> int_to_roman(1.5) # doctest: +IGNORE_EXCEPTION_DETAIL
24 Traceback (most recent call last):
25 TypeError: expected integer, got <type 'float'>
27 >>> for i in range(1, 21): print(int_to_roman(i))
49 >>> print(int_to_roman(2000))
51 >>> print(int_to_roman(1999))
54 if type(input) != type(1):
55 raise TypeError("expected integer, got %s" % type(input))
56 if not 0 < input < 4000:
57 raise ValueError("Argument must be between 1 and 3999")
58 ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
59 nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
61 for i in range(len(ints)):
62 count = int(input / ints[i])
63 result += nums[i] * count
64 input -= ints[i] * count
67 def roman_to_int(input):
69 Convert a roman numeral to an integer.
71 >>> r = list(range(1, 4000))
72 >>> nums = [int_to_roman(i) for i in r]
73 >>> ints = [roman_to_int(n) for n in nums]
77 >>> roman_to_int('VVVIV')
78 Traceback (most recent call last):
80 ValueError: input is not a valid roman numeral: VVVIV
81 >>> roman_to_int(1) # doctest: +IGNORE_EXCEPTION_DETAIL
82 Traceback (most recent call last):
84 TypeError: expected string, got <type 'int'>
86 Traceback (most recent call last):
88 ValueError: input is not a valid roman numeral: A
89 >>> roman_to_int('IL')
90 Traceback (most recent call last):
92 ValueError: input is not a valid roman numeral: IL
94 if type(input) != type(""):
95 raise TypeError("expected string, got %s" % type(input))
97 nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
98 ints = [1000, 500, 100, 50, 10, 5, 1]
102 raise ValueError("input is not a valid roman numeral: %s" % input)
103 for i in range(len(input)):
105 value = ints[nums.index(c)]
106 # If the next place holds a larger number, this value is negative.
108 nextvalue = ints[nums.index(input[i +1])]
109 if nextvalue > value:
112 # there is no next place.
116 for n in places: sum += n
117 # Easiest test for validity...
118 if int_to_roman(sum) == input:
121 raise ValueError('input is not a valid roman numeral: %s' % input)
125 if not os.path.isdir(path):