Recursive Digit Sum

  • + 4 comments

    Totally agree with you. Here's what I did in Python:

    def superDigit(n, k):
        if len(n)==1:
            return n
        return superDigit(str(sum(map(int, n))*k),1)
    
    • + 1 comment

      This does not pass all performance tests.

      • + 1 comment

        It does, though :)

    • + 0 comments

      Hey 7ovana,

      I am try to do the code as below. But not pass all the test and shows that time exceed. Any idea why this approach is slower than yours, please?

      def superDigit(n, k):
          if len(n) == 1:
              return n
          
          n = n * k 
          
          while len(n) > 1:
              n = superDigit_helper(n)
          return n
      
      def superDigit_helper(n):
          return  str(sum(map(int, n)))
      
    • + 0 comments

      I hate recursion, forgot return keyword return after if clause and was wondering why my code wasn't working from the last 3 hours :'(

    • + 0 comments

      Instead of

      if len(n)==1:

      ; it should be

      if len(n)==1 and k==1: