These are chat archives for camperbot/bonfire-no-repeats-please

26th
Feb 2016
Marko Nikic
@MarkoN95
Feb 26 2016 12:31

@/all Guys I need some serious help here. I have (technically) solved the "No repeats please" Bonfire with the following solution:

function permAlone(str) {
  var input = str.split("");
  var permutations = [];

  function generate(n, arr) {
    if(n == 1) {
      permutations.push(arr.join(""));
      return;
    }
    for(var i = 0; i < n; i++) {
      generate(n - 1, arr);

      if(n % 2 === 0) {
        swap(arr, i, n - 1);
      }
      else {
        swap(arr, 0, n - 1);
      }
    }
  } 

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

  function filterDuplicates(arr) {

    var after = arr.filter(function(item, pos, self) {
      for(var i = 0; i < item.length; i++) {
        if(item.charAt(i) == item.charAt(i+1)) {
          return false;
        }
      }
      return true;
    });
    return after.length;
  }

  generate(input.length, input);

  return filterDuplicates(permutations);
}

// these are all the test cases that freecodecamp checks
console.log(typeof permAlone('aab') == "number"); //prints: true
console.log(permAlone('aab')); // prints: 2
console.log(permAlone('aaa')); // prints: 0
console.log(permAlone('aabb')); // prints: 8
console.log(permAlone('abdcefa')); // prints: 3600
console.log(permAlone('abfdefa')); // prints: 2640
console.log(permAlone('zzzzzzzz')); // prints: 0

The solution works fine in the firebug console. And I don't think that too much recursion is the problem since I can also run this code with a string of length 10 which yields more than 3.5 million permutations. It takes a couple of seconds though.

But when I submit the code on the challenge FreeCodeCamp always crashes and I have to stop the script from executing. Why is that??? Its frustrating having a solution but beeing unable to submit it!

EDIT: It suddenly worked and I was finally able to complete the challenge altough I havent changed my code?? what could have been the problem for that? FreeCodeCamp suddenly accepting my solution and not crashing?

CamperBot
@camperbot
Feb 26 2016 12:31
type bonfire name to get some info on that bonfire. And check HelpBonfires chatroom