Skip to content

Instantly share code, notes, and snippets.

@GhostOps77
Last active March 19, 2022 13:49
Show Gist options
  • Save GhostOps77/b57f677df5f5d0cc0580c2889f9f1050 to your computer and use it in GitHub Desktop.
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)
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)
@GhostOps77
Copy link
Author

I know its not the best, but I just did it to test myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment