Files
Technical-Assignment-CQG-Br…/README.md
Martin Stajnko 40ea68be11 Initial implementation with unit and integration tests
Reads value1=value2 pairs from a configuration file and replaces every
non-overlapping occurrence of value1 with value2 in a text file, printing
the resulting lines sorted by the number of symbols replaced.

Overlapping patterns resolve to the longest match so the result does not
depend on configuration file ordering. Ties keep their original file order.
2026-08-25 21:21:38 +02:00

117 lines
3.6 KiB
Markdown

# Replace Configured Values in a Text File
Reads `value1=value2` pairs from a configuration file, replaces every
non-overlapping occurrence of `value1` with `value2` in a text file, and prints
the resulting lines sorted by how many symbols were replaced, most-changed
first.
## Requirements
Python 3.9 or newer. No runtime dependencies.
## Usage
```bash
python main.py <config_file> <text_file>
```
### Example
With the provided `config.txt`:
```
a=z
bb=y
c=x
```
and `input.txt`:
```
jgrebbk6hnae
cnhjrfyjvth3nxr
b#sjcf_ansbbbbbvo!
dajf#aemfaocfna%
```
running:
```bash
python main.py config.txt input.txt
```
produces:
```
b#sjxf_znsyybvo! 6 symbols replaced
dzjf#zemfzoxfnz% 5
jgreyk6hnze 3
xnhjrfyjvth3nxr 1
```
(annotations added here for clarity; the program prints the lines only)
## Design decisions
The assignment leaves a few points open. The choices made here are:
**"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.
**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.
**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.
**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.
**Whitespace inside configuration values is significant.** The format has no
quoting or escaping, so a trailing space in `a=z ` is kept as part of the
replacement value; stripping it would make such a value impossible to express.
**Only the line terminator is stripped.** Both `\n` and `\r\n` are handled, and
every other character of the line is preserved and eligible for replacement.
## 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.
## Tests
```bash
pip install -r requirements-dev.txt
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`.
- `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.
## Layout
```
main.py command line entry point, sorting and output
config_loader.py configuration file parsing and validation
replacer.py replacement algorithm
tests/ unit and integration tests
```