Got it passing my test by rearranging the values that were present in bug cases in the mapping (cleaned up multiletter expressions by *):
# Roman Numerals
# 2010-06-15, Eljakim Schrijvers
# bug testing, translation of variables and reformating
# Tony Veijalainen 2010-06-16
mapping = [ ('d',5*'c'),
('l',5*'x'),
('v',5*'i'),
('cm',9*'c'),
('xc',9*'x'),
('ix',9*'i'),
('cd',4*'c'),
('iv',4*'i'),
('xl',4*'x')]
mapping.reverse()
bignums = [ ('m',1000), ('c',100), ('x', 10), ('i',1) ]
def fromroman(x):
for (shortv, longv) in mapping: x= x.replace(shortv, longv)
x = '+'.join(list(x))
for (character, word) in bignums: x = x.replace(character, str(word))
if x=='': x = '0'
return eval(x)
def toroman(x):
val = ''
for (character, word) in bignums:
val = val + (x / word) * character
x = x % word
for (shortv, longv) in mapping:
val = val.replace(longv,shortv)
return val
bugs=0
for i in range(5000):
if fromroman(toroman(i)) != i:
print ('Bug found: %i, %s, %s' % (i,toroman(i),fromroman(toroman(i))))
bugs+=1
print(('Number of bugs found: %i' % bugs) if bugs else 'Test passed!')
""" Output:
Test passed!
"""