recursive backtracking for sudoku? - javascript

Im trying to make a sudoku solver algorithm to practice recursion and backtracking, so made this function 'backtrack' which is the main function that's supposed to solve it , and four other 'utility/helper' functions;the first one is 'getSubGrid' which returns the subgrid where the current number belongs,and then 'getColumn' which returns the column where the current number belongs, and 'getRow' which returns the row where the current number belongs, and the last helper function is 'subGridIncludes' to check whether a subgrid includes a specific number or not.The problem is that for some boards, this code works and solve them, but for some other boards it doesn't and it gives me 'too much recursion' error.Note that i tried to calculate how many recursive calls each board requires and i found that the boards that worked required less than 6000 calls , but the one of the boards that didnt work required more than 10000 call before i got the message 'too much recursion',and i want to know if the error is because i didnt set a base case that was present in the boards that were not solved and not present in the boards that were solved, or is it because the board is just big and i need to rewrite the script in a way that requires the least amount of recursive calls.
these are examples of the boards that were succesfully solved:
board 1:
[
[7,8,0,4,0,0,1,2,0],
[6,0,0,0,7,5,0,0,9],
[0,0,0,6,0,1,0,7,8],
[0,0,7,0,4,0,2,6,0],
[0,0,1,0,5,0,9,3,0],
[9,0,4,0,6,0,0,0,5],
[0,7,0,3,0,0,0,1,2],
[1,2,0,0,0,7,4,0,0],
[0,4,9,2,0,6,0,0,7]
]
board 2:
[
[0,2,0,0,0,8,0,0,4],
[0,9,0,1,0,0,6,0,0],
[0,0,3,0,0,0,0,1,0],
[9,0,1,4,0,0,0,0,8],
[0,0,0,0,0,0,3,0,2],
[0,0,5,0,8,3,0,0,7],
[0,0,4,0,0,0,9,2,0],
[7,0,0,3,0,0,8,5,0],
[0,0,0,2,5,6,4,0,3]
]
and this is an example of a board that the function didnt solve:
[
[1,0,0, 0,5,0, 0,4,0],
[0,0,0, 9,0,0, 0,0,1],
[0,0,0, 0,0,3, 8,0,0],
[0,5,0, 8,0,2, 4,0,0],
[0,0,0, 0,0,0, 0,0,0],
[4,0,0, 0,0,0, 7,5,3],
[5,0,0, 3,0,0, 0,6,0],
[0,2,0, 0,0,1, 3,7,0],
[0,6,0, 4,0,9, 0,0,0]
]
and this is the code:
let lastPopped; // variable to keep track of the last changed value
let addedVals = [[]]; // array of arrays to keep track of the indexes where the values were changed on each row,where the index of each subarray corresponds to the index of the row
backtrack()
function backtrack(currentRowIndex = 0, currentRow = board[currentRowIndex], testValue = 1, i = 0) {
// testValue is a value put under test to check if its included in the row, column,subgrid
if (currentRowIndex === board.length) {
// this condition checks the board is finished ,if so it stops the execution of the function.
return
} else if (i === currentRow.length) {
// this condition checks if we reached the end of the current row ,if so it takes us to the next row.
lastPopped = undefined
addedVals.push(new Array())
backtrack(currentRowIndex + 1, currentRow = board[currentRowIndex + 1], testValue = 1, i = 0)
} else if (testValue > 9) {
// this condition checks if the testValue is greater than 9,if so we know that we should set the current value to 0 and we backtrack to the previous value.
currentRow[i] = 0
if (i === 0 || (addedVals[currentRowIndex].length === 0)) {
// this condition checks whether we are at the start of a row or the previous values are original, if so we backtrack to the previous row.
lastPopped = addedVals[currentRowIndex - 1].pop()
backtrack(currentRowIndex - 1, currentRow = board[currentRowIndex - 1], testValue = board[currentRowIndex - 1][lastPopped] + 1, i = lastPopped)
} else {
// if the previous condtion is not satisfied we backtrack to the previous value plus one in the same row.
lastPopped = addedVals[currentRowIndex].pop()
backtrack(currentRowIndex, currentRow = board[currentRowIndex], testValue = currentRow[lastPopped] + 1, i = lastPopped)
}
} else {
if (currentRow[i] === 0 || i === lastPopped) {
// this condition checks if the current value is a value that was not original or equal to zero.
if (!getColumn(currentRow[i], currentRowIndex).includes(testValue) && !subGridIncludes(0, testValue, getSubGrid(currentRow[i], currentRowIndex)) && !getRow(currentRow[i], currentRowIndex).includes(testValue)) {
//this condition checks whether the testValue is included in the row, column,subgrid,if so it assigns it to the current index,and pushes the index to the addedVals array to indicate that the current index's value has bee changes and keep trakc of it for comming purposes
currentRow[i] = testValue;
addedVals[currentRowIndex].push(i);
if (currentRow[i] != 0 && i === 8) {
//this condition enables us to move to the next row when needed
lastPopped = undefined
addedVals.push(new Array())
backtrack(currentRowIndex + 1, currentRow = board[currentRowIndex + 1], testValue = 1, i = 0)
} else {
//this condition enables us to move to the next index when needed
backtrack(currentRowIndex, currentRow = board[currentRowIndex], testValue = 1, i + 1)
}
} else {
//this condition adds 1 to the test value with each call.
backtrack(currentRowIndex, currentRow = board[currentRowIndex], testValue + 1, i)
}
} else {
if (currentRow[i] != 0 && i === 8) {
//this condition enables us to move to the next row when needed
addedVals.push(new Array())
backtrack(currentRowIndex + 1, currentRow = board[currentRowIndex + 1], testValue = 1, i = 0)
} else {
//this condition enables us to move to the next index when needed
backtrack(currentRowIndex, currentRow = board[currentRowIndex], testValue = 1, i + 1)
}
}
}
}
for (property in board) {
console.log(".", JSON.stringify(board[property]));
}
//this function checks whether a subgrid includes a specific number or not
function subGridIncludes(index, value, subgrd) {
if (index === subgrd.length) {
return false
} else {
return subgrd[index].includes(value) ? true : subGridIncludes(index + 1, value, subgrd)
}
}
//this function returns the subgrid where the element belongs.
function getSubGrid(num, index) {
let arr = new Array(3).fill([])
let oneDimentionalIndex, twoDimentionalIndex, tdiSub, odiSub;
let [rightOrBelow, leftOrAbove, rightOrBelowAndleftOrAbove] = [[0, 3, 6], [2, 5, 8], [1, 4, 7]];
for (let j = 0; j < board[index].length; j++) {
if (board[index][j] === num) {
oneDimentionalIndex = index;
twoDimentionalIndex = j;
break
}
}
rightOrBelow.includes(oneDimentionalIndex) ? odiSub = 0 : leftOrAbove.includes(oneDimentionalIndex) ? odiSub = 2 : rightOrBelowAndleftOrAbove.includes(oneDimentionalIndex) ? odiSub = 1 : null;
rightOrBelow.includes(twoDimentionalIndex) ? tdiSub = 0 : leftOrAbove.includes(twoDimentionalIndex) ? tdiSub = 2 : rightOrBelowAndleftOrAbove.includes(twoDimentionalIndex) ? tdiSub = 1 : null;
arr = arr.map((_, ind) => board[(oneDimentionalIndex - odiSub) + ind].slice(twoDimentionalIndex - tdiSub, ((twoDimentionalIndex - tdiSub) + 3)))
return arr
}
//this function returns the column where the element belongs.
function getColumn(num, index) {
let twoDimentionalIndex;
let column = new Array(board.length);
for (let j = 0; j < board[index].length; j++) {
if (board[index][j] === num) {
twoDimentionalIndex = j;
break
}
}
for (let i = 0; i < board.length; i++) {
column[i] = board[i][twoDimentionalIndex];
}
return column
}
//this function returns the row where the element belongs.
function getRow(num, index) {
let oneDimentionalIndex;
let row = new Array(board.length);
for (let j = 0; j < board[index].length; j++) {
if (board[index][j] === num) {
oneDimentionalIndex = index;
break
}
}
for (let i = 0; i < board.length; i++) {
row[i] = board[oneDimentionalIndex][i];
}
return row
}
The code obviously has a lot of violations of the DRY principle because i havent had much practice with code cleaning,so im sorry.

Related

Print every value within range iterator generator function javascript

Iterator should print every value within range but its only printing alternate nos.
function iterator(rangeStart, rangeEnd) {
if (rangeStart == 0 && rangeEnd == 0) {
return null;
}
var iterate = function*(start = 0, end = 5, step = 1) {
let iterationcount = 0;
for (let i = start; i <= end; i += step) {
yield i;
iterationCount = i;
}
return iterationCount;
}
var values = iterate(rangeStart, rangeEnd);
var tmp = [];
while (values.next().value != undefined) {
tmp.push(values.next().value);
}
return tmp.join(",");
}
console.log(iterator(0, 10))
expected
[0,1,2,3,4,5,6,7,8,9,10]
Result
[1,3,5,7,9,10]
Every call to next will consume a value from the iterator, so the while condition is consuming a value that will therefore not get into tmp.
But... JavaScript allows you to consume values in much easier ways. For instance with Array.from or spread syntax you can collect all values from the iterator into an array.
Not your question, but:
iterationCount serves no purpose in your code, so just drop that part.
Why would the function behave differently when both range start and end are 0 than when start and end are both 10? I would remove that case. When the range end would be less than the start, it would make sense to exit, but that will happen anyway without any if statement.
The name iterator for your function is somewhat misleading, as the return value is not an iterator, but a comma separated string. I would therefore call it rangeToCsv
function rangeToCsv(rangeStart, rangeEnd) {
var iterate = function*(start = 0, end = 5, step = 1) {
for (let i = start; i <= end; i += step) {
yield i;
}
}
var values = iterate(rangeStart, rangeEnd);
return Array.from(values).join(",");
}
console.log(rangeToCsv(0, 10))

Javascript find the most repetitive character occurrence from the string

Let's say we have this string:
BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF
As you can see, here B is occurring 4 times at first but B is also present before DDDD.
Similarly, A is occurring 4 times at the beginning and later 6 times.
I want the expected output if I am searching B it should 4 times as the max occurrence B is 4. However if I am searching A then it should return 6 because the most occurrence for A is 6.
Here is my code I tried:
function checkRepeatativeString(str) {
let hashMap = {};
let seen = new Set();
let counter = 1;
let maxValue = 1;
let isPreviousValueSame = false;
let isNextValueSame = true;
for (let i = 0; i < str.length; i++) {
/**
* is previous value same
*/
if (str[i] == str[i-1]) {
isPreviousValueSame = true;
}
/**
* is next value same
*/
if (str[i] == str[i+1]) {
isNextValueSame = true;
}
if (seen.has(str[i]) && isPreviousValueSame) {
hashMap[str[i]][0]++;
hashMap[str[i]][1]++;
isPreviousValueSame = false;
} else if(seen.has(str[i]) && !isNextValueSame) {
maxValue = Math.max(hashMap[str[i]][1], maxValue);
counter = 0;
hashMap[str[i]] = [counter, maxValue];
} else {
maxValue = Math.max(maxValue, counter);
seen.add(str[i]);
hashMap[str[i]] = [counter, maxValue];
isPreviousValueSame = false;
}
}
return hashMap;
}
let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));
This code is working but if you look for B, I am getting stuck at the beginning of value.
My program returns out for B:
B: [ 1, 1 ]
^ ^
Inside array, 1 is a counter which scans the string and second 1 in array is a max value which should return the output. However my program is returning 1 for B. I am expecting 4 as max value.
Help would be appreciated~
Quick and dirty.
function maxConsecutiveCharacters(check, haystack) {
if(check.length !== 1) return false;
let result = 0;
let buffer = 0;
for(let i = 0; i < haystack.length; i++) {
if(haystack[i] === check) {
buffer++;
}
else {
if(buffer > result) {
result = buffer;
}
buffer = 0;
}
if(buffer > result) {
result = buffer;
}
}
return result;
}
That looks overly complicated. Consider approaching the problem from a different angle - first split up the string into segments of repeating characters, and group them into an object based on the length of the longest substring for a given character.
const checkRepeatativeString = (str) => {
const longestCounts = {};
for (const consecutive of (str.match(/(.)\1*/g) || [])) {
const char = consecutive[0];
longestCounts[char] = Math.max(
longestCounts[char] || 0, // Use the existing value in the object if it exists and is higher
consecutive.length // Otherwise, use the length of the string iterated over
);
}
return longestCounts;
};
let str = "BBBBAAAABBAAAAAACCCCCBDDDDEEEEEEE,FFF";
console.log(checkRepeatativeString(str));
Simpler code often means less surface area for bugs.

Bird Watcher Exercism Javascript Solution - counter not counting

I'm trying to figure out an exercise from Exercism's Javascript track. It is an exercise using for loops called Bird Watcher.
The instructions say to initialize a function called birdsInWeek that takes two arguments. First is an array 'birdsPerDay', and the second is 'week'. Given the birdsPerDay array, the function is supposed to count the total number of birds seen in the specified week. So, if 'week' = 1, for example, the corresponding indexes of 'birdsPerDay' to add together would be 0 - 6, and so on and so forth.
I have tested my code using the provided test cases and in the Chrome console. I tried logging some values to understand where the bug is, but I can't figure out why my counter (named 'totalBirdsInSpecifiedWeek'), which is initialized to '0' is staying at '0' instead of adding the corresponding indexes of 'week' together and return the correct 'totalBirdsInSpecifiedWeek'. I have tried changing the placement of the return statement as well, but that didn't result in any change.
Below is the code I have written:
export function birdsInWeek(birdsPerDay, week) {
let totalBirdsInSpecifiedWeek = 0
if (week === 1) {
for (let i = 0; i < birdsPerDay[7]; i++) {
totalBirdsInSpecifiedWeek += birdsPerDay[i];
}
return totalBirdsInSpecifiedWeek;
} else {
for (let i = week * 7 - 7; i < birdsPerDay[week * 7]; i++) {
totalBirdsInSpecifiedWeek += birdsPerDay[i];
}
return totalBirdsInSpecifiedWeek;
};
}
Where did I go wrong?
birdsPerDay[7] this means values of birdsPerDay array at index 7 of the array
so the condition would loop checking if i is lesser than that value sometimes it would even hit the NaN so the idea is to just check on either length or index of the array to get an accurate response ..
Per the commenter #gog, I changed the 4th and 9th lines of code to correct the stopping condition in each of the for loops so they are written as indexes of the 'birdsPerDay' array and instead just numerical values.
export function birdsInWeek(birdsPerDay, week) {
let totalBirdsInSpecifiedWeek = 0
if (week === 1) {
for (let i = 0; i < 7; i++) {
totalBirdsInSpecifiedWeek += birdsPerDay[i];
}
return totalBirdsInSpecifiedWeek;
} else {
for (let i = week * 7 - 7; i < week * 7; i++) {
totalBirdsInSpecifiedWeek += birdsPerDay[i];
}
return totalBirdsInSpecifiedWeek;
}; return totalBirdsInSpecifiedWeek;
}
I found the exercise, according to the problem posed, I think this is the solution.
bird-watcher - exercism
Explanation, the array is divided by the days of the week, then the array is divided by that amount from 0 to 6 (Monday to Sunday), then I used a nested "for" to iterate over the corresponding week, I used another "for" to do the addition, return the value from the function and return the result.
let countsbirdsPerDay = [2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1];
let userWeek = 2;
const birdsInWeek = (birdsPerDay, week) => {
let segmentWeek = Math.round(birdsPerDay.length / week);
let total = 0;
for (let i = 0; i < week; i++) {
views = birdsPerDay.slice(i * segmentWeek, segmentWeek * (i + 1));
if ((i + 1) == week) {
for (let x = 0; x < views.length; x++) {
total = total + views[x];
}
}
}
return total;
}
console.log(birdsInWeek(countsbirdsPerDay, userWeek));

