Skip to content

Instantly share code, notes, and snippets.

@607011
Created May 26, 2023 12:49
Show Gist options
  • Save 607011/9382619bf4e05002279ce36026d384f9 to your computer and use it in GitHub Desktop.
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
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