How to find the path in a 2d array - javascript

I'm trying to work on creating a 2d array
where variable a is true otherwise false.
I know i have to use recursion but i'm not sure how to go about this
var a = [[
[0,0,0,0,0],
[1,1,1,1,0],
[0,0,0,0,0],
[0,1,1,1,1],
[0,0,0,0,0]
]]
function MazeSolver(maze) {
this.maze = maze;
this.traverse = function(column, row) {
console.log(1, this.maze[0][column][row] )
if(this.maze[0][column][row] == 2) {
console.log('1dwffdf',this.maze[0][column][row]);
} else if(this.maze[0][column][row] == 1) {
console.log("At valid position (" + column + ", " + row + ")");
this.maze[0][column][row] = 9;
if(column < this.maze[0].length - 1) {
this.traverse(column + 1, row);
}
if(row < this.maze[0][column].length - 1) {
this.traverse(column, row + 1);
}
if(column > 0) {
this.traverse(column - 1, row);
}
if(row > 0) {
this.traverse(column, row - 1);
}
}
};
};
any ideas?

This code will iterate through the maze and reduce the values to an array of objects containing each col and row of the correct path.
Note Array.prototype.flat is still experimental so the below may not be compatible.
let path = maze.reduce((acc, _,row)=> {
let validpath = _.reduce((acc,__,col)=> ((__)&&acc.push({col,row}), acc),[] );
if(validpath.length > 0) acc.push(validpath);
return acc;
}, []).flat(0);
Note This version uses Array.prototype.concat which can be used in combination with the spread operator as a polyfill for Array.prototype.flat:
let path = [].concat(...maze.reduce((acc, _,row)=> {
let validpath = _.reduce((acc,__,col)=> ((__)&&acc.push({col,row}), acc),[] );
if(validpath.length > 0) acc.push(validpath);
return acc;
}, []));
Try it out:
var a = [[
[0,0,0,0,0],
[1,1,1,1,0],
[0,0,0,0,0],
[0,1,1,1,1],
[0,0,0,0,0]
]];
let maze = a[0];
let path = [].concat(...maze.reduce((acc, _,row)=> {
let validpath = _.reduce((acc,__,col)=> ((__)&&acc.push({col,row}), acc),[] );
if(validpath.length > 0) acc.push(validpath);
return acc;
}, []));
console.log(path);
Note: Keep in mind that the col and row values are based on indices of the array, which begin at 0.

Related

Javascript Text Statistic Algorithm Improvement

I am trying to solve one algorithm in Javascript where the user requires the input sentence then have to do statistic as the following screenshot
I have done with following code
class TextAnalytics {
getAnalytics(sentence) {
var analyzedResult = {}
var textArray = new Array();
const trimmed = sentence.replace(/\s/g, '').toUpperCase()
for (let i = 0; i < trimmed.length; i++) {
const currentChar = trimmed[i]
if (!analyzedResult[currentChar]) {
analyzedResult[currentChar] = {
count: 1,
prevChar: trimmed[i - 1] ? [trimmed[i - 1]] : [],
nextChar: trimmed[i + 1] ? [trimmed[i + 1]] : [],
index: [i]
}
} else {
analyzedResult[currentChar].count++
trimmed[i - 1] &&
analyzedResult[currentChar].prevChar.push(trimmed[i - 1])
trimmed[i + 1] &&
analyzedResult[currentChar].nextChar.push(trimmed[i + 1])
analyzedResult[currentChar].index.push(i)
}
}
return analyzedResult;
}
getMaxDistance(arr) {
let max = Math.max.apply(null, arr);
let min = Math.min.apply(null, arr);
return max - min;
}
}
var textAnalytics = new TextAnalytics();
console.log(textAnalytics.getAnalytics("its cool and awesome"));
Want to check if there is any other way to solve this problem or any refactoring require
Help will be appreciated.
Thanks
You can write it more elegantly:
class CharStats {
constructor () {
this.prevs = [];
this.nexts = [];
this.indexes = [];
}
add (prev, next, index) {
prev && this.prevs.push(prev);
next && this.nexts.push(next);
this.indexes.push(index);
return this;
}
get count () {
return this.indexes.length;
}
get maxDistance () {
// If the index array is empty, the result will be Infinite.
// But because the algorithm cannot have a situation where
// this class is used without at least one index, this case
// need not be covered.
return Math.max(...this.indexes) - Math.min(...this.indexes);
}
}
const getAnalytics = sentence =>
[...sentence.replace(/\s/g, '').toUpperCase()].reduce((map, cur, i, arr) =>
map.set(cur, (map.get(cur) || new CharStats).add(arr[i - 1], arr[i + 1], i)),
new Map);
console.log(getAnalytics('its cool and awesome'));
1) Convert string to array of chars, remove empty, change to upper case
2) Use reduce, go thru each char and build object 'keys' as Char values to have before, after and index.
3) if Char already exist in object, Append new stats and calculate max-distance.
const getAnalytics = str => {
const caps = Array.from(str.toUpperCase()).filter(x => x.trim());
return caps.reduce((acc, char, i) => {
const prepost = {
before: caps[i-1] || '',
after: caps[i+1] || '',
index: i
};
if (char in acc) {
const chars = [...acc[char].chars, prepost];
const mm = chars.reduce((acc, curr) => ({
max: Math.max(acc.max, curr.index),
min: Math.min(acc.min, curr.index)
}), {max: -Infinity, min: Infinity});
acc[char] = { chars, max_distance: mm.max - mm.min };
} else {
acc[char] = { chars: [prepost], max_distance: 0 };
}
return acc;
}, {});
}
console.log(getAnalytics('its cool and awesome'));

Javascript reduce() until sum of values < variable

I am fetching an array of video durations (in seconds) from a JSON file in Javascript, that, to simplify, would look like this:
array = [30, 30, 30]
I would like to add each value to the previous value until a condition is met (the sum being less than a variable x) and then to get both the new value and the index position in the array of the video to play.
For example if x=62 (condition), I would like the first two values in the array to be added (from my understanding reduce() is appropriate here), and the index = 2 (the second video in the array).
I've got the grasp of reduce():
var count = array.reduce(function(prev, curr, index) {
console.log(prev, curr, index);
return prev + curr;
});
But can't seem to get beyond this point.. Thanks
You could use Array#some, which breaks on a condition.
var array = [30, 30, 30],
x = 62,
index,
sum = 0;
array.some(function (a, i) {
index = i;
if (sum + a > x) {
return true;
}
sum += a;
});
console.log(index, sum);
With a compact result and this args
var array = [30, 30, 30],
x = 62,
result = { index: -1, sum: 0 };
array.some(function (a, i) {
this.index = i;
if (this.sum + a > x) {
return true;
}
this.sum += a;
}, result);
console.log(result);
var a = [2,4,5,7,8];
var index;
var result = [0, 1, 2, 3].reduce(function(a, b,i) {
var sum = a+b;
if(sum<11){
index=i;
return sum;
}
}, 2);
console.log(result,index);
What about using a for loop? This is hack-free:
function sumUntil(array, threshold) {
let i
let result = 0
// we loop til the end of the array
// or right before result > threshold
for(i = 0; i < array.length && result+array[i] < threshold; i++) {
result += array[i]
}
return {
index: i - 1, // -1 because it is incremented at the end of the last loop
result
}
}
console.log(
sumUntil( [30, 30, 30], 62 )
)
// {index: 1, result: 60}
bonus: replace let with var and it works on IE5.5
You could do
var limit = 60;
var array = [30,30,30];
var count = array.reduce(function(prev, curr, index) {
var temp = prev.sum + curr;
if (index != -1) {
if (temp > limit) {
prev.index = index;
} else {
prev.sum = temp;
}
}
return prev;
}, {
sum: 0,
index: -1
});
console.log(count);
What about this : https://jsfiddle.net/rtcgpgk2/1/
var count = 0; //starting index
var arrayToCheck = [20, 30, 40, 20, 50]; //array to check
var condition = 100; //condition to be more than
increment(arrayToCheck, count, condition); //call function
function increment(array, index, conditionalValue) {
var total = 0; //total to add to
for (var i = 0; i < index; i++) { //loop through array up to index
total += array[i]; //add value of array at index to total
}
if (total < conditionalValue) { //if condition is not met
count++; //increment index
increment(arrayToCheck, count, condition); //call function
} else { //otherwise
console.log('Index : ', count) //log what index condition is met
}
}
// define the max outside of the reduce
var max = 20;
var hitIndex;
var count = array.reduce(function(prev, curr, index) {
let r = prev + curr;
// if r is less than max keep adding
if (r < max) {
return r
} else {
// if hitIndex is undefined set it to the current index
hitIndex = hitIndex === undefined ? index : hitIndex;
return prev;
}
});
console.log(count, hitIndex);
This will leave you with the index of the first addition that would exceed the max. You could try index - 1 for the first value that did not exceed it.
You can create a small utility method reduceWhile
// Javascript reduceWhile implementation
function reduceWhile(predicate, reducer, initValue, coll) {
return coll.reduce(function(accumulator, val) {
if (!predicate(accumulator, val)) return accumulator;
return reducer(accumulator, val);
}, initValue)
};
function predicate(accumulator, val) {
return val < 6;
}
function reducer(accumulator, val) {
return accumulator += val;
}
var result = reduceWhile(predicate, reducer, 0, [1, 2, 3, 4, 5, 6, 7])
console.log("result", result);

