Created
May 28, 2016 17:04
-
-
Save jacksongabbard/212d83e055b1c43dd83902eed46125a7 to your computer and use it in GitHub Desktop.
Gist copy of https://github.com/jacksongabbard/TheUnqualifiedEngineer/blob/master/Best-Fitting-Subrange.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findBestFittingSubrange( | |
original, replacement) { | |
var ii; | |
var jj; | |
var distance; | |
var distanceSum; | |
var smallestDistanceSum = Infinity; | |
var smallestDistanceSumIndex; | |
// 0 1 2 3 4 5 6 | |
// [ 0, 1, 2, 3, 4, 5, 6 ] | |
// [ 4, 5, 6 ] | |
// 7 - 3 = 4 | |
// Need one more than that to make sure we | |
// compare the last set of digits | |
for (ii = 0; ii <= original.length - replacement.length; ii++) { | |
distanceSum = 0; | |
console.log('Logging for pass #' + ii); | |
for (jj=0; jj<replacement.length; jj++) { | |
distance = | |
Math.abs(original[ii + jj] - replacement[jj]); | |
console.log( | |
' ' + original[ii + jj] + ' is ' + distance + | |
' off from ' + replacement[jj] | |
); | |
distanceSum += distance; | |
} | |
console.log( | |
' The total distance on this pass is ' + distanceSum | |
); | |
if (distanceSum < smallestDistanceSum) { | |
console.log(' >>> Found a new low with that.'); | |
smallestDistanceSum = distanceSum; | |
smallestDistanceSumIndex = ii; | |
} | |
} | |
return smallestDistanceSumIndex; | |
} | |
console.log('---Test Run---'); | |
console.log( | |
findBestFittingSubrange( | |
[2,5,9,1,-3,40,2,19], | |
[10,-3,39] | |
) | |
); | |
console.log('---Test Run---'); | |
console.log( | |
findBestFittingSubrange( | |
[2,5,9,1,-3,10,-3,39], | |
[10,-3,39] | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment