Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace spaces, validate string #1

Merged
merged 3 commits into from
Apr 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules/
*.swp
*~
*.log
node_modules
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ global.sameBlock = function(i, j) {
sudokuSolver.solve = function (options) {

// Expect at least one problem
options.problem = options.problem || 0;
var problem = options.problem;

// Replace spaces with nothing
if (problem && problem.constructor == String) {
problem = problem.replace (/ /g, "");
}

// Verify if we get a problem
if (options.problem == 0) {
console.log("I need a problem");
} else if (options.problem.length != 81) {
if (!problem || problem.constructor !== String) {
console.log("Problem mubst be a nonempty string.");
} else if (problem.length != 81) {
console.log("A valid sudoku problem should have a length of 81");
} else {
solver(options.problem);
solver(problem);
}

// This is called to solve our game
Expand Down
10 changes: 9 additions & 1 deletion test/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@ var SudokuSolver = require("../index");

SudokuSolver.solve({
// Problem from: http://projecteuler.net/index.php?section=problems&id=96
problem: '003020600900305001001806400008102900700000008006708200002609500800203009005010300'
problem: '0 0 3 0 2 0 6 0 0'
+ '9 0 0 3 0 5 0 0 1'
+ '0 0 1 8 0 6 4 0 0'
+ '0 0 8 1 0 2 9 0 0'
+ '7 0 0 0 0 0 0 0 8'
+ '0 0 6 7 0 8 2 0 0'
+ '0 0 2 6 0 9 5 0 0'
+ '8 0 0 2 0 3 0 0 9'
+ '0 0 5 0 1 0 3 0 0'
});