‹ back to the board

Tuesday, October 27

Mastermulti-constraint & composite logic

The Moth-Post of Yselwyn Heights

Commission of the Moth-Post Courier-Wardens, Yselwyn Heights

The great moth-riders of Yselwyn Heights carry dispatch slips across the high passes to camps no road reaches. Each slip is a single line of text, divided into fields by the Post's scribes. A misread slip has sent supplies to the wrong ridge before, so the Courier-Wardens pay a standing bounty to anyone who can read a slip's fields exactly as the scribes wrote them, and who can refuse any slip that was botched.

Write readSlip(line). line is a string containing no newline characters. Return a list of strings, which are the slip's fields in order, or null (None in Python) if the slip is malformed.

The scribes' rules

1. Margin notes. A # that is not inside the contents of a quoted field begins a margin note. That # and everything after it are ignored. A # inside a quoted field's contents is an ordinary character. 2. Separators. The rest of the slip is divided into fields at every | that is not inside a quoted field's contents. A slip with *k* such separators has exactly *k* + 1 fields. An empty slip has one field, the empty string. 3. Quoted fields. A field is *quoted* if its first character other than spaces is ". The contents begin after that opening ". Read the contents from left to right: - Two consecutive quote marks "" stand for one " in the value. - A lone ", meaning one not immediately followed by another ", is the closing quote. - Every other character is kept exactly, including spaces, | and #. After the closing quote, only spaces may appear before the next |, the end of the slip, or a margin note. Any other character makes the slip malformed. A quoted field with no closing quote also makes the slip malformed. The field's value is its decoded contents. 4. Bare fields. Any field that is not quoted is *bare*. Its value is its text with leading and trailing spaces removed. A bare field containing a " anywhere makes the slip malformed. A field that is empty or contains only spaces is a bare field, and its value is the empty string.

Here "space" means the space character only. Tabs and all other characters are ordinary and are never removed.

If any field is malformed, the whole slip is malformed, and you must return null / None instead of a list.

  • state machines
  • Master

examples

  1. case 1line of `"Bell|Hook #2" | north # check`["Bell|Hook #2", "north"]. The `|` and `#` inside the quotes are kept, and the note after the second `#` is ignored.
  2. case 2line of `"the ""Grey"" Moth"|`["the \"Grey\" Moth", ""]. Each doubled quote becomes one quote, and the trailing separator leaves a final empty field.
  3. case 3line of `"amber"x|jet`null / None, because a character other than a space follows the closing quote.
  4. case 4line of `# nothing to carry`[""]. The whole line is a margin note, which leaves one empty field.
reward
540 XP
signature
readSlip
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…
  • plain fields
  • bare fields are trimmed
  • quoted pipe and hash kept, note dropped
  • doubled quotes and trailing separator
  • margin note after bare field
  • empty slip
  • slip that is only a note
  • separators only
  • quoted contents keep their spaces
  • junk after closing quote
  • quote inside bare field
  • unclosed quote swallows pipe and hash
  • doubled quote at end leaves field unclosed
  • escaped quote then closing quote
  • note begins before stray quote
  • tabs are not trimmed
  • empty quoted fields
  • two quoted pieces in one field
  • note right after a separator
  • hash in quotes then note
The Moth-Post of Yselwyn Heights · bitwick