43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
dic = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
|
|
rdic = {1:'I', 2:'II', 3:'III', 4:'IV', 5:'V', 6:'VI', 7:'VII', 8:'VIII', 9:'IX',
|
|
10:'X', 20:'XX', 30:'XXX', 40:'XL', 50:'L', 60:'LX', 70:'LXX', 80:'LXXX',
|
|
90:'XC', 100:'C', 200:'CC', 300:'CCC', 400:'CD', 500:'D', 600:'DC',
|
|
700:'DCC', 800:'DCCC', 900:'CM', 1000:'M'}
|
|
|
|
def rtoi(ss):
|
|
out = dic.get(ss[-1])
|
|
for i in xrange(len(ss) - 1):
|
|
if dic.get(ss[i]) < dic.get(ss[i + 1]):
|
|
out -= dic.get(ss[i])
|
|
else:
|
|
out += dic.get(ss[i])
|
|
return out
|
|
|
|
def itor(num):
|
|
out = ''
|
|
thousand = num - num % 1000
|
|
if num > 0:
|
|
out += rdic.get(1000) * (thousand / 1000)
|
|
num %= 1000
|
|
hundred = num - num % 100
|
|
if hundred > 0:
|
|
out += rdic.get(hundred)
|
|
num %= 100
|
|
teen = num - num % 10
|
|
if teen > 0:
|
|
out += rdic.get(teen)
|
|
num %= 10
|
|
if num > 0:
|
|
out += rdic.get(num)
|
|
return out
|
|
|
|
ff = open('../roman.txt', 'r')
|
|
lis = ff.readlines()
|
|
ff.close()
|
|
|
|
count = 0
|
|
for i in lis:
|
|
new = itor(rtoi(i.rstrip()))
|
|
count += abs(len(new) - len(i.rstrip()))
|
|
print count
|