‹ back to the board

Monday, September 21

Novicesyntax & basics

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.

  • slicing and indexing
  • Novice

examples

  1. case 1stick of "|||-|||", k of 2"|-|" — two notches come off each end of the seven
  2. case 2stick of "~~grain~~", k of 2"grain"
  3. case 3stick of "|||", k of 0"|||" — nothing is shaved off at all
  4. case 4stick of "||||", k of 2"" — the four notches are exactly consumed
reward
140 XP
signature
trimStick
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • 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
The Miller's Tally Stick · bitwick