Java: return back true IF AND ONLY IF all of the list numbers are greater than zero

I am having trouble with returning back true IF AND ONLY IF all of the list numbers are greater than zero. It should return back true IF AND ONLY IF all of the list numbers are greater than zero. -5 is in the list, but after the program is run, it returns true, when it should be false ("Some of the numbers are not positive").
var allPositive = false;
var numberList = [2, -5, 1, 50, 4, 82, 34];
checkIfAllPositive();
console.log(allPositive);
if (allPositive == true)
{
console.log("All of the numbers are positive!");
}
else
{
console.log("Some of the numbers are not positive!");
}
function checkIfAllPositive()
{
for (var index = 0; index < numberList.length; index++) {
console.log(numberList[index] + " " + index + " " +(numberList[index] > 0));
if (numberList[index] <= 0)
{
allPositive = false;
} else {
allPositive = true;
}
}
}
There are some quirks in your function that you have to tune.
To solve your problem there are different ways:
The more efficient and useful to learn is this:
var numberList = [2, -5, 1, 50, 4, 82, 34];
function checkIfAllPositive(list) {
for (var index = 0; index < list.length; index++) {
if (list[index] <= 0) return false
}
return true
}
var allPositive = checkIfAllPositive(numberList)
console.log(allPositive)
Visit all array elements and as soon you discover a negative (or 0) one return false. If you peruse all the array elements without returning a false, you have to return true as all values are positives.
This code obey to a general rule Avoid side effects as much as possible:
You will not change any outside value from the body of a function.
You will not read any value in a function without naming it in the parameters declaration.
They are the twelfth and the Thirteenth commandments of the Programmer Book. It is a life saver rule you need to enforce almost everywhere, specially with Javascript that has some scope caveats that can drive you mad.
You code has a critical flaw, apart relying on side effects for input and output: When you evaluate a value in your list, you will lost the evaluation of the previous one. Apart my preferred procedural solution above, if you want to stick to the rule one entry -> one exit that I will recommend only for long functions. You have to account on this, maybe counting the negative values (sol b)
function checkIfAllPositive(numberList)
{
var negatives = 0;
for (var index = 0; index < numberList.length; index++) {
console.log(numberList[index] + " " + index + " " +(numberList[index] > 0));
if (numberList[index] <= 0)
{
negatives += 1;
}
}
return negatives > 0;
}
Or taking in account for previous values
function checkIfAllPositive(numberList)
{
var allPositiveSoFar = true;
for (var index = 0; index < numberList.length; index++) {
console.log(numberList[index] + " " + index + " " +(numberList[index] > 0));
if (numberList[index] <= 0)
{
// this is equivalent to allPosivitesSoFar = allPositiveSoFar && false;
allPositivesSoFar = false;
} else {
allPosivitesSoFar = allPositivevSoFar && true;
}
return negatives > 0;
}
If you want to have a little of fun you can also use a bit of functional programming to solve your problem in one line using the Array filter method:
var numberList = [2, -5, 1, 50, 4, 82, 34]
var allPositive = numberList.filter(a => a <= 0).length == 0
console.log(allPositive)
You invert your test and select (ie create a new array containing them) all the values that will cause a false response. If there is at least one, the expression will be false.
It is a not so efficient way to solve your task as the function filter will check all the elements even if the first one is negative.
A better approach is to use the Array some method
var numberList = [2, -5, 1, 50, 4, 82, 34]
var allPositive = !numberList.some(a => a <= 0)
console.log(allPositive)
I think it is self explanatory: the numbers are all positive if there aren't (!) negative values
When you loop through your array you need to break after one of them is false.
Like so:
function checkIfAllPositive()
{
for (var index = 0; index < numberList.length; index++) {
console.log(numberList[index] + " " + index + " " +(numberList[index] > 0));
if (numberList[index] <= 0)
{
allPositive = false;
break;
} else {
allPositive = true;
}
}
}
This is because once one of them is false it will set allPositive to false, but then it will keep going and if it finds another positive one then it will set allPositive to true even though there's a negative number in there.

JavaScript function not working for 0 value in array

This function is meant to find the highest variable in a list of variables, which have five id letters, and one number. It works fine with all the other slots, 2, 3, 4, 5, 6, 7, 8, 9, 10, but not 1. I need another set of eyes here.
The getVer function takes the number from the id; so ImpHt1 with getVer would be 1, while getShtNm gets ImpHt.
function find_max_feat(array_input,ShtNm) {
if (String(array_input[0]).length == 1) {
var max = 0;
}
else {
var max = getVer(array_input[0]);
}
var maxver = 0
var len = array_input.length;
for (var i = 0; i < len; i++) {
if (String(array_input[i]).length > 1) {
if (getShtNm(String(array_input[i])) == ShtNm) {
if (getVer(String(array_input[i])) > maxver) {
var max = array_input[i];
var maxver = getVer(String(array_input[i]));
}
}
}
}
return max;
}
0,DmnHt1_0,AltFm1_,0,0,0,0,0,0,0
An example of the array, which is why getVer is needed.
This is for a sheet generator, to be clear, but I've been working on the entire thing for at least a few days now, maybe even a week or weeks of on and off work.
The array above is generated any time a feat is selected, and the find_max_feat array is used to find the highest version in a group; it operates off of an infinite loop since nothing else I did could get it to work the way I wanted it to.
function checkFeats() {
updateFeatsel();
t=setTimeout("checkFeats()",1000);
}
function updateFeatsel() {
curselarray = new Array();
var selinc = 1;
while (selinc <= 10) {
var selincar = selinc - 1;
var selid = document.getElementById(String('ftlst' + selinc));
if (getVer(selid.options[selid.selectedIndex].title)) {
curselarray[selincar] = selid.options[selid.selectedIndex].title;
}
else {
curselarray[selincar] = 0;
}
selinc++;
}
document.getElementById('debug1').innerHTML = curselarray.valueOf();
featSelch('hlthm','ImpHt',healthom);
featSelch('strdmgm','ImpPd',Strpdom);
featSelch('strwhtm','ImpLi',Strwhtom);
featSelch('strsltm','EnhIt',StrSltom);
featSelch('endsurm','ImpEn',EndSurom);
featSelch('endsokm','ImpDf',EndSokom);
featSelch('intelmpm','ImpMg',Intelmom);
featSelch('willsokm','ImpMs',Willsokom);
featSelch('luckrllm','ImpLu',Lukrllom);
featSelch('luckpntm','EnhLu',Lukpntom);
featSelch('hlthbn','DmnHt',0);
featSelch('strbn','SupSt',0);
featSelch('luckbn','DmnLu',0);
featSelch('endbn','Armor',0)
document.getElementById('debug2').innerHTML = find_max_feat(curselarray,'DmnHt');
updateAmounts();
}
function featSelch(sid,fshtnm,defval) {
return document.getElementById(sid).innerHTML = getFeatvalue(fshtnm,defval);
}
That is because you are initialising max using getVer(array_input[0]) instead of array_input[0]. If the first item is the highest and has the version number zero, the initial value is used.
Change this:
var max = getVer(array_input[0]);
into:
var max = array_input[0];

Categories