Git: Read a configuration file

Read the kind of settings file Git uses for its configuration.

What to build

A program that reads a Git-style configuration file and answers lookups.

Requirements

  • Sections are written [name]; settings key = value (spaces around = are optional). The full name of a setting is section.key, in lower case. Comments start with # or ;.
  • For each lookup print name = value, or name is not set.

Input

Line 1: n. Then n lines of the file. Then q and q names to look up.

Output

q lines.

Constraints

n, q ≤ 100.

Example

Input:

4
[user]
    name = Ada Lovelace
    email = ada@example.com
[core]
2
user.name
core.editor

Output:

user.name = Ada Lovelace
core.editor is not set

Why it works

Every setting belongs to the most recent section header, so tracking the current section turns each line into a full name.

Examples

Example 1 · Input
4
[user]
    name = Ada Lovelace
    email = ada@example.com
[core]
2
user.name
core.editor
Expected output
user.name = Ada Lovelace
core.editor is not set
Example 2 · Input
5
# my settings
[init]
defaultBranch = main
[pull]
rebase=true
2
init.defaultbranch
pull.rebase
Expected output
init.defaultbranch = main
pull.rebase = true

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

A line `[section]` starts a section; a line `key = value` sets section.key.

Hint 2

Ignore blank lines and lines starting with # or ;. A later setting replaces an earlier one.

Solution

Show one correct solution (try it yourself first)
settings = {}
section = ""
for _ in range(int(input())):
    line = input().strip()
    if not line or line[0] in "#;":
        continue
    if line.startswith("["):
        section = line.strip("[]").strip().lower()
    else:
        key, _, value = line.partition("=")
        settings[f"{section}.{key.strip().lower()}"] = value.strip()
for _ in range(int(input())):
    name = input().strip().lower()
    print(f"{name} = {settings[name]}" if name in settings else f"{name} is not set")

Lesson: Staging and committing →