The Peal-Script of Saint Wickham's
The bell-tower at Saint Wickham's is rung from a written peal-script pinned inside the ringing chamber. The sexton has lost the knack of reading the longer ones and wants them written out bell by bell.
You are given expr, a string holding one peal-script. Return the sequence of bell numbers it denotes, in ringing order, as a list of integers (an array of numbers in TypeScript).
What the marks mean
- A run of one or more digits is a bell number (
7,12). On its own it denotes a sequence of exactly that one bell. ( ... )groups a script. Groups are never empty.- The operators, listed from tightest-binding to loosest:
| form | name | meaning | | --- | --- | --- | | X*n | repeat (postfix) | X written out n times end to end. n is a literal run of digits, never an expression, and may be 0. | | ~X | reverse (prefix) | the bells of X in reverse order | | X^Y | weave (infix, right-associative) | first bell of X, first of Y, second of X, second of Y, and so on; when one side runs out, the remaining bells of the other follow in their own order | | X-Y | silence (infix, left-associative) | the bells of X, dropping every bell whose number appears anywhere in Y | | X,Y | then (infix, left-associative) | the bells of X followed by the bells of Y |
- Because repeat is tighter than reverse, a
~applies to its operand together with any repeats written on that operand. Several repeats in a row apply left to right, and~may be written twice in a row. - Spaces may appear anywhere between marks and are ignored; the digits of one number are always adjacent.
Every input is well-formed: brackets balance, every operator has the operands it needs, and all numbers are non-negative. The result may be empty, in which case return an empty list.
Write pealNotes(expr), which returns that list of bell numbers.
examples
- case 1
expr of "(1,2,3)^4^5"[1, 4, 2, 5, 3] — weave leans right, so 4^5 is woven first into [4, 5], and [1, 2, 3] is woven against that - case 2
expr of "~(1,2)*2"[2, 1, 2, 1] — repeat is tighter than reverse, so (1,2) becomes [1, 2, 1, 2] and only then is reversed - case 3
expr of "1,2,3-2,4*0"[1, 2, 3] — silence binds tighter than then, so only the third piece loses its bell 2; the last piece repeats zero times and contributes nothing
- reward
- 540 XP
- signature
pealNotes
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- a lone bell
- weave leans right
- repeat outranks reverse
- silence outranks then, and a zero repeat
- silence leans left with a woven right side
- the tower falls silent
- uneven weave with a wide bell and loose spacing
- the sexton's long script
- stacked repeats under a reverse