Skip to content

Instantly share code, notes, and snippets.

@mose
Created March 19, 2013 01:48
Show Gist options
  • Save mose/5193010 to your computer and use it in GitHub Desktop.
Save mose/5193010 to your computer and use it in GitHub Desktop.
Some playing around with python
import re
subnums = [ ('cd',4*'c'),
('xl',4*'x'),
('iv',4*'i'),
('d',5*'c'),
('l',5*'x'),
('v',5*'i'),
('cm',9*'c'),
('xc',9*'x'),
('ix',9*'i')]
nums = [ ('m',1000), ('c',100), ('x', 10), ('i',1) ]
def cal(x):
for (s, l) in subnums:
x = x.replace(s, l)
x = '+'.join(list(x))
for (c, w) in nums:
x = x.replace(c, str(w))
if x == '': x = '0'
return eval(x)
def ask():
roman = raw_input('Input the Roman Number: ').lower()
if (re.search("^m*?(d|cm|cd)?c*?(l|xc|xl)?x*?(ix|v|iv)?(i{1,3})?$",roman)):
print " The Roman number %s is actually %s" % (roman.upper(), cal(roman))
else:
print " This is definately not a Roman Number."
print " Please pay attention."
ask()
ask()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment