Log in

JS - Place Queens - self checker wrong

  • 2
  • 2 Dec '15

Note: This doesn't apply to the auto-testing. It correctly checked solution.
The self-checker for the Place Queens problems was always failing. Here are the changes I made to it check correctly:

    var COLS = "abcdefgh";
    var ROWS = "12345678";
    var checkCoordinate = function (coor) {
            var c = coor[0];
            var r = coor[1];
            //return COLS.indexOf(c) !== -1 && ROWS.indexOf(r); // failed when r = '1' (because index was 0)
            return COLS.indexOf(c) !== -1 && ROWS.indexOf(r) !== -1;
    };
    var checker = function (func, placed, isPossible) {
    var result = func(placed.slice());
    for (var i = 0; i < result.length; i++) {
        var coor = result[i];
        if (typeof(coor) !== "string" || coor.length !== 2 || !checkCoordinate(coor)) {
            console.log("Wrong Coordinates");
            return false;
        }
    }

    if (!isPossible) {
        //if(placed.length > 0) { // this failed if any queens were initially placed
        if (result.length) {
            console.log("Hm, how did you place them?");
            return false;
        }
        else {
            return true;
        }
    }

    for (i = 0; i < result.length; i++) {
        var fCol = COLS.indexOf(result[i][0]);
        var fRow = ROWS.indexOf(result[i][1]);
        for (var j = i + 1; j < result.length; j++) {
            var sCol = COLS.indexOf(result[j][0]);
            var sRow = ROWS.indexOf(result[j][1]);

            if (fCol === sCol || fRow === sRow
                || (fRow - fCol === sRow - sCol) || ((fRow + fCol === sRow + sCol))) {
                console.log("I see some problems in this placement.");
                return false;
            }
        }
    }

    // was in the loop above
    for(i = 0; i < placed.length; i++) {
       // if (placed.indexOf(result) === -1) { // this always failed
       if (result.indexOf(placed[i]) === -1) {
            console.log("You forgot about placed queens.");
            return false;
        }
    }

    return true;
};
  • 2 Dec '15

which test is wrong?

  • 2 Dec '15

Both self-test assertions failed:
assert(checker(placeQueens, ["b2", "c4", "d6", "e8"], true), "1st Example"); //failed with error 'Wrong Coordinates' because checkCoordinate(coord) fails when the row = '1'.
assert(checker(placeQueens, ["b2", "c4", "d6", "e8", "a7", "g5"], false), "2nd Example"); //failed with error 'Hm, how did you place them?' because when !isPossible it's checking the length of the placed queens (input) instead of the results (output).

JChousePhil Rigovanov
  • 22 Feb '16

My solution also cannot pass assertions, but have passed all tests with success when it checked.

  • 1
  • 19 Apr

Self checker still wrong. Just do "Check" and skip "Run".

Reply