No repeats please 2

In this example below you will see how to do a No repeats please 2 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by f1e2l3i4x5, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright f1e2l3i4x5 ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>No repeats please 2</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/f1e2l3i4x5/no-repeats-please-2-QqQJXe */
function permAlone(str) {
  
//base parametrs
  var length = str.length;
  var arr = str.split('');
  var coded = [];
  var answer = 0;
  
//condition for extreme str length
if (length>1) {
  
//collect all items in coded form
  for (var i=0;i<length;i++) {
  coded[i]=i+1;
  }
  
//console.log(coded);
  
//define first library item
  var library = [];
  var firstStore = coded.slice(0);
  library.push(firstStore);
  
//console.log(library);
  
//FUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONS
//the roll func for algos
  function roll (arr) {
    var arrClone = arr.slice(0);
    var arrClone2 = arr.slice(0);
    var length = arrClone.length;
    var firstItem = arrClone[0];
    arrClone[0] = arrClone[length-1];
    arrClone[1] = firstItem;
    for (var i=2;i<length;i++) {
      arrClone[i] = arrClone2[i-1];
    }
    return arrClone;
  }
  
//the mirror func for algos
  function mirror (arr) {
    var arrClone = arr.slice(0);
    var arrClone2 = arr.slice(0);
    var length = arrClone.length;
    var firstItem = arrClone[0];
    for (var i=1;i<length;i++) {
      arrClone[i] = arrClone2[length-i];
    }
    return arrClone;
  }
  
//the twist func for algos
  function twist (arr) {
    var arrClone = arr.slice(0);
    var arrClone2 = arr.slice(0);
    var arrClone3 = arr.slice(0);
    var length = arrClone.length;
    arrClone[0] = arrClone2[0];
    var circle = arrClone3.splice(1,length-1);
    circle = roll (circle);
    for (var k=0;k<circle.length;k++) {
      arrClone[k+1]=circle[k];
    }
    return arrClone;
  }
  
//factorial func
  function factorial(num) {
    if (num==0) {
      return 1;
    }
    else {
      return num*factorial(num-1);
    }
  }
  
//check library if there is any not unique item
  function libraryCheck (lib, leng) {
  //library uniqueness check
  var checkVault = [];
  var checker = 0;
  var checkerSelf = 0;
  var libraryAnswer = true;
  var libraryAnswer2 = false;
  for (var m=0;m<lib.length;m++) {
    for (var n=0;n<lib.length;n++) {
      for (var l=0;l<leng;l++) {
          if (lib[m][l]==lib[n][l]) {
          checker++;
          }
      }
      if (checker==length) {
        checkerSelf++;
        //console.log(m);
        if (checkerSelf>lib.length) {
          libraryAnswer = false;
        } 
        checker = 0;
      }
      else {
        libraryAnswer2 = true;
        checker = 0;
      }
    }
  }
   
  //console.log(libraryAnswer);
  //console.log(libraryAnswer2);
  //console.log(checkerSelf);
  return libraryAnswer;
  }
  
//build the library with funcs. It is a func only
  function libraryBuilder (lib, length) {
  // permutation algo
  var newArr = [];
  var numTwists = factorial(length-1)/(2*factorial((length-1)-2)); 
  for (var k=0;k<numTwists-1;k++) {
    newArr = twist(lib[k]);
    newArr = newArr.slice(0);
    lib.push(newArr);
  }

  for (var l=0;l<numTwists;l++) {
    var newArr2 = [];
    var newArr2Clone = [];
    newArr2Clone = lib[l].slice(0);
    newArr2.push(newArr2Clone);
    for (var m=1;m<length;m++) {
      newArr2Clone = roll(newArr2[m-1]);
      newArr2Clone.slice(0);
      newArr2.push(newArr2Clone);
    }
    for (var m=0;m<length-1;m++) {
      lib.push(newArr2[m+1]);
    }
  }
  
  var newArr3 = [];
  var beforeMirrorLength = lib.length;
  for (var d=0;d<beforeMirrorLength;d++) {
    newArr3 = mirror(lib[d]);
    newArr3 = newArr3.slice(0);
    lib.push(newArr3);  
  }
return lib;
}
  
function permutationArr(arr) 
{ 
  var permutations = [];   

  function swap(a, b)
  {
    var tmp = arr[a];
    arr[a] = arr[b];
    arr[b] = tmp;
  }

  function generate(n) {
    if (n == 1) {
      var arrClone = arr.slice(0);
      permutations.push(arrClone);
    } else {
      for (var i = 0; i != n; ++i) {
        generate(n - 1);
        swap(n % 2 ? 0 : i, n - 1);
      }
    }
  }

  generate(arr.length);
  return permutations;
}    
  
//FUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONSFUNCTIONS
  
//the actual build of the library

var permut = permutationArr(library[0]);  
library = permut;

  
 console.log(library);
 //console.log(libraryCheck(library)); 
  //console.log(count);

//find unique characters 
  var repeatList = [];
  var matches = str.split('');
  matches = matches.sort();
  matches = matches.join('');
  var regex = /(.)\1{1,}/g;
  repeatList = matches.match(regex);
  console.log(repeatList);
  if (repeatList!==null) {
  for (var b=0;b<repeatList.length;b++) {
    repeatList[b]=repeatList[b].split('');
    repeatList[b]=repeatList[b][0];
  }
  }
  else {
    answer = factorial(length);
  }
  
  console.log(repeatList); 
  
//define coded sets of similar items
  var repeatListNum = [[],[]];
  if (repeatList!==null) {
  for (var y=0;y<repeatList.length;y++) {
    for (var t=0;t<str.length;t++) {
      if (repeatList[y]==str[t]) {
        repeatListNum[y].push(t+1);
      }
    }
  }
  }
  
 console.log(repeatListNum); 
  
//check which library item is to be deleted
  function doubleCheck (lib, tester) {
  
  var trigger = 0;
  var trigger2 = 0;
  var libraryOut = 0;
  
    for (var r=0;r<lib.length;r++) {
      trigger = 0;
      trigger2 = 0;  
      for (var f=0;f<tester.length;f++) {
        if (lib[r]==tester[f]) {
          trigger = 1;
        }
        if (lib[r+1]==tester[f]) {
          trigger2 = 1;
        }
      }
      if (trigger == 1 && trigger2 == 1) {
        libraryOut++;
        trigger = 0;
        trigger2 = 0;
      }
    }
  return libraryOut;
  }

//prepare a list of deleted items from the library
var trial;
var deleteList = [];  
var similar = 0;
  
for (var t=0;t<repeatListNum.length;t++) {
  for (var u=0;u<library.length;u++) {
    trial = doubleCheck (library[u], repeatListNum[t]);
    if (trial>0) {
      for (var f=0;f<deleteList.length;f++) {
        if (deleteList[f]==u) {
          similar++;
        }
      }
      if (similar==0) {
        deleteList.push(u);
      }
      else {
        similar = 0;
      }
    }
  }
}
  
//console.log(deleteList); 
console.log(factorial(str.length));
//console.log(deleteList.length); 
  
//calc the number of valid items
  answer = factorial(length)-deleteList.length;

}
  
//scpecial length case
if (length==1) {
  answer = 1;
}
  
  return answer;
}

console.log(permAlone('zzzzzzzz'));

Comments