Code for Roman in python


  • 0
    R

    ''''
    class Solution(object):
    def intToRoman(self, num):
    """
    :type num: int
    :rtype: str
    """

        romanten={1:"I",2:"II",3:"III",4:"IV",5:"V",6:"VI",7:"VII",8:"VIII",9:"IX",10:"X"}
               
        numarr=str(num)
        
        lenNum=len(numarr)
        finalRoman=""
        for i in range(lenNum):
            number=int(numarr[i])
            if(number==0):
                finalRoman+=""
            else:
                string=romanten[number]
                power=pow(10,lenNum-i-1)
                x=self.shiftChar(string,power)
                finalRoman+=x
        return finalRoman
            
    def shiftChar(self,string,power):
        revromanliterals={"I":1,"V":5,"X":10}
        literals={1:"I",5:"V",10:"X",50:"L",100:"C",500:"D",1000:"M"}
        if(power==0):
            return string
        else:
            finstr=""
            for c in string:
                tempval=revromanliterals[c]
                tempval*=power
                finstr+=literals[tempval]
            print finstr
            return finstr
                '''

Log in to reply
 

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