Created
January 28, 2019 00:43
-
-
Save therealklanni/2cdfa28014db6bedaad7202924276b35 to your computer and use it in GitHub Desktop.
Sudoku Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { sudokuVerifier } from '../src/sudoku_verifier' | |
import problems, { sudoku } from '../problems' | |
function assertValid(problem, solution) { | |
expect( | |
sudokuVerifier({ problem, solution }) | |
).toEqual({ status: 'valid', invalidIndexes: [ ] }) | |
} | |
function assertIncomplete(problem, solution) { | |
expect( | |
sudokuVerifier({ problem, solution }) | |
).toEqual({ status: 'incomplete', invalidIndexes: [ ] }) | |
} | |
function assertInvalid(problem, solution, expectedInvalidIndexes) { | |
const verificationData = sudokuVerifier({ problem, solution }) | |
expect(verificationData.status).toEqual('invalid') | |
function compare(a, b) { | |
return a - b | |
} | |
expect(verificationData.invalidIndexes.sort(compare)).toEqual(expectedInvalidIndexes.sort(compare)) | |
} | |
describe('When called with the first sudoku problem', function() { | |
describe('and a valid solution', function() { | |
it('it should have a valid status and an empty invalidIndexes array', function() { | |
assertValid(problems[0].problemData, problems[0].solutions[0]) | |
}) | |
}) | |
describe('and a valid incomplete solution', function() { | |
it('it should return a status of incomplete and an empty invalidIndexes array', function() { | |
assertIncomplete(problems[0].problemData, problems[0].solutions[1]) | |
}) | |
}) | |
describe('and an invalid complete solution', function() { | |
it('it should return a status of invalid and an array with invalid cell indexes', function() { | |
assertInvalid( | |
problems[0].problemData, | |
problems[0].solutions[2], | |
[ 7, 16, 27, 29, 64, 72 ] | |
) | |
}) | |
}) | |
describe('and an invalid incomplete solution', function() { | |
it('it should return a status of invalid and an array with invalid cell indexes', function() { | |
assertInvalid( | |
problems[0].problemData, | |
problems[0].solutions[3], | |
[ 1, 2, 29, 38 ] | |
) | |
}) | |
}) | |
}) | |
describe('When called with the second sudoku problem', function() { | |
describe('and a valid solution', function() { | |
it('it should have a valid status and an empty invalidIndexes array', function() { | |
assertValid(problems[1].problemData, problems[1].solutions[0]) | |
}) | |
}) | |
describe('and a valid incomplete solution', function() { | |
it('it should return a status of incomplete and an empty invalidIndexes array', function() { | |
assertIncomplete(problems[1].problemData, problems[1].solutions[1]) | |
}) | |
}) | |
describe('and an invalid complete solution', function() { | |
it('it should return a status of invalid and an array with invalid cell indexes', function() { | |
assertInvalid( | |
problems[1].problemData, | |
problems[1].solutions[2], | |
[ 18, 20, 21, 40, 48, 60, 61, 62, 66, 68, 69 ] | |
) | |
}) | |
}) | |
describe('and an invalid incomplete solution', function() { | |
it('it should return a status of invalid and an array with invalid cell indexes', function() { | |
assertInvalid( | |
problems[1].problemData, | |
problems[1].solutions[3], | |
[ 34 ] | |
) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import problems from '../problems'; | |
import _ from 'lodash'; | |
export function sudokuVerifier({ problem, solution }) { | |
let invalidIndexes = []; | |
let status = 'valid'; | |
const answer = problems.filter(({problemData}) => | |
problemData.toString() === problem.toString())[0].solutions[0]; | |
const solved = solution.every((a, i) => a === answer[i]); | |
const incompleteIndices = solution.map((v, i) => v === null ? i : false).filter(v => v !== false); | |
const incorrectIndices = solution | |
.map((v, i) => v === answer[i] ? false : i) | |
.filter(v => v !== false) | |
.filter(v => !incompleteIndices.includes(v)); | |
if (!solved && incompleteIndices.length && !incorrectIndices.length) { | |
status = 'incomplete' | |
} else if (!solved) { | |
status = 'invalid' | |
invalidIndexes = incorrectIndices | |
} | |
return { status, invalidIndexes } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment