Skeleton of a very simple TCR (test && commit || revert
) setup.
Each time the script is run, if all tests succeed we commit all changes.
If tests fail, we revert any production code changes (but not tests) since the last commit.
The process
- Write a test
./tcr
to confirm the test fails- Write enough just enough code to pass the test
./tcr
to get to green and commit (or revert, and go back to step 1, if we fail)- Write "real" code if needed to pass the tests honestly
./tcr
to get to green and commit (or revert, and go back to step 1, if we fail)- Repeat
Advanced version
Run ./tcrloop
while coding. This will run the test/commit/revert script every time a file is edited and the files are in a compilable state.
After a successful (or reversion) the script will attempt to git pull
and git push
.
The exercise - Magic Square
Write a function to validate 3x3 magic squares. A magic square is one where all rows, all columns, and both diagonals sum to the same total. For 3x3 magic squares containing the digits 1-9, that total will be 15 in all directions.
Input
A 3x3 array of integers:
[
[1,2,3],
[4,5,6],
[7,8,9]
]
(the above is not a valid matching square; for example, the first row sums to 6 while the last sums to 24)
Output
true
if the magic square is valid, false
otherwise.
Tests
- Size must be 3x3
- All digits 1-9 must be present
- Each row sums to 15
- Each column sums to 15
- Each diagonal (top left + center + bottom right, top right + center + bottom left) sums to 15
Sample valid magic squares:
[
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
]
[
[4, 9, 2],
[3, 5, 7],
[8, 1, 6]
]
Preliminaries
Install the entr utility from Homebrew, by running brew install entr
.
Run npm install
in this folder to install needed node dependencies.