andrei.hetman
BAN USERfunction printMultiplicationTable(): void {
for (let x=1; x<13; x++) {
const arr = [];
for (let y=1; y<13; y++) {
arr.push(x*y);
}
console.log(arr.toString());
}
}
/** Given a magic number x and a list of numbers, attempt to use addition and subtraction
* to determine if the magic number can be found from the list
**/
function magicNumberSolver(x, list, total: number = 0, process: string = "") {
if (list.length === 0 && total === x) {
console.log("TRUE for x: ", x, " using: ", process);
return true;
} else if (list.length !== 0) {
let num = list.shift();
magicNumberSolver(x, list, total ? total + num : num, process.length? process + "+" + num : num.toString());
magicNumberSolver(x, list, total ? total - num : num, process.length? process + "-" + num : num.toString());
} else {
// console.log("There is no way to get " + x + " from " + process);
return false;
}
}
//test magicNumberSovler()
magicNumberSolver(10, [1,2]);
magicNumberSolver(2, [1,2,3,4]);
magicNumberSolver(0, []);
magicNumberSolver(1, []);
magicNumberSolver(1, [1]);
magicNumberSolver(0, [1]);
I would agree that having it sorted is the proper approach, however by calling
would require loading the array into memory, which the question states cannot be done. You would have to do an external merge sort using small chunks of the entire array. I think you're missing a critical component of the question...
- andrei.hetman July 20, 2018