problems / 201

Config Parser and Builder

easy parsing constructive strings 1000 ms · 256 MB

Config Parser and Builder

Parse a simple configuration file format and output all key-value pairs in the order they were defined.

The configuration file consists of:

  • Comment lines: lines beginning with # (possibly after leading whitespace)
  • Blank lines: lines with only whitespace
  • Section headers: lines matching [section_name] where section_name is a sequence of letters, digits, underscores, and dots
  • Key-value pairs: lines matching key = value where key is alphanumeric/underscore and value is the rest of the line after =, with leading and trailing whitespace trimmed

For each key-value pair, output section_path.key = value where section_path is the dot-separated path of the current section (empty if not in any section), and both the key and value have whitespace preserved as-is after initial trimming.

Input

The first line contains n (1 ≤ n ≤ 1000), the number of lines in the configuration file. The next n lines form the configuration file.

Each line is at most 200 characters and contains only printable ASCII characters (space through tilde).

Output

For each key-value pair encountered in order, print section_path.key = value on its own line. If not currently in a section, omit the leading dot (i.e., just key = value). The section path should reflect the most recent [section_header] line encountered before the key-value pair.

Notes

  • Section names can contain dots to represent nested sections (e.g., [db.primary]).
  • Whitespace around the = in a key-value pair is stripped from both the key and value.
  • Values retain any internal spaces or trailing spaces after the initial trim.
  • Comment lines and blank lines do not produce output.
  • If a key appears multiple times in the same section, each occurrence is output separately.
  • Section headers and keys are case-sensitive.

Example 1

Input

5
# This is a comment
name = Alice
age = 30
email = [email protected]
port = 8080

Output

name = Alice
age = 30
email = [email protected]
port = 8080

Example 2

Input

12
# Database configuration
[database]
host = localhost
port = 5432

[database.primary]
# Primary instance
name = main_db
user = admin
[cache]
enabled = true
ttl = 3600

Output

database.host = localhost
database.port = 5432
database.primary.name = main_db
database.primary.user = admin
cache.enabled = true
cache.ttl = 3600

Example 3

Input

3
  key1  =  value1
  key2=value2
key3   =   value3

Output

key1 = value1
key2 = value2
key3 = value3

Hints · reveal one at a time if you're stuck

Hint 1

Read the file top to bottom. Which lines only change some state you carry along, and which lines produce output?

Hint 2

Keep the name of the current section as you go. For a key-value line, split on the first = and strip both sides, then prefix the key with the section path when there is one. Check the comment and blank-line rules before treating a line as a key-value pair.

report an issue Prefer your own editor? sooks prep 201 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.