Skip to content

Instantly share code, notes, and snippets.

@AlseinX
Created December 24, 2018 04:26
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 AlseinX/93cee396c5cebc5b2332d7dfc1bb48a4 to your computer and use it in GitHub Desktop.
Save AlseinX/93cee396c5cebc5b2332d7dfc1bb48a4 to your computer and use it in GitHub Desktop.
Formatting any positive integer to roman numerals
def roman(num: int) -> str:
chlist = "VXLCDM"
rev = [int(ch) for ch in reversed(str(num))]
chlist = ["I"] + [chlist[i % len(chlist)] + "\u0304" * (i // len(chlist))
for i in range(0, len(rev) * 2)]
def period(p: int, ten: str, five: str, one: str) -> str:
if p == 9:
return one + ten
elif p >= 5:
return five + one * (p - 5)
elif p == 4:
return one + five
else:
return one * p
return "".join(reversed([period(rev[i], chlist[i * 2 + 2], chlist[i * 2 + 1], chlist[i * 2])
for i in range(0, len(rev))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment