Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-281

Base Conversion

Problem

Convert a non-negative integer to its string representation in any base from 2 to 36, using digits 0 through 9 and letters A through Z.

Repeatedly divide by the base and collect the remainders, then reverse them. The value 0 is represented as the single digit 0.

Hint

divmod gives both the next quotient and the digit in one step.

Test cases

case 1
in: [255,16]
expected: "FF"
case 2
in: [10,2]
expected: "1010"
case 3
in: [0,2]
expected: "0"
case 4
in: [1000,8]
expected: "1750"
case 5
in: [35,36]
expected: "Z"
case 6
in: [255,10]
expected: "255"
Starter codePython
def base_conversion(n, base):
    # Your code here
    pass

6 test cases · run your code against them in the editor