9 Commits

Author SHA1 Message Date
591bd86235 document performance characteristics 2026-08-27 10:31:29 +02:00
62a4fce866 docs: document performance characteristics 2026-08-26 21:03:34 +02:00
8be290a859 chore: remove the empty conftest.py 2026-08-26 20:58:51 +02:00
ef06c9c166 docs: document performance characteristics 2026-08-26 14:53:39 +02:00
688b3ca31b Merge pull request 'bugfix/BUG-1_empty_key_in_config_hangs_the_program' (#1) from bugfix/BUG-1_empty_key_in_config_hangs_the_program into main
Reviewed-on: #1
2026-08-26 12:46:01 +00:00
800ded7819 update 2026-08-26 14:45:11 +02:00
dc401be837 fix: reject a configuration entry with an empty value1
A line such as "=z" parsed into the pair ("", "z"). An empty value1
matches at every position and consumes nothing, so the scan in
replace_line never advanced and the program looped forever, printing
nothing and growing its output buffer until it was killed.

Reject the entry while parsing, reporting the offending line the same
way as a line with no "=" at all, and document the termination
invariant the loop depends on.
2026-08-26 14:35:32 +02:00
721d21a603 update 2026-08-25 22:10:56 +02:00
709d4ca60d chore: declare pytest in one place only 2026-08-25 22:10:25 +02:00
7 changed files with 99 additions and 35 deletions

5
.gitignore vendored
View File

@@ -9,5 +9,8 @@ venv/
.idea/
.vscode/
# assignment brief - kept locally, not published
# assignment brief
instructions.md
# local experiments
scratch/

View File

@@ -36,42 +36,71 @@ xnhjrfyjvth3nxr 1
## Design decisions
The assignment leaves a few points open. The choices made here are:
The assignment leaves a few points open:
**"Total number of symbols replaced" counts source symbols.** A line where
`bb` and `a` were both replaced counts 3, being the combined length of the
matched `value1` strings, not the number of replacements and not the length of
the substituted text. This makes the count a measure of how much of the
original line changed.
**"Total number of symbols replaced" counts source symbols.** A line where `bb`
and `a` were replaced counts 3, the combined length of the matched `value1`
strings, not the number of replacements.
**Overlapping patterns resolve to the longest match.** If the configuration
contains both `b` and `bb`, the input `bbbb` is read as two `bb` matches rather
than four `b` matches. The assignment guarantees unique values but not that
none is a prefix of another, so `prepare_pairs` orders the pairs by descending
length once, and `replace_line` takes the first match at each position. Without
a rule like this the output would depend on configuration file ordering.
**Overlapping patterns resolve to the longest match.** With both `b` and `bb`
configured, `bbbb` is two `bb` matches, not four `b`. Values are unique but may
still be prefixes of one another, so `prepare_pairs` orders by descending length
and `replace_line` takes the first match at each position. Otherwise the output
would depend on configuration file ordering.
**Ties keep their original file order.** The final sort keys only on the
replacement count and Python's sort is stable, so lines replaced an equal
number of times appear in the order they had in the input file.
**Ties keep their original file order.** The sort keys only on the count, and
Python's sort is stable.
**Unchanged lines are still printed.** The assignment says to sort the changed
lines but also to output the resulting text. Dropping lines that happened to
match nothing would silently lose content, so every line is printed and lines
with zero replacements simply sort last.
**Unchanged lines are still printed.** Dropping lines that matched nothing
would silently lose content, so every line is printed; those with no
replacements sort last.
**Input is preserved apart from the line terminator.** Both `\n` and `\r\n` are
stripped; every other character is kept and eligible for replacement. The
configuration format has no quoting either, so whitespace inside a value is
significant — the trailing space in `a=z ` is part of the replacement value.
**Only the line terminator is stripped.** `\n` and `\r\n` both; every other
character stays replaceable. The format has no quoting, so the trailing space in
`a=z ` is part of the value.
## Performance characteristics
`replace_line` tries every pair at each position, so cost grows with both the
line length and the number of pairs. Two alternatives, measured on the same
20,000-line input of 40-character lines and producing identical output:
| Pairs | this implementation | regex alternation | prefix tree |
| ----: | ------------------: | ----------------: | ----------: |
| 3 | 1x | 1x | 1x |
| 100 | 20x | 1.2x | 1.1x |
| 1000 | 207x | 1.1x | 1.1x |
Median of repeated runs, relative to each column's own three-pair baseline, so
the figures do not depend on the machine. The three-pair run takes about a
quarter of a second. Neither alternative is quite flat, because the larger
configurations in this benchmark also use longer keys, which cost slightly more
to match.
The regex alternation joins every `value1` into one pattern (`a|bb|c`) that
Python's `re` module scans in C: a constant-factor win, the same work done
faster. The prefix tree is the algorithmic one, taking the pair count out of the
complexity rather than making each step cheaper. The explicit loop is kept
because at the sizes described here the difference is not observable, and it
keeps the longest-match rule visible in the code.
The alternation is only equivalent because `prepare_pairs` orders the patterns
longest first: `re` is leftmost-first rather than leftmost-longest, so `b|bb`
would otherwise match `b` against the input `bb`.
Memory is proportional to the input size, since every line is held before
sorting.
## Error handling
The configuration file is validated while loading: a line without `=` is
rejected, reporting the offending line number. Missing files, unreadable
files, and input that is not valid UTF-8 are reported as messages on stderr
rather than tracebacks, and exit with status 1. Status 2 is reserved by
`argparse` for command line misuse.
The configuration file is validated while loading: a line without `=`, and a
line whose `value1` is empty, are both rejected, reporting the offending line
number. An empty `value1` would match at every position without consuming
anything, which is why it is refused rather than ignored.
Missing files, unreadable files, and input that is not valid UTF-8 are reported
as messages on stderr rather than tracebacks, and exit with status 1. Status 2
is reserved by `argparse` for command line misuse.
## Tests
@@ -81,8 +110,9 @@ pytest
```
- `tests/unit/` covers configuration parsing and the replacement algorithm,
including the overlapping-pattern and empty-input edge cases, plus a line by
line check of the worked example in `test_sample_data.py`.
including the overlapping-pattern case, empty input, and the rejection of an
empty `value1`, plus a line by line check of the worked example in
`test_sample_data.py`.
- `tests/integration/test_cli.py` invokes `main.py` as a subprocess to cover
what unit tests cannot: argument handling, exit status, stderr messages, and
the sort order as actually printed.

View File

@@ -18,6 +18,15 @@ def load_config(filename: str) -> list[tuple[str, str]]:
)
value1, value2 = line.split("=", 1)
# An empty value1 would match at every position without
# consuming anything, leaving replace_line unable to advance.
if not value1:
raise ValueError(
f"Invalid configuration at line {line_number}: {line!r} "
f"(the value to replace must not be empty)"
)
pairs.append((value1, value2))
return pairs

View File

View File

@@ -5,9 +5,6 @@ description = "Replace configured values in a text file."
requires-python = ">=3.9"
dependencies = []
[project.optional-dependencies]
dev = ["pytest>=7.0"]
[tool.pytest.ini_options]
pythonpath = ["."]
testpaths = ["tests"]

View File

@@ -24,6 +24,8 @@ def replace_line(line: str, pairs: list[tuple[str, str]]) -> tuple[str, int]:
replaced_symbols = 0
position = 0
# Terminates because every branch below advances `position`, which
# holds only while no value1 is empty. `load_config` rejects those.
while position < len(line):
match = None

View File

@@ -55,4 +55,27 @@ def test_empty_config_file(tmp_path):
config_file = tmp_path / "config.txt"
config_file.write_text("", encoding="utf-8")
assert load_config(str(config_file)) == []
assert load_config(str(config_file)) == []
def test_empty_value1_raises_value_error(tmp_path):
"""A line such as '=z' has no value1 and must be rejected.
An empty value1 matches at every position and consumes nothing, so
the scan in replace_line would never advance past it. Rejecting the
pair here keeps that invariant a parsing concern rather than
something the replacement loop has to defend against.
"""
config_file = tmp_path / "config.txt"
config_file.write_text("a=z\n=x\n", encoding="utf-8")
with pytest.raises(ValueError):
load_config(str(config_file))
def test_line_that_is_only_an_equals_sign_raises_value_error(tmp_path):
"""'=' alone has neither value1 nor value2."""
config_file = tmp_path / "config.txt"
config_file.write_text("=\n", encoding="utf-8")
with pytest.raises(ValueError):
load_config(str(config_file))