You have the important Mission to detect mines in the path of your team soldiers, so They can cross in one piece through the enemy territory,
How can You detect mines? as You walk very carefully with the detector, pay atention to anomalies on the soil:
the data you receive:
- Vibrance (V): a number between 0 and 5, represents the noise of your detector
- Visual anomaly (A): a footprint you could identify as a potential mine, represented as a character "*" = 5 , "#" = 4, "X" = 3, "(" = 2, "O" = 1
Not all the footprints will be mines, You will know if It's a mine if your detector gets a reading level 7,
that's determined by a combination of vibrance/footprint EX:
V:2 + A:"#" 4 , Not a mine
V:4 + A:"0" 1 , Not a mine
V:5 + A:"#" 4 , It's a mine
V:5 + A:"0" 1 , Not a mine
You will receive an array of data:
[[V,A],[V,A],[V,A]]
and You will return an array containing the position in the array where a mine is located EX:
[0,2,7, ... etc] .. etc
Good Luck!
function mineDetectionByRadar(detectorData = []){
let result = [];
const anomalies = {
"0": 1,
"(": 2,
"X": 3,
"#": 4,
"*": 5
};
for(let x = 0; x < detectorData.length; x++){
const zone = detectorData[x];
const check = {};
check.pos = x;
check.mine = false
if((zone[0] + anomalies[zone[1]]) >= 7){
check.mine = true;
result.push(x);
}
}
return result;
}
const chai = require("chai");
const assert = chai.assert;
function randomDataGenerator(){
let data = [];
for(let x = 0; x < 100; x++){
const arrayLength = Math.floor(Math.random() * 20)
const testData = Array.from({length: arrayLength}).map(() => []);
const randomInput = testData.map(array => {
const vibration = Math.floor(Math.random() * (5 - 1 + 1)) + 1;
const anomalies = ["0","(","X","#","*"];
const anomalyIndex = Math.floor(Math.random() * anomalies.length);
array.push(vibration);
array.push(anomalies[anomalyIndex]);
return array;
});
data.push(randomInput);
}
return data;
}
describe('Mine Detection by Radar', () => {
it('Basic Tests', () => {
assert.deepEqual(
mineDetectionByRadar([[2,"X"],[4,"X"],[5,"("],[2,"*"],[1,"*"]]),
[ 1, 2, 3 ]
);
assert.deepEqual(
mineDetectionByRadar([[5,"X"],[1,"*"],[2,"("],[3,"#"],[5,"*"],[3,"0"],[5,"#"]]),
[ 0, 3, 4, 6 ]
);
assert.deepEqual(
mineDetectionByRadar([[4, "X"], [5, "#"], [1, "0"], [2, "X"], [3, "0"], [4, "*"], [5, "#"], [1, "("], [2, "X"], [3, "#"], [4, "*"], [5, "("], [1, "0"], [2, "X"], [3, "#"], [4, "*"], [5, "X"]]),
[ 0, 1, 5, 6, 9, 10, 11, 14, 15, 16]
);
assert.deepEqual(
mineDetectionByRadar([[2, "X"], [3, "#"], [4, "0"], [2, "#"], [5, "("], [3, "0"], [1, "*"]]),
[ 1, 4 ]
);
assert.deepEqual(
mineDetectionByRadar([[5, "#"], [1, "X"], [2, "0"], [3, "("], [4, "#"], [5, "X"], [1, "*"], [2, "0"], [3, "("], [4, "X"], [5, "#"]]),
[ 0, 4, 5, 9, 10 ]
);
assert.deepEqual(
mineDetectionByRadar([[4, "#"], [5, "X"], [1, "0"], [2, "X"], [3, "0"], [4, "*"], [5, "#"]]),
[ 0, 1, 5, 6 ]
);
assert.deepEqual(
mineDetectionByRadar([[3, "#"], [4, "X"], [5, "#"], [1, "0"], [2, "X"], [3, "0"], [4, "*"], [5, "#"], [1, "("], [2, "X"], [3, "#"], [4, "*"], [5, "("], [1, "0"], [2, "X"], [3, "#"]]),
[ 0, 1, 2, 6, 7, 10, 11, 12, 15]
);
});
it('Random Tests', async() => {
const randomData = randomDataGenerator();
const randomResults = randomData.map(data => mineDetectionByRadar(data));
await randomData.forEach((data,index) => {
assert.deepEqual(
mineDetectionByRadar(data),
randomResults[index]
);
});
});
});