kcsarath
BAN USERMy solution to the problem in Javascript.
var windowMax = 0;
var maxFor = 0;
var findMaxInWindow = function (windowSize, value) {
// console.log ("Finding the max in the range with the value" , value);
if (value > windowMax) {
windowMax = value;
maxFor = 1;
} else {
maxFor ++;
}
if (maxFor > windowSize) {
windowMax = value;
maxFor = 1;
}
}
var main = function ( ){
var windowSize = 3;
var stream = [1, 4, 2, 7, 9, 15, 11, 26, 96, 15, 3, 56, 25];
var numOfValues = stream.length;
for (var i = 0 ; i < numOfValues; i++) {
var num = stream[i];
//console.log (num);
findMaxInWindow (windowSize, num);
//console.log ("The window max is " + windowMax + " maxFor " + maxFor);
console.log (i + " ) " + stream[i] +" - MAX : " + windowMax);
}
}
main();
The output
0 ) 1 - MAX : 1
1 ) 4 - MAX : 4
2 ) 2 - MAX : 4
3 ) 7 - MAX : 7
4 ) 9 - MAX : 9
5 ) 15 - MAX : 15
6 ) 11 - MAX : 15
7 ) 26 - MAX : 26
8 ) 96 - MAX : 96
9 ) 15 - MAX : 96
10 ) 3 - MAX : 96
11 ) 56 - MAX : 56
12 ) 25 - MAX : 56
When a data file is split into blocks and stored in the HDFS, the physical file might not match a record boundary and some portion of the record could be in different locations.
To avoid this, hadoop provides some thing called a logical input split. This ensures that the map function always gets a complete record with out partial data.
So an input split is a logical representation of a complete record.
Thanks Shashank for pointing the issue with 1, 16. The traverse function needed the following fix.
for ( ; ; c += cSign) {
if (c == destCoord.col + cSign) {break;}
path += " " + array[destCoord.row][c] + " ";
}
I do not see a problem with 1, 17 and 2, 21 !!
Will post a java version in some time.
Wrote this up in Javascript, please provide your feedback :
var buildArray = function (length, breadth) {
var array = new Array();
for (var i = 0; i < length; i++) {
array[i] = [
];
}
return array;
}
var initialize = function (array) {
var count = 1;
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
array[i][j] = count++;
}
}
return array;
}
var displayArray = function (array) {
array.forEach(function (entry) {
console.log(entry);
});
}
var findPath = function (array, src, dest) {
console.log('finding path from src, ' + src + ' to destination ' + dest);
var srcCoord = getCoordinates(src);
var destCoord = getCoordinates(dest);
path = traverse(array, srcCoord, destCoord);
return path;
}
var traverse = function (array, srcCoord, destCoord ) {
var path = "";
var rSign = (destCoord.row > srcCoord.row) ? +1 : -1;
var r = srcCoord.row;
for ( ; ;r += rSign) {
path += " " + array[r][srcCoord.col] + " ";
if (r == destCoord.row) break;
}
var cSign = (destCoord.col > srcCoord.col) ? +1 : -1;
var c = srcCoord.col + cSign;
for ( ; ; c += cSign) {
if (c == destCoord.col + cSign) {break;}
path += " " + array[destCoord.row][c] + " ";
}
return path;
}
var getCoordinates = function (value) {
var row = Math.floor((value - 1 )/ 5); // console.log ( "Row = " + row);
var rowStart = (row * 5) + 1; // console.log ("Row Start = " + rowStart);
var col = value - rowStart; // console.log ("Col = " + col);
return {
'row': row,
'col': col
};
}
var main = function () {
var array = buildArray(5, 5);
console.log(array);
array = initialize(array);
displayArray(array);
var endPoints = [
{
src: 1,
dest: 22
},
{ src: 18, dest: 7},
{
src: 4,
dest: 17
},
{
src: 5,
dest: 18
},
{
src: 9,
dest: 13
},
{
src: 20,
dest: 23
}
];
endPoints.forEach(function (entry) {
var path = findPath(array, entry.src, entry.dest);
console.log ("The path is " + path);
});
}
main();
Hi Alpha,
- kcsarath July 04, 2015Thanks for pointing out, I did realise it myself too but feels good that some one is looking at the solution and providing feedback. Thanks for taking the time :)
thanks,
Sarath.