python convert number to base 1

python convert number to base

def numberToBase(n, b):
    if n == 0:
        return [0]
    digits = []
    while n:
        digits.append(int(n % b))
        n //= b
    return digits[::-1]

Here is what the above code is Doing:
1. If n is 0, return [0]
2. If n is not 0, create an empty list called digits
3. While n is not 0, append the remainder of n divided by b to the list digits
4. Divide n by b
5. Return the list digits in reverse order

Similar Posts