The Miller's Tally Stick
Pinned by the miller of bitwick, beside the flour scales.
Every sack of grain that goes through the mill is recorded as a character notched onto a tally stick. Before a stick is filed away, the miller squares off both ends, shaving away the same number of notches from the front and from the back.
You are given stick, a string, and k, a non-negative integer.
Write trimStick(stick, k), which returns a string: the characters of stick that remain after removing the first k characters and the last k characters. If the length of stick is less than or equal to 2 * k, return the empty string.
stick is never modified; a new string is returned.
examples
- case 1
stick of "|||-|||", k of 2"|-|" — two notches come off each end of the seven - case 2
stick of "~~grain~~", k of 2"grain" - case 3
stick of "|||", k of 0"|||" — nothing is shaved off at all - case 4
stick of "||||", k of 2"" — the four notches are exactly consumed
- reward
- 140 XP
- signature
trimStick
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- trims two from each end
- trims a worded stick
- k of zero shaves nothing
- stick exactly consumed
- k larger than the stick
- single notch untouched
- empty stick
- one off each end of a long stick