I recently took a coding test for a promotion at work. This was one of the tasks I really struggled with and was wondering what is the best way to do this. I used a load of
First allCom
will return all combination of the 4 number (total 24 combinations)
Then for the 24 array (combinations) call .forEach
go through each array check if it is a valid time format. If it is valid time format then calculate the time value with
If the time is AB:CD then the value:
A = A * 10 hours = A * 10 * 3600s = A * 36000s
B = B * 1 hour = B * 3600s
C = C * 10s
D = D
Total value = A*36000 + B*3600 + C*10 + D
Now you got the value of the current array, compare with the saved Max, replace the max if this value is bigger.
At the end of the loop determine if a max found or it is not valid.
generate(6, 5, 2, 0);
generate(3, 9, 5, 0);
generate(7, 6, 3, 8);
generate(1, 7, 2, 7);
generate(1, 1, 1, 2);
// return all combination of 4 number (24 combination total)
function allCom(inputArray) {
var result = inputArray.reduce(function permute(res, item, key, arr) {
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(function(perm) {
return [item].concat(perm);
}) || item);
}, []);
return result;
}
// core function to determine the max comb
function generate(A, B, C, D) {
let input = [A, B, C, D];
let allComb = allCom(input);
let max = '';
let maxA = [];
allComb.forEach(function(comb, index, arr) {
if (validCom(comb)) {
let temp = calValue(comb);
maxA = temp > max ? comb : maxA;
max = temp > max ? temp : max;
}
if (index == allComb.length - 1) {
if (max) {
return console.log('For ' + JSON.stringify(input) + ' found max comb: ' + maxA[0] + maxA[1] + ':' + maxA[2] + maxA[3]);
}
return console.log('Sorry ' + JSON.stringify(input) + ' is not valid');
}
});
}
// check if this array is valid time format, ex [1,2,9,0] false, [2,2,5,5] true
function validCom(ar) {
if (ar[0] <= 2 && ((ar[0] == 2 && ar[1] < 4) || (ar[0] != 2 && ar[1] <= 9)) && ar[2] <= 5 && ar[3] <= 9) {
return true;
}
return false;
}
// calculate the total value of this comb array
function calValue(ar) {
return +ar[0] * 36000 + +ar[1] * 3600 + +ar[2] * 10 + +ar[0];
}
$('button').on('click', function(e) {
let inp = $('select');
generate(inp[0].value, inp[1].value, inp[2].value, inp[3].value);
});
var s = $('');
for(i=0;i<10;i++) {
$('', {value: i, text: i}).appendTo(s);
}
s.clone().appendTo('#myform');
s.clone().appendTo('#myform');
s.clone().appendTo('#myform');
s.clone().appendTo('#myform');
/* --------------- Start Speed Test --------------------- */
let startTime = Math.floor(Date.now());
let times = 10000; //how many generate call you want?
let timesHolder = times;
while (times--) {
let A = randNum();
let B = randNum();
let C = randNum();
let D = randNum();
generate(A, B, C, D);
if (times == 0) {
let totalTime = Math.floor(Date.now()) - startTime;
let msg = timesHolder + ' Call Finished Within -> ' + totalTime + ' ms <-';
console.log(msg);
alert(msg);
}
}
function randNum() {
return Math.floor(Math.random() * (9 - 0 + 1)) + 0;
}
/* --------------- END Speed Test --------------------- */
/* --------------- Start Speed Test --------------------- */
let startTime = Math.floor(Date.now());
let times = 10000; //how many generate call you want?
let timesHolder = times;
while (times--) {
let A = randNum();
let B = randNum();
let C = randNum();
let D = randNum();
generate(A, B, C, D);
if (times == 0) {
let totalTime = Math.floor(Date.now()) - startTime;
let msg = timesHolder + ' Call Finished Within -> ' + totalTime + ' ms <-';
console.log(msg);
alert(msg);
}
}
function randNum() {
return Math.floor(Math.random() * (9 - 0 + 1)) + 0;
}
/* --------------- END Speed Test --------------------- */
// return all combination of 4 number (24 combination total)
function allCom(inputArray) {
var result = inputArray.reduce(function permute(res, item, key, arr) {
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(function(perm) {
return [item].concat(perm);
}) || item);
}, []);
return result;
}
// core function to determine the max comb
function generate(A, B, C, D) {
let input = [A, B, C, D];
let allComb = allCom(input);
let max = '';
let maxA = [];
allComb.forEach(function(comb, index, arr) {
if (validCom(comb)) {
let temp = calValue(comb);
maxA = temp > max ? comb : maxA;
max = temp > max ? temp : max;
}
if (index == allComb.length - 1) {
if (max) {
return 'For ' + JSON.stringify(input) + ' found max comb: ' + maxA[0] + maxA[1] + ':' + maxA[2] + maxA[3];
}
return 'Sorry ' + JSON.stringify(input) + ' is not valid';
}
});
}
// check if this array is valid time format, ex [1,2,9,0] false, [2,2,5,5] true
function validCom(ar) {
if (ar[0] <= 2 && ((ar[0] == 2 && ar[1] < 4) || (ar[0] != 2 && ar[1] <= 9)) && ar[2] <= 5 && ar[3] <= 9) {
return true;
}
return false;
}
// calculate the total value of this comb array
function calValue(ar) {
return +ar[0] * 36000 + +ar[1] * 3600 + +ar[2] * 10 + +ar[0];
}