#!/usr/bin/python def romanToInt(s): d = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000, 'IV':4, \ 'IX':9, 'XL':40, 'XC':90, 'CD':400, 'CM':900} n = 0 i = 0 while i < len(s)-1: if d[s[i]] >= d[s[i+1]]: n += d[s[i]] i += 1 else: n += d[s[i]+s[i+1]] i += 2 if i < len(s): n += d[s[i]] return n if __name__ == '__main__': print romanToInt('III') print romanToInt('IV') print romanToInt('IX') print romanToInt('LVIII') print romanToInt('MCMXCIV')