Created
June 9, 2022 04:17
-
-
Save mike-pete/a8c9a07d525f9b6186df383de891b66d to your computer and use it in GitHub Desktop.
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
class Solution: | |
def intToRoman(self, num: int) -> str: | |
numerals = { | |
1: 'I', | |
4: 'IV', | |
5: 'V', | |
9: 'IX', | |
10: 'X', | |
40: 'XL', | |
50: 'L', | |
90: 'XC', | |
100: 'C', | |
400: 'CD', | |
500: 'D', | |
900: 'CM', | |
1000: 'M' | |
} | |
numeralKeys = list(numerals.keys()) | |
numeralsToReturn = '' | |
while num > 0: | |
if (numeralKeys[-1] > num): | |
numeralKeys.pop() | |
else: | |
num -= numeralKeys[-1] | |
numeralsToReturn += numerals[numeralKeys[-1]] | |
return numeralsToReturn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment