noodcon's picture mondk's picture
Duplicate from mondk/TerminalBench-DockerLess
b199558
|
Raw History Blame Contribute Delete
2.15 kB
# `canon` — canonicalise a CSV export
Downstream tools need one boring, byte-stable TSV. Write an executable script at
`bin/canon` (shebang, `chmod +x`) that is invoked as:
```
./bin/canon <input.csv> <output.tsv>
```
## Input
UTF-8 CSV, possibly with a byte-order mark, possibly CRLF, RFC 4180 quoting:
fields may be quoted, may contain commas, doubled quotes (`""`) and embedded
newlines. The first record is the header.
## Output
UTF-8 **without** a BOM, `\n` line endings, tab-separated, one line per record,
header first. Fields are written verbatim — never re-quoted, never trimmed —
except for the numeric normalisation below. Column order is preserved.
## Numeric normalisation
A field is numeric when the whole field matches
`^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$`. Numeric fields are rewritten in
plain decimal notation with no exponent, no leading `+`, no leading zeros, and
no trailing zeros after the decimal point:
| input | output |
| --- | --- |
| `1.50` | `1.5` |
| `1.0` | `1` |
| `1e3` | `1000` |
| `2.5e-3` | `0.0025` |
| `+5` | `5` |
| `007` | `7` |
| `-0` | `0` |
Every other field is copied unchanged.
## Ordering
Sort the data records by field 0, then by field 1, comparing the **normalised
strings** in byte order (so `"10"` sorts before `"9"`). The sort must be stable.
## Errors
On any error, print exactly one line to stderr, exit with the given code, and do
**not** create the output file:
| condition | stderr line | exit |
| --- | --- | --- |
| input path does not exist | `no such file: <input path as given>` | `2` |
| two header names collide case-insensitively after trimming | `duplicate column: <name as written in the later header field>` | `3` |
| a record's field count differs from the header's | `malformed row: line <n>` | `4` |
`<n>` is the 1-based physical line number where the offending record starts
(so a record whose first line is the third line of the file reports `3`).
On success exit `0` and print nothing to stderr.
## Notes
- `python` and `python3` are both on `PATH`; standard library only, no network.
- `data/orders.csv` is a sample with a BOM and CRLF endings.