Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/611b4af88a296915cec8cab57b9ee4d8 to your computer and use it in GitHub Desktop.
Save jrichardsz/611b4af88a296915cec8cab57b9ee4d8 to your computer and use it in GitHub Desktop.
permutation combination snippets

https://www.jsplayground.dev/

function checkSimilarity(baseArray, sampleArray) {
  var length=0;
  for(var i=0; i<baseArray.length; i++){
    var subArray = baseArray.slice(0, baseArray.length-i);
    var contains = isContained(sampleArray, subArray);
    if(contains){
      length = baseArray.length -i;
      console.log(subArray, contains, length);
      break
    };
  }
  var score = (length*100)/baseArray.length;
  console.log(score)
  console.log(sampleArray.length)
  console.log("----")
  
  if(score==0 && sampleArray.length>2){    
    for(let ii=sampleArray.length; ii>=2; ii--){
      var iterationLength = ii;
      for(let i=0; i<sampleArray.length; i++){
        console.log(i, i+iterationLength)
        if(i+iterationLength>sampleArray.length)continue;       
        console.log(sampleArray.slice(i, i+iterationLength))
      }      
    }
    // const containsAll = sampleArray.every(element => {
    //   return baseArray.includes(element);
    // });   
    // console.log(containsAll);
  }
}  

function isContained(b, a, bIndex) {
  if (!a.length) return true;
  bIndex ??= b.findIndex(el => el === a[0]);
  return a[0] !== b[bIndex] ? false : isContained(b.slice(bIndex+1), a.slice(1), 0);
}

const baseArray = [1,2,3,4,5]
checkSimilarity(baseArray, [2,3,4,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment