20 lines
552 B
Python
20 lines
552 B
Python
# -*- coding: utf-8 -*-
|
|
|
|
roman_numerals = zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
|
|
('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I'))
|
|
|
|
def roman(num):
|
|
res = ""
|
|
for i, n in roman_numerals:
|
|
res += n * (num // i)
|
|
num %= i
|
|
return res
|
|
|
|
def from_roman(rom):
|
|
if not rom:
|
|
return 0
|
|
for i, n in roman_numerals:
|
|
if rom.upper().startswith(n):
|
|
return i + from_roman(rom[len(n):])
|
|
raise Exception('Invalid roman numeral: "%s"', rom)
|
|
|