lingo: add to_arabic, the inverse of to_roman

Ported from upstream's toArabic, including the T, MT, A and MA
numerals. Web saves store spell levels as roman numerals, so the .pqw
importer needs it.
This commit is contained in:
Dash committed 2026-10-17 15:56:01 +02:00
1 parent ed5c5cbdc3
commit fced929d77
2 files changed
+45 -1

No files matched your search

+37
View File
@@ -79,6 +79,43 @@ def to_roman(num: int) -> str:
return ret return ret
def to_arabic(roman: str) -> int:
num = 0
rest = roman.upper()
def _arab(ds: str, dn: int) -> bool:
nonlocal num, rest
if not rest.startswith(ds):
return False
rest = rest[len(ds) :]
num += dn
return True
while _arab("T", 10000):
pass
_arab("MT", 9000)
_arab("A", 5000)
_arab("MA", 4000)
while _arab("M", 1000):
pass
_arab("CM", 900)
_arab("D", 500)
_arab("CD", 400)
while _arab("C", 100):
pass
_arab("XC", 90)
_arab("L", 50)
_arab("XL", 40)
while _arab("X", 10):
pass
_arab("IX", 9)
_arab("V", 5)
_arab("IV", 4)
while _arab("I", 1):
pass
return num
def act_name(act: int) -> str: def act_name(act: int) -> str:
if act == 0: if act == 0:
return "Prologue" return "Prologue"
+8 -1
View File
@@ -1,4 +1,4 @@
from pqcli.lingo import indefinite, to_roman from pqcli.lingo import indefinite, to_arabic, to_roman
def test_indefinite_article() -> None: def test_indefinite_article() -> None:
@@ -19,3 +19,10 @@ def test_to_roman() -> None:
assert to_roman(9000) == "MT" assert to_roman(9000) == "MT"
assert to_roman(14999) == "TMACMXCIX" assert to_roman(14999) == "TMACMXCIX"
assert to_roman(23456) == "TTMMMCDLVI" assert to_roman(23456) == "TTMMMCDLVI"
def test_to_arabic_inverts_to_roman() -> None:
for num in [*range(1, 5000), 9000, 9999, 14999, 23456]:
assert to_arabic(to_roman(num)) == num
assert to_arabic("N") == 0
assert to_arabic("iv") == 4