Settings overlay
MediumOnline Assessment
Combine configuration layers into one final settings list.
An application reads its settings from several layers, listed from lowest to highest priority (for example defaults, then team settings, then user settings). Each layer is a list of entries. Entries are applied in order: all entries of layers[0] in their listed order, then all entries of layers[1], and so on, each one changing the current settings. Rules for one entry:
- If it contains no
=, it is ignored. - Split it at the first
=. The part before is the key and the part after is the value. Remove leading and trailing spaces from both. Keys are case-insensitive and are stored in lowercase; values keep their case. If the key is empty, the entry is ignored. - If the value is exactly
!, the entry is a deletion. A plain key removes that key from the current settings (nothing happens if it is not set). A key ending in.*(for exampledb.*) removes every current key that starts with the key's text before the*(for exampledb.). A deletion only affects what has been applied so far; a later entry may set the key again. - If the key ends in
.*and the value is not!, the entry is ignored. - Otherwise the entry sets the key to the value, replacing any earlier value. An empty value is a normal value (not a deletion).
Input: layers, a list of lists of strings.
Output: a list of strings "key=value" for the final settings, sorted by key in ascending lexicographic order. If no settings remain, return [].
Example 1
- Input:
- [[["Host=a","port=80"],["PORT = 8080","mode=dev"]]]
- Output:
- ["host=a","mode=dev","port=8080"]
- Explanation
- Keys are lowercased and spaces around keys and values are trimmed, so "PORT = 8080" in the upper layer overrides "port=80" from the lower one. Output is sorted by key: host, mode, port.
Example 2
- Input:
- [[["host=a","port=80","db.user=x","db.pass=y"],["port=!","db.*=!","db.user=z","bad line"]]]
- Output:
- ["db.user=z","host=a"]
- Explanation
- In the second layer, "port=!" deletes port. "db.*=!" deletes every key starting with "db." (db.user and db.pass) as of that moment, and the later entry "db.user=z" sets it again. "bad line" has no "=" and is ignored. host survives.
Example 3
- Input:
- [[["a=1"],["a=!","a="],["b=x=y"," =3","c=!"]]]
- Output:
- ["a=","b=x=y"]
- Explanation
- Layer 2 first deletes a with "a=!", then "a=" sets it to the empty string: an empty value is a value, not a deletion. In layer 3, only the first "=" splits, so b gets "x=y"; " =3" has an empty key and is ignored; deleting c does nothing because c does not exist.
Constraints
0 <= layers.length <= 10, and each layer has at most 200 entries- Entries are arbitrary strings of ASCII letters, digits, spaces and the characters
= . * ! _ - - At most 400 entries in total