mamweb/seminar/utils.py

21 lines
552 B
Python
Raw Permalink Normal View History

2015-05-14 00:49:20 +02:00
# -*- 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'))
2015-05-14 00:49:20 +02:00
def roman(num):
res = ""
for i, n in roman_numerals:
2015-05-14 00:49:20 +02:00
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)
2015-05-14 00:49:20 +02:00