title: Algorithm not passing tests even when I get correct results on my end
body: <p>The question is mostly about base conversion. Here's the question.</p>
  <ol>
  <li>Start with a random minion ID n, which is a nonnegative integer of length k in base b</li>
  <li>Define x and y as integers of length k.  x has the digits of n in descending order, and y has the digits of n in ascending order</li>
  <li>Define z = x - y.  Add leading zeros to z to maintain length k if necessary</li>
  <li>Assign n = z to get the next minion ID, and go back to step 2</li>
  </ol>
  <p>For example, given minion ID n = 1211, k = 4, b = 10, then x = 2111, y = 1112 and z = 2111 - 1112 = 0999. Then the next minion ID will be n = 0999 and the algorithm iterates again: x = 9990, y = 0999 and z = 9990 - 0999 = 8991, and so on.</p>
  <p>Depending on the values of n, k (derived from n), and b, at some point the algorithm reaches a cycle, such as by reaching a constant value. For example, starting with n = 210022, k = 6, b = 3, the algorithm will reach the cycle of values [210111, 122221, 102212] and it will stay in this cycle no matter how many times it continues iterating. Starting with n = 1211, the routine will reach the integer 6174, and since 7641 - 1467 is 6174, it will stay as that value no matter how many times it iterates.</p>
  <p>Given a minion ID as a string n representing a nonnegative integer of length k in base b, where 2 &lt;= k &lt;= 9 and 2 &lt;= b &lt;= 10, write a function solution(n, b) which returns the length of the ending cycle of the algorithm above starting with n. For instance, in the example above, solution(210022, 3) would return 3, since iterating on 102212 would return to 210111 when done in base 3. If the algorithm reaches a constant, such as 0, then the length is 1.</p>
  <p>Here's my code</p>
  <pre><code>def solution(n, b): #n(num): str, b(base): int
      #Your code here
      num = n
      k = len(n)
      
      resList = []
      resIdx = 0
      
      loopFlag = True
      
      while loopFlag:
          numX = &quot;&quot;.join(x for x in sorted(num, reverse=True))
          numY = &quot;&quot;.join(y for y in sorted(num))
          
          xBaseTen, yBaseTen = getBaseTen(numX, b), getBaseTen(numY, b)
          xMinusY = xBaseTen - yBaseTen
          num = getBaseB(xMinusY, b, k)
          
          resListLen = len(resList)
          for i in range(resListLen - 1, -1, -1):
              if resList[i] == num:
                  loopFlag = False
                  resIdx = resListLen - i
                  break
  
          if loopFlag:
              resList.append(num)
              
          if num == 0:
              resIdx = 1
              break
      return resIdx
  
  def getBaseTen(n, b): #n(number): str, b(base): int -&gt; int
      nBaseTenRes = 0
      
      n = str(int(n)) # Shave prepending zeroes
      length = len(n) - 1
      
      for i in range(length + 1):
          nBaseTenRes += int(n[i]) * pow(b, length - i)
      
      return nBaseTenRes
  
  def getBaseB(n, b, k): #(number): int, b(base): int, k:(len): int -&gt; str
      res = &quot;&quot;
      r = 0 # Remainder
      
      nCopy = n
      while nCopy &gt; 0:
          r = nCopy % b
          nCopy = floor(nCopy / b)
          
          res += str(r)
          
      res = res[::-1]
      
      resPrependZeroesLen = k - len(res)
      if resPrependZeroesLen &gt; 0:
          for i in range(resPrependZeroesLen):
              res = &quot;0&quot; + res
      
      return res
  </code></pre>
  <p>The two test that are available to me and are not passing, are ('1211', 10) and ('210022', 3). But I get the right answers for them (1, 3).</p>
  <p>Why am I failing? Is the algo wrong? Hitting the time limit?</p>