Skip to content

Doding dojo JS edition - Sudoku solver Kata. Artur Wodarz solution#1

Open
ArtWodarz wants to merge 2 commits intoCodingDojoSilesia:masterfrom
ArtWodarz:master
Open

Doding dojo JS edition - Sudoku solver Kata. Artur Wodarz solution#1
ArtWodarz wants to merge 2 commits intoCodingDojoSilesia:masterfrom
ArtWodarz:master

Conversation

@ArtWodarz
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Member

@inirudebwoy inirudebwoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending your solution.

Comment thread src/sudokuParser.js
function parseToArray(sudokuAsString) {
let sudokuArray = helper.createMatrix(9);
sudokuAsString.split("").forEach((el, index) => {
sudokuArray[Math.floor(index / 9)][index % 9] = el != "_" ? parseInt(el) : null;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Math.floor(index / 9)[index %9]is clever 😃
but it also is not tested. Since this is the smartest part of the whole function I'd move this calculation into a separate function that returns a tuple.

function arrayPosition(index: number): [number, number];

Comment thread test/solver.test.js

describe("Sudoku one", function() {
it("should be solved", function() {
var unsolved_sudoku = "___26_7_168__7__9_19___45__82_1___4___46_29___5___3_28__93___74_4__5__367_3_18___";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small inconsistency. Using linter and formatter may help you with these.

Comment thread src/solver.js
if (sudokuMatrix[x][y] == null) sudokuMatrix[x][y] = 0;
let newUnique = helper.findUniqueInt(
sudokuMatrix[x][y],
9,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a magic number. It also can be retrieved from parsed matrix dimension.

Comment thread src/solver.js
const parser = require("./sudokuParser");
const helper = require("./helper");

function getRelatedCells(cell, sudokuMatrix) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When reading body of solve function my initial though when reached line with getRelatedCells was to look into helper module. I was wrong 😄 Maybe you should move it there.

Comment thread src/solver.js
while (helper.inRange(0, modifiableCells.length - 1, nowModified)) {
let x = modifiableCells[nowModified].row;
let y = modifiableCells[nowModified].col;
if (sudokuMatrix[x][y] == null) sudokuMatrix[x][y] = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you only set the cell to 0 so helper.findUniqueInt can work. You should move this if into helper function.
Separate functions should not be aware of such details.

Comment thread src/helper.js
return block;
}

function extractRow(rowIndex, matrix) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed unit tests for this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants