Created
May 26, 2023 12:49
-
-
Save 607011/9382619bf4e05002279ce36026d384f9 to your computer and use it in GitHub Desktop.
Integer to Roman number literals; O(1) solution for constraint 0 < num < 4000
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 int2roman(num: int) -> str: | |
ONES = ["","I","II","III","IV","V","VI","VII","VIII","IX"] | |
TENS = ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"] | |
HUNDREDS = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"] | |
THOUSANDS = ["","M","MM","MMM"] | |
return THOUSANDS[num//1000] + HUNDREDS[(num%1000)//100] + TENS[(num%100)//10] + ONES[num%10]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment