Search code examples
pythonradix

Python: breaking down integers into exponents?


I would like to make a function which convert an integer into exponents of base 27. The function should return a list with the multiple of the largest exponent in the first position and the final exponent in the last. So for example, 65 would return [2,11]. I've tried using int % b, where b is an increasing power of 27 within a loop. However it's turning out very complicated. Any help?


Solution

  • def convert(x, base):
      res = []
      while x:
        res.append(x%base)
        x //= base # this line depends on Python's version!
      res.reverse()
      return res