Problem 241130 · easy · Phase 02 Linear Data Structures

Parse Config Pairs

strings · parsing · dicts

A configuration string holds key=value entries separated by semicolons. Write parse_config(s) that returns a dictionary of the entries.

Rules:

  • Entries are separated by ;. Empty entries (for example from ;; or a trailing ;) are ignored.
  • Each entry is split at its first =; the key is everything before it and the value everything after it. Entries without an = are ignored.
  • Keys and values are stripped of leading and trailing spaces.
  • If a key appears more than once, the last value wins.

Examples

Input:  s = "host = localhost; port=8080;;debug=true"
Output: {"host": "localhost", "port": "8080", "debug": "true"}

Input:  s = "a=1;a=2; b = x=y "
Output: {"a": "2", "b": "x=y"}
Explanation: the second a overrides the first; the value of b keeps its inner "=".

Constraints

  • 0 <= len(s) <= 10**5, printable ASCII only
  • Target: O(n) time in the length of s

Goals

  • Split text on two different separators
  • Strip surrounding whitespace from keys and values
  • Let later entries override earlier ones
Starting Python…