Recursive Digit Sum

  • + 0 comments

    Like others said, converting str to int and doing calculations won't work for the input test cases and Python string conversion defaults.

    Doing digit by digit conversions avoids the problem.

    My solution:

    def superDigit(n, k):
        # Write your code here
        sum_of_digits = 0
        for digit in n:
            sum_of_digits += int(digit)
        sum_of_digits *= k
        if sum_of_digits < 10:
            return sum_of_digits
        else:
            return superDigit(str(sum_of_digits), 1)