Friday, March 10, 2023

Coding Challenge #18

 // Daily Coding Challenge #18
// given an array [ary], and a length [k], iterate through [ary] and find the max value of each index and the next [k]-1 values
// example: k=3; [1,2,3,4,5] -> [3,4,5] (max(1,2,3), max(2,3,4), max(3,4,5))
// k=4; [1,5,1,3,1,2] -> [5,5,3]  (max(1,5,1,3), max(5,1,3,1), max(1,3,1,2))
function calc(ary, k){
    for(var xint = 0, max = ary.length - (k-1);xint < max;xint++)
        console.log(ary.slice(xint,xint+k).sort((a,b)=>{return a<b;})[0]);
}
// part of the challenge was to output these as we iterate through the array, without storing them, explaining the choice of form

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...