logistics.container-fit
How many identical cartons fit in a 20ft, 40ft or 40ft high-cube container or a 13.6 m trailer, by space and by weight.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 16 tests, run in TypeScript, Python and Rust.
What it does
How many identical cartons fit in a shipping container or a road trailer: the best of the carton's orientations, in a simple block stow (every carton the same way round, in rows, columns and layers), then capped by the payload.
## Orientations
For example
container_fit(20gp, 600, 400, 400, 12,000, false, —)→ container 20gp, cartons 225, space limit 225, weight limit 2,516, limited by space, along length 9, across width 5, layers 5, placed length mm 600, placed width mm 400, placed hei… 600 x 400 x 400 cartons in a 20ft box, space limitedcontainer_fit(40hc, 580, 380, 300, 8,000, true, —)→ container 40hc, cartons 1,116, space limit 1,116, weight limit 3,581, limited by space, along length 31, across width 4, layers 9, placed length mm 380, placed width mm 580, place… turned on the floor beats as given: 1116, not 1080container_fit(40hc, 580, 380, 300, 8,000, false, —)→ container 40hc, cartons 1,120, space limit 1,120, weight limit 3,581, limited by space, along length 40, across width 4, layers 7, placed length mm 300, placed width mm 580, place… allowed to tip, a tipped orientation gives 1120
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def container_fit(container: str, length_mm: int, width_mm: int, height_mm: int, carton_grams: int, keep_upright: bool, max_payload_kg: Optional[int]) -> ContainerFit
| container | string | 20gp, 40gp, 40hc or trailer-13.6 |
| length_mm | int | the carton's outside dimensions, 1 or more; heightMm is its "this way up" axis |
| width_mm | int | |
| height_mm | int | |
| carton_grams | int | gross weight of one packed carton, 1 or more |
| keep_upright | bool | true for "this way up" cartons: only turn them on the floor, never tip them |
| max_payload_kg | int? | a lower limit than the container's rated payload, e.g. a road weight limit; null for the rating |
| returns | ContainerFit |
The types it declares, generated into your project
FitLimit = Literal["space", "weight"]
@dataclass(frozen=True)
class ContainerFit:
"""The load, how it is stacked, and what stopped it growing."""
container: str
#: how many to load: the smaller of the two limits
cartons: int
#: how many fit by dimensions alone
space_limit: int
#: how many the payload allows
weight_limit: int
#: weight only when it is strictly the smaller
limited_by: FitLimit
#: cartons in a row along the container
along_length: int
#: rows side by side
across_width: int
#: tiers stacked
layers: int
#: the carton dimension lying along the container's length
placed_length_mm: int
#: ...across its width
placed_width_mm: int
#: ...upright
placed_height_mm: int
#: cartons x cartonGrams
cargo_grams: int
#: carton volume loaded over the space's volume, rounded down; 10000 = full
volume_used_basis_points: int
Your code names it in one line, in the file that uses it
from fune.logistics.container_fit import container_fit # logistics.container-fit@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from typing import Optional, Tuple
from .logistics_container_fit_data import CONTAINERS ← this capability’s own data, compiled from data/containers.json into the same file by fune build
from .logistics_container_fit_types import ContainerFit
def _whole(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool)
def container_fit(
container: str,
length_mm: int,
width_mm: int,
height_mm: int,
carton_grams: int,
keep_upright: bool,
max_payload_kg: Optional[int],
) -> ContainerFit:
"""How many identical cartons fit in a container or trailer: the best block
stow over the allowed orientations that pass the door, capped by payload."""
for d in (length_mm, width_mm, height_mm):
if not _whole(d) or d < 1:
raise ValueError("carton dimensions must be 1 mm or more, received %r" % (d,))
if not _whole(carton_grams) or carton_grams < 1:
raise ValueError("cartonGrams must be 1 or more, received %r" % (carton_grams,))
spec = next((c for c in CONTAINERS if c.code == container), None)
if spec is None:
raise ValueError(
'unknown container "%s": expected %s' % (container, ", ".join(c.code for c in CONTAINERS))
)
payload_kg = spec.max_payload_kg
if max_payload_kg is not None:
if not _whole(max_payload_kg) or max_payload_kg < 1:
raise ValueError("maxPayloadKg must be 1 or more, received %r" % (max_payload_kg,))
if max_payload_kg > spec.max_payload_kg:
raise ValueError(
"maxPayloadKg must not exceed %d kg for %s, received %d"
% (spec.max_payload_kg, container, max_payload_kg)
)
payload_kg = max_payload_kg
l, w, h = length_mm, width_mm, height_mm
# Upright first (as given, then turned on the floor), then the four tipped ones.
orientations = (
[(l, w, h), (w, l, h)]
if keep_upright
else [(l, w, h), (w, l, h), (l, h, w), (h, l, w), (w, h, l), (h, w, l)]
)
best: Optional[Tuple[int, int, int, Tuple[int, int, int]]] = None
best_count = 0
for o in orientations:
a, b, c = o
if a > spec.length_mm or b > spec.width_mm or c > spec.height_mm:
continue
# End-loaded containers: the carton's cross-section has to pass the door.
if spec.door_width_mm is not None and b > spec.door_width_mm:
continue
if spec.door_height_mm is not None and c > spec.door_height_mm:
continue
along = spec.length_mm // a
across = spec.width_mm // b
layers = spec.height_mm // c
count = along * across * layers
if best is None or count > best_count:
best = (along, across, layers, o)
best_count = count
if best is None:
raise ValueError("carton does not fit in %s in any allowed orientation" % container)
weight_limit = payload_kg * 1000 // carton_grams
cartons = min(best_count, weight_limit)
space = spec.length_mm * spec.width_mm * spec.height_mm
along, across, layers, placed = best
return ContainerFit(
container=container,
cartons=cartons,
space_limit=best_count,
weight_limit=weight_limit,
limited_by="weight" if weight_limit < best_count else "space",
along_length=along,
across_width=across,
layers=layers,
placed_length_mm=placed[0],
placed_width_mm=placed[1],
placed_height_mm=placed[2],
cargo_grams=cartons * carton_grams,
volume_used_basis_points=cartons * l * w * h * 10000 // space,
)Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add logistics.container-fit
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./logistics.container-fit-1.0.0-python.fune, or fetch it from a terminal with fune pull logistics.container-fit@1.0.0:python.
The whole function, every language, is one file too: logistics.container-fit-1.0.0.fune, 27,549 bytes, sha256 0724243703a57d2a7546dcf573145a83d6aab928f399362008abab1ef93294b6. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before logistics.container-fit
after — your function gets the result and the arguments, and returns the final result.
# fune: after logistics.container-fit
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show logistics.container-fit --steps.
# fune: step logistics.container-fit after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| 600 x 400 x 400 cartons in a 20ft box, space limited | 20gp, 600, 400, 400, 12,000, false, — | → | container 20gp, cartons 225, space limit 225, weight limit 2,516, limited by space, along length 9, across width 5, layers 5, placed length mm 600, placed width mm 400, placed hei… |
| turned on the floor beats as given: 1116, not 1080 | 40hc, 580, 380, 300, 8,000, true, — | → | container 40hc, cartons 1,116, space limit 1,116, weight limit 3,581, limited by space, along length 31, across width 4, layers 9, placed length mm 380, placed width mm 580, place… |
| allowed to tip, a tipped orientation gives 1120 | 40hc, 580, 380, 300, 8,000, false, — | → | container 40hc, cartons 1,120, space limit 1,120, weight limit 3,581, limited by space, along length 40, across width 4, layers 7, placed length mm 300, placed width mm 580, place… |
| heavy cartons: the payload runs out first | 20gp, 600, 400, 400, 150,000, false, — | → | container 20gp, cartons 201, space limit 225, weight limit 201, limited by weight, along length 9, across width 5, layers 5, placed length mm 600, placed width mm 400, placed heig… |
| a lower road limit caps it further | 20gp, 600, 400, 400, 150,000, false, 20,000 | → | container 20gp, cartons 133, space limit 225, weight limit 133, limited by weight, along length 9, across width 5, layers 5, placed length mm 600, placed width mm 400, placed heig… |
| taller than the door: only a tipped orientation gets in | 20gp, 1,000, 1,000, 2,300, 50,000, false, — | → | container 20gp, cartons 10, space limit 10, weight limit 604, limited by space, along length 5, across width 1, layers 2, placed length mm 1,000, placed width mm 2,300, placed hei… |
| pallet-sized loads in a trailer: 34 fit, the payload allows 24 | trailer-13.6, 1,200, 800, 1,500, 1,000,000, true, — | → | container trailer-13.6, cartons 24, space limit 34, weight limit 24, limited by weight, along length 17, across width 2, layers 1, placed length mm 800, placed width mm 1,200, pla… |
| lighter pallets: space limited at 34 (800 mm along the length, 17 x 800 = 13600) | trailer-13.6, 1,200, 800, 1,500, 500,000, true, — | → | container trailer-13.6, cartons 34, space limit 34, weight limit 48, limited by space, along length 17, across width 2, layers 1, placed length mm 800, placed width mm 1,200, plac… |
| equal limits count as space | 20gp, 600, 400, 400, 134,222, false, — | → | container 20gp, cartons 225, space limit 225, weight limit 225, limited by space, along length 9, across width 5, layers 5, placed length mm 600, placed width mm 400, placed heigh… |
| taller than the door and kept upright | 20gp, 1,000, 1,000, 2,300, 50,000, true, — | → | error: carton does not fit in 20gp in any allowed orientation |
Show the other 6 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a carton the size of the inside cannot pass the door | 20gp, 5,900, 2,352, 2,393, 1,000, false, — | → | error: carton does not fit in 20gp in any allowed orientation |
| an unknown container | 45hc, 600, 400, 400, 12,000, false, — | → | error: unknown container "45hc" |
| a road limit above the container's rating | 20gp, 600, 400, 400, 12,000, false, 40,000 | → | error: maxPayloadKg must not exceed 30200 kg for 20gp |
| a zero payload limit | 20gp, 600, 400, 400, 12,000, false, 0 | → | error: maxPayloadKg must be 1 or more |
| a zero dimension | 20gp, 600, 0, 400, 12,000, false, — | → | error: carton dimensions must be 1 mm or more |
| a weightless carton | 20gp, 600, 400, 400, 0, false, — | → | error: cartonGrams must be 1 or more |
More from the author
A carton can lie six ways. With `keepUpright` ("this way up" cartons) only the two that keep `heightMm` vertical are tried; otherwise all six. Each orientation's count is `floor(L / a) x floor(W / b) x floor(H / c)`, and the largest wins, ties going to the first in this order: as given, turned on the floor, then the four tipped ones (length-height-width, height-length-width, width-height-length, height-width-length).
Turning a carton on the floor often matters more than people expect: 580 x 380 x 300 mm cartons in a 40ft high cube give 1,080 as given and 1,116 turned (31 rows of 380 mm beat 20 rows of 580 mm). Dividing the volumes instead (`container volume / carton volume`) overstates the answer every time, since the gaps at the walls and roof are real.
It is a block stow, not an optimiser: filling the leftover strip along a wall with cartons turned the other way can add a few more, and load-planning software does that. The answer here is a safe, reproducible lower bound.
## Doors
A carton has to get in. Containers are loaded through the end doors, which are smaller than the inside (a 20ft box is 2,393 mm high inside but 2,292 mm at the door), so an orientation whose width or height does not pass the door is not used. The trailer is a curtainsider, loaded from the side, so it has no door limit. No usable orientation at all is an error.
## Weight
`weightLimit = floor(maxPayloadKg x 1000 / cartonGrams)`, using the container's rated payload or the caller's lower `maxPayloadKg` (road weight limits on the vehicle carrying the box are often the real constraint: ask the haulier). A figure above the container's rating is an error. `cartons` is the smaller limit, and `limitedBy` says which one it was. The payload is assumed spread evenly; concentrated loads have their own limits.
## Container data
Inside and door dimensions are the carrier's nominal figures, not the ISO minimums: an ISO 668 / ISO 1496-1 container is only guaranteed 5,898 x 2,350 x 2,350 mm inside (20ft), so a plan that needs the last few millimetres may not fit every box. Payloads vary with the tare of the individual box; the figures are one carrier's.
| code | inside (mm) | door (mm) | payload | source | |---|---|---|---|---| | 20gp | 5,900 x 2,352 x 2,393 | 2,340 x 2,292 | 30,200 kg | Hapag-Lloyd | | 40gp | 12,032 x 2,352 x 2,395 | 2,340 x 2,292 | 28,800 kg | Hapag-Lloyd | | 40hc | 12,032 x 2,352 x 2,700 | 2,340 x 2,597 | 28,650 kg | Hapag-Lloyd | | trailer-13.6 | 13,600 x 2,440 x 2,650 | side loaded | 24,000 kg | haulier reference values |
Trailers are not standardised the way containers are; the figures are the cautious end of the published range, and your haulier's own figures should be preferred.
## Sources
- Hapag-Lloyd, Container Specification (general purpose and high cube tables; it also reproduces the ISO 668 external and minimum internal dimensions), https://www.hapag-lloyd.com/content/dam/website/downloads/press_and_media/publications/15211_Container_Specification_engl_Gesamt_web.pdf - ISO 668:2020, Series 1 freight containers: classification, dimensions and ratings; ISO 1496-1, general cargo containers. - PLS Transport, "Trailer dimensions: technical guide", https://pls-transport.be/en/articles/trailer-dimensions ; Calcargo, "Trailer dimensions: what fits inside", https://calcargo.eu/en/blog/trailer-dimensions-what-fits-inside
Files
| Path | Bytes |
|---|---|
| README.md | 3,660 |
| data/containers.json | 1,623 |
| impl/python.py | 3,555 |
| impl/rust.rs | 4,879 |
| impl/typescript.ts | 3,379 |
| vectors.json | 4,753 |