Skip to content

Instantly share code, notes, and snippets.

@ludndev
Created January 27, 2025 10:07
Show Gist options
  • Save ludndev/81ef0afa7ab0970828dcb877624ab8a8 to your computer and use it in GitHub Desktop.
Save ludndev/81ef0afa7ab0970828dcb877624ab8a8 to your computer and use it in GitHub Desktop.
def int_to_roman(num: int) -> str:
ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
hrns = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
ths = ["", "M", "MM", "MMM"]
return ths[num // 1000] + hrns[(num % 1000) // 100] + tens[(num % 100) // 10] + ones[num % 10]
print(int_to_roman(3)) # Output: "III"
print(int_to_roman(58)) # Output: "LVIII"
print(int_to_roman(1994)) # Output: "MCMXCIV"
print(int_to_roman(2025)) # Output: "MMXXV"
print(int_to_roman(3999)) # Output: "MMMCMXCIX"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment