Last active
March 19, 2022 13:49
-
-
Save GhostOps77/b57f677df5f5d0cc0580c2889f9f1050 to your computer and use it in GitHub Desktop.
A sample code that can convert Integer to Roman numeral in Python (not the fastest, but still it works; kind of)
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 int_to_roman_num(number:int) -> str: | |
num, string, lst = str(number)[::-1], 'IVXLCDM', [] | |
for index, val in enumerate(map(int, num)): | |
if val == 0: continue | |
(q, r), index2 = divmod(val, 5), index*2 | |
if val in (4, 9): | |
temp = f'{string[index2]}{string[index2+q+1]}' | |
else: | |
temp = f'{string[index2+q]*q}{string[index2]*r}' | |
lst.insert(0, temp) | |
return ''.join(lst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know its not the best, but I just did it to test myself