tox fixes for the roman library to prevent travis from yelling at us
This commit is contained in:
@@ -16,26 +16,39 @@ http://www.python.org/2.1.1/license.html
|
||||
|
||||
import re
|
||||
|
||||
#Define exceptions
|
||||
class RomanError(Exception): pass
|
||||
class OutOfRangeError(RomanError): pass
|
||||
class NotIntegerError(RomanError): pass
|
||||
class InvalidRomanNumeralError(RomanError): pass
|
||||
|
||||
#Define digit mapping
|
||||
romanNumeralMap = (('M', 1000),
|
||||
# Define exceptions
|
||||
class RomanError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class OutOfRangeError(RomanError):
|
||||
pass
|
||||
|
||||
|
||||
class NotIntegerError(RomanError):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidRomanNumeralError(RomanError):
|
||||
pass
|
||||
|
||||
|
||||
# Define digit mapping
|
||||
romanNumeralMap = (('M', 1000),
|
||||
('CM', 900),
|
||||
('D', 500),
|
||||
('D', 500),
|
||||
('CD', 400),
|
||||
('C', 100),
|
||||
('C', 100),
|
||||
('XC', 90),
|
||||
('L', 50),
|
||||
('L', 50),
|
||||
('XL', 40),
|
||||
('X', 10),
|
||||
('X', 10),
|
||||
('IX', 9),
|
||||
('V', 5),
|
||||
('V', 5),
|
||||
('IV', 4),
|
||||
('I', 1))
|
||||
('I', 1))
|
||||
|
||||
|
||||
def toRoman(n):
|
||||
"""convert integer to Roman numeral"""
|
||||
@@ -51,7 +64,8 @@ def toRoman(n):
|
||||
n -= integer
|
||||
return result
|
||||
|
||||
#Define pattern to detect valid Roman numerals
|
||||
|
||||
# Define pattern to detect valid Roman numerals
|
||||
romanNumeralPattern = re.compile("""
|
||||
^ # beginning of string
|
||||
M{0,4} # thousands - 0 to 4 M's
|
||||
@@ -62,7 +76,8 @@ romanNumeralPattern = re.compile("""
|
||||
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
|
||||
# or 5-8 (V, followed by 0 to 3 I's)
|
||||
$ # end of string
|
||||
""" ,re.VERBOSE)
|
||||
""", re.VERBOSE)
|
||||
|
||||
|
||||
def fromRoman(s):
|
||||
"""convert Roman numeral to integer"""
|
||||
@@ -74,8 +89,7 @@ def fromRoman(s):
|
||||
result = 0
|
||||
index = 0
|
||||
for numeral, integer in romanNumeralMap:
|
||||
while s[index:index+len(numeral)] == numeral:
|
||||
while s[index:index + len(numeral)] == numeral:
|
||||
result += integer
|
||||
index += len(numeral)
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user