Recursive Digit Sum

  • + 0 comments

    you need to sum first before combine the string

    very manual way in python

    def combineMethod(n,k):
        return str(n)*k
    
    def sumCurrentDigit(current):
        sum = 0
        for record in current:
            sum += int(record)
        return sum
    
    def getSuperDigit(combineDigit):
        super = None
        current = list(str(combineDigit))
        while(True):
            if len(current) ==1:
                super = current
                break
            sumDg = sumCurrentDigit(current)
            current = list(str(sumDg))
        return super
    def superDigit(n, k):
        # Write your code here
        sumCurrent = sumCurrentDigit(n)
        combineDigit = combineMethod(sumCurrent,k)
        super = getSuperDigit(combineDigit)
        super = super[0]
        super = int(super)
        return super