Created
December 24, 2018 04:26
-
-
Save AlseinX/93cee396c5cebc5b2332d7dfc1bb48a4 to your computer and use it in GitHub Desktop.
Formatting any positive integer to roman numerals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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