Problem 499699 · hard · Phase 04 Non-Linear Data Structures

Loom Pattern Expansion

recursion · parsing · strings · nested structures

A weaving loom reads compact pattern strings. Plain letters are woven as-is, and k[...] means "weave the bracketed pattern k times" where k is a positive integer (possibly with several digits). Patterns nest arbitrarily. Write weave(pattern) returning the fully expanded string.

Examples

Input:  pattern = "2[ab]c"
Output: "ababc"

Input:  pattern = "2[a3[b]]"
Output: "abbbabbb"

Input:  pattern = "xyz"
Output: "xyz"

Constraints

  • 0 <= len(pattern) <= 200; the input is always well formed.
  • Expanded output has at most 5000 characters; nesting depth at most 200.

Goals

  • Write a recursive descent parser that consumes characters from a shared position
  • Return both the parsed text and the position where parsing stopped
  • Handle multi-digit counts and arbitrary nesting
Starting Python…