Recursive Digit Sum

Sort by

recency

|

584 Discussions

|

  • + 0 comments

    I really wanted this to work with recursion so I did. After looking here at the comments it seems that multiplying by 'k' after doing the first sum is the best way to optimize away the memory issues. Did this in C++, could be improved upon but i'll settle with this. Also had issues with the way the question was worded, guess I either need to improve my reading comprehension or this needs to be better.

    int superDigit(string n, int k) {
        
        // Base case
        if (n.length() == 1 && k == 0) {
            return std::stoi(n);
        }
        
        // First run
        if (k != 0) {
            size_t sum = 0;        
            for (size_t i = 0; i < n.length(); ++i) {
                sum += n.at(i) - '0';
            }
            sum = sum * k;
            return superDigit(to_string(sum), 0);
            
        } else { // Recursion body
            size_t sum = 0;        
            for (size_t i = 0; i < n.length(); ++i) {
                sum += n.at(i) - '0';
            }
    
            return superDigit(std::to_string(sum), 0);   
        }
    }
    
  • + 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)
    
  • + 0 comments

    I first did this by calculating n as an int and then iteratively dividing n by 10 to get the least significant digit and add it to a total, multiplying by k before a recursive call to calculate the result's super digit. But that required setting Python's max string conversion much higher than the default. Leaving the digits as strings until the calculation is made worked out nicer. Here is my finished code with a few notes to myself:

    # Notes:
    # 1. Don't calc p ahead of time both b/c Python's conversion of string to int
    #    has a cap (adjust w/ sys.set_int_max_str_digits()) and b/c it's easier to
    #    manage the expense of the operation during iteration
    # 2. It's a little silly to pass k in the recursive call b/c it's only used
    #    on the first iteration, but it seems even sillier to write a superDigit0
    #    that only takes an n and is used for recursion.
    # 3. It may be possible to determine that n < 1e100000 and k <= 1e5 will give
    #    you a p that won't blow the stack by recurring too many times but I didn't
    #    do that. To be safe, we could unroll the recursion into a loop.
    def superDigit(n: str, k: int) -> int:
            
        def sdc(ns: str) -> int:
            acc = 0
            for ch in ns:
                acc += int(ch)
            return acc * k
            
        total = sdc(n)    
        if total < 10:
            return total
        return superDigit(str(total), 1)
    
  • + 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
    
  • + 1 comment

    If someone out there is having any issues with some of the test cases even though their solution is correct you might want to look at the constraints, using a integer then converting that to string to store your sum is not the move, it will work but not for some of the test cases, it might be worth making your sum append to a string or list and see it that pass some of the test cases.