Created
August 15, 2013 16:15
-
-
Save vralex/6242161 to your computer and use it in GitHub Desktop.
Dive into python example. Roman converting.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
__author__ = 'VladimirDel' | |
class OutOfRangeError(ValueError): | |
pass | |
class NotIntegerError(ValueError): | |
pass | |
class InvalidRomanNumeralError(ValueError): | |
pass | |
roman_numeral_map = (('M', 1000), | |
('CM', 900), | |
('D', 500), | |
('CD', 400), | |
('C', 100), | |
('XC', 90), | |
('L', 50), | |
('XL', 40), | |
('X', 10), | |
('IX', 9), | |
('V', 5), | |
('IV', 4), | |
('I', 1)) | |
roman_numeral_pattern = re.compile(''' | |
^ # beginning of string | |
M{0,3} # thousands - 0 to 3 Ms | |
(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 Cs), | |
# or 500-800 (D, followed by 0 to 3 Cs) | |
(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 Xs), | |
# or 50-80 (L, followed by 0 to 3 Xs) | |
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 Is), | |
# or 5-8 (V, followed by 0 to 3 Is) | |
$ # end of string | |
''', re.VERBOSE) | |
def to_roman(n): | |
"""convert integer to numeral""" | |
if not isinstance(n, int): | |
raise NotIntegerError("n should be an integer") | |
if not (0 < n < 4000): | |
raise OutOfRangeError("n should be in range [1..3999]") | |
result = '' | |
for numeral, integer in roman_numeral_map: | |
while n >= integer: | |
result += numeral | |
n -= integer | |
return result | |
def from_roman(s): | |
"""convert numeral to integer""" | |
if not roman_numeral_pattern.search(s): | |
raise InvalidRomanNumeralError('Invalid Roman numeral: {0}'.format(s)) | |
result = 0 | |
index = 0 | |
for numeral, integer in roman_numeral_map: | |
while s[index:index + len(numeral)] == numeral: | |
result += integer | |
index += len(numeral) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment