Predict the output
What does the line print?
import string
ALPHABET = string.digits + string.ascii_letters # 62 symbols
def base62(n: int) -> str:
if n == 0:
return ALPHABET[0]
out = []
while n:
n, r = divmod(n, 62)
out.append(ALPHABET[r])
return "".join(reversed(out))
print(base62(125), 62**7)Hint 1 of 1
125 ÷ 62 = 2 remainder 1
Choose an answer, then open Show answer below to check it.
Text version
Boxes on the board, in order:
- Client
- API
- ID allocator
- Postgres
- Redis cache
- Click queue
Show answer
Answer: A 21 3521614606208
Correct. 125 = 2×62 + 1, and 62⁷ is about 3.5 trillion.
Base 62 repeatedly divides by 62 and keeps the remainder: 125 is 2 × 62 + 1, so the digits are '2' and '1'. Seven characters give 62⁷, about 3.5 trillion codes.
Why not the others?
- B
125 3521614606208. 125 is encoded in base 62. - C
12 3521614606208. Remainders are emitted least-significant first, then reversed. - D
21 62000000. 62⁷ is far larger.