Fill the array with average of result with left and right values in an array

I have an array like this
var salaries=[30000,undefined,40000,50000,undefined,70000]
here i want fill the undefined values with average of left and right values. like this.
modified_salaries=[30000,35000,40000,50000,60000,700000]
Actually i tried a lot of times and getting confusion to achieve this.
please help me, is there any solution for this type of calculation.
Edit: if the array is
var salaries=[30000,undefined,undefined,40000,50000,undefined,70000]
the result should be:
modified_salaries=[30000,33000,36000,40000,50000,60000,70000]
See the comments inline in the code:
var salaries = [30000, undefined, 40000, 50000, undefined, 70000];
var modified_salaries = []; // Define empty array
for (var i = 0; i < salaries.length; i++) {
if (salaries[i]) {
modified_salaries.push(salaries[i]); // If not undefined, push it as it is
} else {
// Check if prev and next element exists, use 0 otherwise to calculate average.
average = ((salaries[i - 1] || 0) + (salaries[i + 1] || 0)) / 2;
modified_salaries.push(average); // Push average in new array.
}
}
console.log(modified_salaries); // Log new array
Demo
Try like this
var salaries = [30000, undefined, 40000, 50000, undefined, 70000]
var modified_salaries = salaries.map(function(x, i) {
return typeof x === "undefined" ? (salaries[i - 1] || 0 + salaries[i + 1] || 0) / 2 : x;
});
console.log(modified_salaries);
Try like this:
<div></div>
var salaries=[30000,undefined,40000,50000,undefined,70000];
var modified_salaries = [];
for(var i=0; i<salaries.length; i++) {
if(salaries[i] === undefined) {
modified_salaries.push((salaries[i-1] + salaries[i + 1])/2);
} else {
modified_salaries.push(salaries[i])
}
}
$("div").text(modified_salaries)
Jsfiddle
Can be do in this way.
var salaries = [30000, undefined, 40000, 50000, undefined, 70000];
var finalSal = [];
salaries.forEach(function(val, key) {
if (val) {
finalSal.push(val);
} else {
finalSal.push((salaries[key - 1] + salaries[key + 1]) / 2);
}
});

How to rollup data points using underscore.js?

Say I have an array of 6 numeric data points and want to change it to an array of 3 data points where each point is the sum of a 2 of the points
[1,1,1,1,1,1] ~> [2,2,2]
What is the best way to do this with a library like underscore.js
If you wanted to do it in a generic, functional way, then
function allowIndexes(idx) {
return function(item, index) {
return index % idx;
}
}
function sum() {
return _.reduce(_.toArray(arguments)[0], function(result, current) {
return result + current;
}, 0);
}
var isIndexOdd = allowIndexes(2);
var zipped = _.zip(_.reject(data, isIndexOdd), _.filter(data, isIndexOdd));
console.log(_.map(zipped, sum));
# [ 3, 7, 11 ]
But, this will be no where near the performance of
var result = [];
for (var i = 0; i < data.length; i += 2) {
result.push(data[i] + data[i + 1]);
}
console.log(result);
I found one way. I'll gladly accept a better solution.
values = [1,1,1,1,1,1];
reportingPeriod = 2;
var combined = [];
_.inject( values, function ( total, value, index) {
if ((index + 1) % reportingPeriod === 0) {
combined.push(total + value);
return 0;
} else {
return total + value
}
}, 0);
Using lodash, you could do the following:
let arr = _.fill(Array(6), 1);
let reducer = (sum, num) => sum += num;
arr = _.chain(arr).chunk(2).map(arr => arr.reduce(reducer)).value();

Javascript implementation of the inversion-counting with merge-sort algorithm

i am trying to implement the inversion-counting using merge sort algorithm in javascript. I found description and pseudo-code on this site.
My implementation looks like this:
var mergeAndCount, sortAndCount;
/*
the merging routine
#param List1 the first list to be merged
#param List2 the second list to be merged
*/
mergeAndCount = function(List1, List2) {
var count, outputList;
outputList = [];
count = 0;
while (List1.length > 0 || List2.length > 0) {
outputList.push(Math.min(List1[0], List2[0]));
if (List2[0] < List1[0]) {
count += List1.length;
List2.shift();
} else {
List1.shift();
}
}
outputList = outputList.concat(List1.concat(List2));
return {
'count': count,
'list': outputList
};
};
/*
count inversion algorithm
#param List the sequence to be sorted
*/
sortAndCount = function(List) {
var List1, List2, mergeOut, output1, output2;
if (List.length < 2) {
return {
'count': 0,
'list': List
};
} else {
List1 = List.splice(0, List.length / 2);
List2 = List;
output1 = sortAndCount(List1);
output2 = sortAndCount(List2);
mergeOut = mergeAndCount(List1, List2);
return {
'count': output1.count + output2.count + mergeOut.count,
'list': mergeOut.list
};
}
};
I wanted to test it on Jsfiddle here, but it crashes (too much memory used). Somehow it works for the inupt [1, 3, 2] but not for other. I am not sure what is going wrong, if my implementation or the original pseudocode is false.
Error 1 : infinite loop
The while goes on for a very long time when it starts to compare numbers with undefined. If List1.length is 0, the comparison List2[0] < List1[0] will always be false, resulting in List1.shift() which changes nothing.
Replace:
while (List1.length > 0 || List2.length > 0) {
With:
while (List1.length > 0 && List2.length > 0) {
Error 2 : manipulating arrays
You alter the arrays and then use what you expect to be their initial values. At the begining of each function you should copy the arrays (using slice is the fastest way).
Error 3 : ignoring output of sortAndCount
Replace:
mergeOut = mergeAndCount(List1, List2);
With:
mergeOut = mergeAndCount(output1.list, output2.list);
Correct solution:
var mergeAndCount, sortAndCount;
/*
the merging routine
#param List1 the first list to be merged
#param List2 the second list to be merged
*/
mergeAndCount = function(List1, List2) {
List1 = List1.slice();
List2 = List2.slice();
var count = 0;
var outputList = [];
while (List1.length > 0 && List2.length > 0) {
outputList.push(Math.min(List1[0], List2[0]));
if (List2[0] < List1[0]) {
count += List1.length;
List2.shift();
} else {
List1.shift();
}
}
outputList = outputList.concat(List1.concat(List2));
return {
'count': count,
'list': outputList
};
};
/*
count inversion algorithm
#param List the sequence to be sorted
*/
sortAndCount = function(List) {
List = List.slice();
var List1, List2, mergeOut, output1, output2;
if (List.length < 2) {
return {
'count': 0,
'list': List
};
} else {
List1 = List.splice(0, Math.floor(List.length / 2));
List2 = List;
output1 = sortAndCount(List1);
output2 = sortAndCount(List2);
mergeOut = mergeAndCount(output1.list, output2.list);
return {
'count': output1.count + output2.count + mergeOut.count,
'list': mergeOut.list
};
}
};
console.clear();
var r = sortAndCount([1,3,4,2]);
console.log('RESULT',r.list);
DEMO: http://jsbin.com/UgUYocu/2/edit
As pointed out, the problem was || instead of &&. Here's an implementation that seems to work (to make things interesting, it returns a list of inversions instead of simply counting them):
sort_and_count = function(L) {
if (L.length < 2)
return [[], L];
var m = L.length >> 1;
var na = sort_and_count(L.slice(0, m));
var nb = sort_and_count(L.slice(m));
var nc = merge_and_count(na[1], nb[1]);
return [[].concat(na[0], nb[0], nc[0]), nc[1]];
}
merge_and_count = function(a, b) {
var inv = [], c = [];
while(a.length && b.length) {
if(b[0] < a[0]) {
a.forEach(function(x) { inv.push([x, b[0]])});
c.push(b.shift());
} else {
c.push(a.shift());
}
}
return [inv, c.concat(a, b)];
}
nn = sort_and_count([2, 4, 1, 3, 5])
// [[[2,1],[4,1],[4,3]],[1,2,3,4,5]]
For completeness, here's the quadratic algorithm:
inversions = function(L) {
return L.reduce(function(lst, a, n, self) {
return self.slice(n).filter(function(b) {
return b < a;
}).map(function(b) {
return [a, b];
}).concat(lst);
}, []);
}
inversions([2, 4, 1, 3, 5])
// [[4,1],[4,3],[2,1]]

Categories

Resources