Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
want to use getRow inside allRowsValid for sudoku solver how can i solve it?

So i have been trying to write a sudoku solver and i had to complet methods inside an extend class here's the head of the code:

class Board extends EventEmitter {
  constructor(board) {
    super();

    this.board = board || [
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0],
    ];
  }

  getRow(index) {
    return this.board[index];
  }

here i have to check if all the rows inside that board are valid (from 1 to 9 no repet):

allrowsValid() {
  for (let c = 0; c < 9; ++c) {
    **var row = this.getRow(c)** ***//what i need to fix***

      for ( let num = 1; num <= 9; ++num){
        if (this.board[row][c] === num) {
          return false;
      }
      }
    }
    return true;
    }

How can i solve it ? thanks !

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It depends whether "valid" means "full house" (all numbers in range [1-9]), or not invalid (some numbers in range [1-9] without repetition). I've used bit arithmetic to specifically address this in Sudoku in the past:

// class methods...

validHouse( array) {
    // no non zero repetitions
    let allowed = 0b1111111110;
    for( let index = 9; index--;) {
        if( array[index]) {
            let position  = 1 << array[index];
            if( !(allowed & position)) {
                 return false; // repetition
            }
            allowed &= ~position;
        }
    }
    return true;
}

fullHouse(array) {
    // all house numbers completed
    let required = 0b1111111110;
    for( let index = 9; index--;) {
        if( array[index]) {
            let position  = 1 << array[index];
            required &= ~position;
        }
    }
    return required == 0;
}

So if you wanted to know if all rows were complete or valid you could use

allRowsFull() {
    return this.board.every(row => this.fullHouse(row));
}

allRowsValid() {
   return this.board.every(row => this.validHouse(row));
}

The point here is not to push you into using binary arithmetic (it's an acquired taste1) but to point out that a row is only one of the three types of Sudoku houses (row, column and box) that need to be considered.


1 Answer updated to use the bit-wise complement operator (~) instead of the logical not operator (!) in bit clearing operations. They are not interchangeable.

about 4 years ago · Juan Pablo Isaza Report

0

if (this.board[row][c] === num)

The problem is at this line, you are trying to get the row by using the row. The row variable is already referencing the row.

So the solution is to replace this.board[row][c] with row[c]

for ( let num = 1; num <= 9; ++num){
    if (row[c] === num) {
        return false;
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!