‹ back to the board

Monday, September 21

Apprenticelogic & control flow

The Tollhouse Permit Desk

The tollhouse at the east road keeps a desk where carters hand over their permits, and the clerk has gone to market. The rules carved into the desk are these, and they are checked strictly in this order — a permit is judged by the first rule it breaks, and no further.

A permit is a string. Using zero-based positions:

1. It must be exactly 9 characters long. Otherwise the verdict is "length". 2. Positions 0, 1 and 2 must each be an uppercase letter A–Z. Otherwise the verdict is "prefix". 3. Position 3 must be the hyphen character -. Otherwise the verdict is "dash". 4. Positions 4, 5, 6, 7 and 8 must each be a digit 0–9. Otherwise the verdict is "digits". 5. The sum of those five digits must be even. Otherwise the verdict is "checksum".

A permit that breaks no rule has the verdict "valid".

Write inspectPermits(permits), which takes a list of strings and returns a list of strings: the verdict for each permit, in the same order as the input. An empty input list returns an empty list.

  • input validation branches
  • Apprentice

examples

  1. case 1permits of ["ABC-12340", "abc-12345", "ABC-1234"]["valid", "prefix", "length"] (the digits of the first sum to 10, which is even; the third is only 8 characters long)
  2. case 2permits of ["ab-123"]["length"] (the length rule is checked first, so the bad prefix is never reported)
  3. case 3permits of ["ABC-00000", "XYZ-11111"]["valid", "checksum"] (a digit sum of 0 is even; a digit sum of 5 is not)
reward
190 XP
signature
inspectPermits
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…
  • empty batch at the desk
  • length is judged before anything else
  • each rule in its turn
  • all zeros pass the checksum
  • clean permits let the carts through
  • a lowercase digit-lookalike prefix
The Tollhouse Permit Desk · bitwick