Created
October 5, 2016 16:44
-
-
Save holgergp/328b1fc30e1a25aa713781f8444abd5c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 zip(array1, array2) { | |
return array1.map(function (e, i) { | |
return [e, array2[i]]; | |
}); | |
} | |
function diff (a ,b) { | |
var zippedArray = zip(a,b); | |
return zippedArray.reduce(function(previousValue ,currentValue){ | |
return previousValue + (currentValue[0] - currentValue[1]); | |
},0); | |
} | |
//returns array of subsequences | |
function findAllSubsequences(array, elemsToRemove){ | |
var arrayOfSubseqence = []; | |
for (var i=0; i<array.length ;i++ ) { | |
arrayOfSubseqence.push( array.splice(i,1 )); | |
} | |
console.log(arrayOfSubseqence) | |
return arrayOfSubseqence; | |
} | |
function closestSequence2(a, b) { | |
//Compare Lenghts | |
//If a.length > b.length | |
if(a.length >= b.length) { | |
return diff(a,b) | |
} | |
//b is larger than a | |
var numberToRemove = b.length - a.length; | |
var allSubsequences = findAllSubsequences(b, 2); | |
var allSubsequencesWithWeight = allSubsequences.map(function (elem) { | |
return [elem, diff(elem)] | |
}); | |
return allSubsequencesWithWeight.reduce (function(previousValue, currentValue) { | |
return previousValue[1] < currentValue[1] ? previousValue : currentValue; | |
},0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment