Problem 135929 · hard · Phase 01 Prerequisites & Setup

Tidying the Print Dialog

strings · split · parsing · lists · loops · conditionals

A print dialog lets people type which pages of a total-page document to print. Given the typed spec, return the same selection in tidy form.

The spec is a comma-separated list of tokens. Spaces anywhere are ignored, and empty tokens (for example from a trailing comma) are skipped. A token is one of:

  • N: page N;
  • A-B: every page from A to B, in either order (9-7 means pages 7, 8, 9);
  • -B: pages 1 to B; A-: pages A to total;
  • odd, even or all: those pages of the document.

Pages below 1 or above total are silently dropped. The tidy form lists the chosen pages in increasing order, each page once, as items separated by ", ". A run of three or more consecutive pages is written first-last; shorter runs are written page by page. If no page is chosen, return "none".

Examples

Input:  spec = "3-5, 1, 4, 9-7", total = 20
Output: "1, 3-5, 7-9"

Input:  spec = "even, 12-, 2", total = 13
Output: "2, 4, 6, 8, 10, 12, 13"

Input:  spec = " - 3, 30-25,0,", total = 27
Output: "1-3, 25-27"

Constraints

  • 1 <= total <= 10**4; 0 <= len(spec) <= 2000
  • Numbers are non-negative integers of at most 6 digits and may have leading zeros
  • Every non-empty token matches one of the forms above

Goals

  • Parse a comma-separated list of several kinds of token
  • Mark chosen pages in a list of flags instead of juggling ranges
  • Rebuild the shortest description by scanning for runs
Starting Python…