Skip to content

Instantly share code, notes, and snippets.

@JDeuce
Created December 12, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDeuce/7355747f71b23188deddd0a27a8c6b28 to your computer and use it in GitHub Desktop.
Save JDeuce/7355747f71b23188deddd0a27a8c6b28 to your computer and use it in GitHub Desktop.
_rdecode = dict(zip('MDCLXVI', (1000, 500, 100, 50, 10, 5, 1)))
def decode( roman ):
result = 0
for r, r1 in zip(roman, roman[1:]):
rd, rd1 = _rdecode[r], _rdecode[r1]
result += -rd if rd < rd1 else rd
return result + _rdecode[roman[-1]]
if __name__ == '__main__':
for r in 'MCMXC MMVIII MDCLXVI'.split():
print( r, decode(r) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment