Int to Roman python solution

  • 0
    class Solution(object):
    	def single_digit(self, num,one,five,ten):
    		if num == 0:
    			return ''
    		elif num == 9:
    			return one+ten
    		elif num == 4:
    			return one+five
    		elif num < 5:
    			return one*num
    			return five + (num-5)*one
    	def num_apart(self, num):
    		thousand = num/1000
    		hundred = (num%1000)/100
    		ten = (num%100)/10
    		one = num%10
    		return thousand, hundred, ten, one
    	def intToRoman(self, num):
    		num_dict = {1:'I', 5:'V', 10: 'X', 50: 'L', 100:'C', 500:'D', 1000:'M'}
    		result = ''
    		thousand, hundred, ten, one = self.num_apart(num)
    		num_list = [thousand, hundred, ten, one]
    		if thousand:
    			result += self.single_digit(thousand, num_dict[1000], 'monster', 'cookie_monster')
    		if hundred:
    			result += self.single_digit(hundred, num_dict[100], num_dict[500], num_dict[1000])
    		if ten:
    			result += self.single_digit(ten, num_dict[10], num_dict[50], num_dict[100])
    		if one:
    			result += self.single_digit(one, num_dict[1], num_dict[5], num_dict[10])
    		return result

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.