4 Commits

2 changed files with 60 additions and 30 deletions

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