The Thatcher's Reed Bundles
# The Thatcher's Reed Bundles
The thatcher of bitwick ties the morning's cut reeds into bundles before they go up on the roof. The reeds come off the cart in a fixed order, and the thatcher never reorders them: each bundle is a contiguous run taken from the front of what remains.
You are given three arguments:
reeds— a list of non-negative integers, the reed lengths in cart order.spread— a non-negative integer, the largest allowed difference between the longest and the shortest reed inside one bundle.cap— a positive integer, the largest number of reeds one bundle may hold.
The bundling rule, applied to the reeds strictly left to right:
- The first reed starts the first bundle.
- For each later reed, consider adding it to the bundle currently being tied. It may be added only if, after adding it, that bundle holds at most
capreeds and its longest reed minus its shortest reed is at mostspread. - If it may be added, add it. Otherwise the current bundle is tied off as it stands and the reed starts a new bundle.
Write bundleReeds(reeds, spread, cap), which returns a list of bundles. Each bundle is a list of integers — the reed lengths it holds, in cart order. Concatenating the bundles in order must give back reeds exactly. If reeds is empty, return an empty list (never a bundle holding nothing).
examples
- case 1
reeds of [4, 5, 9, 10, 10, 3], spread of 2, cap of 3[[4, 5], [9, 10, 10], [3]] — 9 is too far from 4, and the fourth reed 3 arrives at a bundle already holding 3 reeds - case 2
reeds of [1, 3, 2, 4], spread of 2, cap of 10[[1, 3, 2], [4]] — 4 sits close to 2, but the bundle already holds a 1, and 4 minus 1 is more than 2 - case 3
reeds of [], spread of 3, cap of 4[] — no reeds, no bundles
- reward
- 210 XP
- signature
bundleReeds
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- both rules bite
- span measured across the whole bundle
- no reeds at all
- cap of one
- spread of zero groups equals
- a single reed
- one long bundle
- descending cart