Created
January 27, 2019 21:58
-
-
Save BarthesSimpson/3338603e9a92970bc5070320e5bc718b to your computer and use it in GitHub Desktop.
Integers to Numerals
This file contains hidden or 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: | |
I = 'I' | |
V = 'V' | |
X = 'X' | |
L = 'L' | |
C = 'C' | |
D = 'D' | |
M = 'M' | |
def intToRoman(self, num): | |
""" | |
:type num: int | |
:rtype: str | |
""" | |
self.generate_int_array(num) | |
return self.thousands() + self.hundreds() + self.tens() + self.units() | |
def generate_int_array(self, num): | |
self.int_array = [] | |
while num > 0: | |
self.int_array.append(num % 10) | |
num //= 10 | |
def thousands(self): | |
if len(self.int_array) < 4: | |
return '' | |
return self.M * self.int_array[3] | |
def hundreds(self): | |
return self.convert_digit_to_symbol(2, self.C, self.M, self.D) | |
def tens(self): | |
return self.convert_digit_to_symbol(1, self.X, self.C, self.L) | |
def units(self): | |
return self.convert_digit_to_symbol(0, self.I, self.X, self.V) | |
def convert_digit_to_symbol(self, cardinality, symbol, higher, mid): | |
if len(self.int_array) < (cardinality + 1): | |
return '' | |
val = self.int_array[cardinality] | |
if val == 9: | |
return symbol + higher | |
if val >= 5: | |
return mid + (symbol * (val - 5)) | |
if val == 4: | |
return symbol + mid | |
return symbol * val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment