Quotes help make search much faster. Example: "Practice Makes Perfect"
Showing posts with label Code Garage. Show all posts
Showing posts with label Code Garage. Show all posts
Thursday, December 20, 2012
LearnStreet Sample Solution Sudoku Solver Project Method 8
function isSolved(sudoku) {
// A sudoku will be solved if all its rows, columns and blocks are correct.
// Use a combination of the previous three functions -- isCorrectRow(),
// isCorrectColumn(), and isCorrectBlock() -- to determine if the 'sudoku'
// is solved and return true if this is the case. If the sudoku is not solved,
// you should return false.
// REPLACE THIS CODE WITH YOUR isSolved() METHOD
var returnValue = true;
//var block = getCellBlock(row,col);
for(var i=0; i<9; i++){
if(!isCorrectRow(sudoku, i)){
returnValue = false;
}
if(!isCorrectColumn(sudoku, i)){
returnValue = false;
}
if(!isCorrectBlock(sudoku, i)){
returnValue = false;
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 7
function isCorrectBlock(sudoku, block) {
// This function is very similar to the previous two above except
// that you will again have to figure out how to index all the elements
// in the 'sudoku' array for the block specified by the 'block' argument.
// This function should return true if all the numbers from 1 to 9 are
// in the block. It should return false if the block is incomplete or
// contains duplicates.
// REPLACE THIS CODE WITH YOUR isCorrectBlock() METHOD
var returnValue = true;
var blockCopy = [];
for(var i=0; i<9; i++){
for(var j=0; j<9; j++){
if(getCellBlock(i,j)===block){
blockCopy.push(sudoku[i][j]);
}
}
}
blockCopy.sort();
for(var i=1; i<=9; i++){
if(blockCopy[i-1]!==i){
returnValue = false;
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 6
function isCorrectColumn(sudoku, col) {
// This method should return true if all the numbers from 1 to 9
// are present in the column indexed by the 'col' argument in the
// 'sudoku' puzzle. It should return false if the column is incomplete
// or has duplicates.
// This method is analogous to the isCorrectRow() method above.
// REPLACE THIS CODE WITH YOUR isCorrectColumn() METHOD
var returnValue = true;
var colCopy = [];
for(var i=0; i<9; i++){
colCopy[i] = sudoku[i][col];
}
colCopy.sort();
for(var i=1; i<=9; i++){
if(colCopy[i-1]!==i){
returnValue = false;
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 5
function isCorrectRow(sudoku, row) {
// This method should return true if all the numbers from 1 to 9 are
// present in the row indexed by the 'row' argument in the 'sudoku'
// puzzle. It should return false if the row is incomplete or has duplicates.
// Note that the "empty" rows will actually contain the value 0.
// 1) Start off by making a *copy* of the row.
// 2) Then sort the new array that you have made so that the
// numbers run in ascending order.
// 3) You should then be able to run through this sorted array and
// easily determine if all the required numbers are there.
// REPLACE THIS CODE WITH YOUR isCorrectRow() METHOD
var returnValue = true;
var rowCopy = [];
for(var i=0; i<9; i++){
rowCopy[i] = sudoku[row][i];
}
rowCopy.sort();
for(var i=1; i<=9; i++){
if(rowCopy[i-1]!==i){
returnValue = false;
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 4
function isPossibleNumber(sudoku, row, col, number) {
// Now you will start using of the other methods you've written above.
// This method accepts a 'sudoku' puzzle, a 'row' and a 'col' (column), and a
// possible 'number' to move into that row/column position.
// Such a move would be possible if: 1) isPossibleRow() returns true for
// that row, and 2) ifPossibleColumn() returns true for that column, and
// 3) isPossibleBlock() returns true for the block that sudoku[row][col] is
// in. Use the getCellBlock() function below to find which block this is. If all of
// these functions return true, then isPossibleNumber() should return true.
// If any of the those functions returns false, then ifPossibleNumber() should
// return false.
// REPLACE THIS CODE WITH YOUR isPossibleNumber() METHOD
var returnValue = false;
var block = getCellBlock(row,col);
if(isPossibleRow(sudoku, row, number) && isPossibleColumn(sudoku, col, number) && isPossibleBlock(sudoku, block, number)){
returnValue = true;
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 3
function isPossibleBlock(sudoku, block, number) {
// A 'block' is a 3x3 area of the 'sudoku' in which numbers 1 to 9
// must all be present. There are 9 of these zones in the Sudoku.
// This method will return true if the 'number' argument is not already
// in the block of the 'sudoku' specified by the 'block' argument --
// again indicating a potentially legal move. (The 'sudoku' argument is,
// as always, a 9x9 two-dimensional array.)
// This method should return false if the 'number' already exists in the
// specified 'block'.
// The blocks are indexed from 0 to 8. The top row blocks being 0 to 2,
// the middle row blocks being from 3 to 5 and the bottom row being 6 to 8.
// Btw, the getCellBlock() function below might be helpful here.
// REPLACE THIS CODE WITH YOUR isPossibleBlock() METHOD
var returnValue = true;
for(var i=0; i<9; i++){
for(var j=0; j<9; j++){
if(getCellBlock(i,j)===block && sudoku[i][j]===number){
returnValue = false;
}
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 2
function isPossibleColumn(sudoku, col, number) {
// This is a lot like isPossibleRow() above: it returns true if the 'number'
// argument is NOT already in the column of the 'sudoku' indexed
// by the 'col' argument . This would mean that adding the
// 'number' to the 'col' for this 'sudoku' is a potentially legal move.
// This function should return false if the number already exists in
// the specified column.
// REPLACE THIS CODE WITH YOUR isPossibleColumn() METHOD
var returnValue = true;
for(var i=0; i<9; i++){
if(sudoku[i][col]===number){
returnValue = false;
}
}
return returnValue;
}
LearnStreet Sample Solution Sudoku Solver Project Method 1
// Sudoku Solver Project
function isPossibleRow(sudoku, row, number) {
// This isPossibleRow() function returns true if the 'number'
// argument is NOT already in the row of the 'sudoku' indexed
// by the 'row' argument . This would mean that adding the
// 'number' to the 'row' for this 'sudoku' is a potentially legal move.
// (The 'sudoku' argument is a 9x9 two-dimensional array.)
// This function should return false if the number already exists in
// the specified row.
// REPLACE THIS CODE WITH YOUR isPossibleRow() METHOD
var returnValue = true;
for(var i=0; i<9; i++){
if(sudoku[row][i]===number){
returnValue = false;
}
}
return returnValue;
}
function isPossibleRow(sudoku, row, number) {
// This isPossibleRow() function returns true if the 'number'
// argument is NOT already in the row of the 'sudoku' indexed
// by the 'row' argument . This would mean that adding the
// 'number' to the 'row' for this 'sudoku' is a potentially legal move.
// (The 'sudoku' argument is a 9x9 two-dimensional array.)
// This function should return false if the number already exists in
// the specified row.
// REPLACE THIS CODE WITH YOUR isPossibleRow() METHOD
var returnValue = true;
for(var i=0; i<9; i++){
if(sudoku[row][i]===number){
returnValue = false;
}
}
return returnValue;
}
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
favorite_animal ||= "dog" favorite_animal ||= "cat"
-
Answer: Alt - p p stands for previous. If you keep pressing Alt - p, you will continue cycling back through commands you typed before. I...
-
//sample solution def isEven(x): if(x % 2 == 0): return "yep" else: return "nope"
-
<!--sample solution--> <html> <head> <title>Week 13 Project</title> </head> ...
-
$("#tours > li")
-
#sample solution hungry = false unless hungry puts "I'm writing Ruby programs!" else puts "Time to eat!...
-
<!-- Sample solution to index.html --> <!DOCTYPE html> <html> <body> <div class="nav"> ...
-
<html> <head> <!-- Add the Parse SDK here! --> <script src="http://www.parsecdn.com/js/par...
-
//sample solution var yourName = "Pam"; var gender = "female"; var result; //Line 10 starts an if statement //Ne...
-
#sample solution count = 0 if count < 5: print "Hello, I am an if statement and count is", count while count ...