Master the fundamental concepts of cpython internals through this focused micro-challenge.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksPython integers are not fixed-width C `long` values. CPython stores them as `PyLongObject` with a digit array in base 2^30, defined in `longobject.c` and `longintrepr.h`.
Each limb is a `digit` (typically `uint32`) but only 30 bits are used so multiplication of two digits never overflows a 64-bit intermediate. A number like 2^60 splits into limbs `[0, 1]` because 2^60 = 1 * 2^30 + 0.
CPython also caches integers from -5 to 256:
For example, 1234567890123 requires multiple 30-bit limbs when stored in little-endian order.
```c typedef struct { PyObject_HEAD ssize_t ob_size; digit ob_digit[1]; } PyLongObject; ```
Arbitrary precision is not free: every large integer operation allocates and may carry across limbs. Profile hot loops that accidentally promote small ints to big ints when values exceed the machine word.
This exercise asks you to model `PyLongObject` layout in C. You will implement limb splitting for a 64-bit input and document the small-integer cache range that explains Python's identity quirks.
Model CPython's PyLongObject. A Python int is stored as a sign plus a magnitude in base 2^30 digits, least significant first. The sign lives in ob_size: its absolute value is the digit count, and it is negative for negative numbers. Parse arbitrarily long decimal integers into this layout, print the layout, and implement + and * digit by digit, the way longobject.c does.
One command per line:
cLoading…
The numbers are decimal integers with an optional sign and up to 200 digits.
ob_size = ±digit count (0 for zero, and -0 is zero). Each digit is in 0..2^30-1, with no leading zero digits.24 + 4 × digits bytes (the CPython 3.11 header plus 4 bytes per digit)., cached small int.bad integer: X.cLoading…
Numbers are printed in canonical decimal: no plus sign, no leading zeros. Use 1 carry/1 borrow/1 digit product in the singular.
Input:
cLoading…
Output:
cLoading…
long: the inputs overflow it.Hidden tests cover 30-digit operands, mixed-sign addition with borrows, results that cancel to zero, negative products, and malformed numbers.