Tomas Gavenciak
10 years ago
2 changed files with 17 additions and 3 deletions
@ -1,12 +1,20 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- 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): |
def roman(num): |
||||
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) |
|
||||
nums = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I') |
|
||||
res = "" |
res = "" |
||||
for i, n in zip(ints, nums): |
for i, n in roman_numerals: |
||||
res += n * (num // i) |
res += n * (num // i) |
||||
num %= i |
num %= i |
||||
return res |
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) |
||||
|
|
||||
|
Loading…
Reference in new issue