The three rules of Ruby Quiz:
1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.
2. Support Ruby Quiz by submitting ideas as often as you can:
3. Enjoy!
···
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Bill Kleb
This week's quiz is to write a version of the diff command that compares files
numerically. In Unix-speak,
$ ndiff --help
Usage: ndiff [options] file1 file2
Numerically compare files line by line, numerical field by numerical field.
-d INT --digits INT Maximum number of significant digits that
should match. (default: 0)
-h --help Output this help.
-q --quiet No output, just exit code.
-s --statistics Provide comparison statistics only. (extra credit)
-t DBL --tolerance DBL Tolerate <= DBL distance between numbers.
(default: 0.0)
For example, given fileA,
1.000001
2.00
-3
Cy=0.11278889E-01 Cx=-1.343e+02
And fileB,
1.000000
1.99
-3.4
Cy=0.11278890E-01 Cx=-1.343e+02
the following scenarios could play out:
$ ndiff fileA fileB
1,4c1,4
< 1.000001
< 2.00
< -3
< Cy=0.11278889E-01 Cx=-1.343e+02
---
> 1.000000
> 1.99
> -3.4
> Cy=0.11278890E-01 Cx=-1.343e+02
$ ndiff -t 0.000001 fileA fileB
2,3c2,3
< 2.00
< -3
---
> 1.99
> -3.4
$ ndiff --tolerance 0.01 fileA fileB
3c3
< -3
---
> -3.4
$ ndiff --digits 1 fileA fileB (zero exit code)
$ ndiff -q fileA fileB (non-zero exit code)
and, for extra credit,
$ ndiff --statistics fileA fileB
Numbers compared: 5
Distance range: 0.0..0.4
Average distance: 0.99987e-01 [guess]
Mean distance: 1.0e-06 [guess]
FWIW, the results of this quiz will be used by NASA's FUN3D
team for regression testing aerothermodynamic simulation
software.