generate the list of all possible sequences that could be generated for the given input selection from 1 to 5

I've been dabbling with some fun challenges and i came across this one that's a little frustrating to wrap my head around. Can anyone point me in the right direction?

I have a list:

var values = ['blue', 'green', 'red', 'yellow', 'purple'];

From that list I want to return all the possible sequences for a given input. I've tried something like this:

function combinations(number) {
  const values = ['blue', 'green', 'red', 'yellow', 'purple'];
  let arr = values.slice(0, number);

  if (arr.length === 1) {
    result = [...arr];
    console.log(result)
  }

  var result = arr.reduce((acc, v, i) => acc.concat(arr.slice(i + 1).map(w => v + ' ' + w)),[]);


  console.log(result);
}

combinations(2);

but the result only returns ['blue', 'green'] where it should return ['blue', 'green', 'bluegreen', 'greenblue']. The solution should return at least 1 value per sequence and of course no duplicates

can someone help please!! I'm stuck!

3 answers

  • answered 2019-09-15 19:41 Chetan Prakash

    var permArr = [],
          usedChars = [];
          const values = ['blue', 'green', 'red', 'yellow', 'purple'];
    
        function permute(number, temp) {
    
          let input = temp ? temp : values.slice(0, number);
    
          var i, ch;
          for (i = 0; i < input.length; i++) {
            ch = input.splice(i, 1)[0];
            usedChars.push(ch);
            // if (input.length == 0) {
              permArr.push(usedChars.slice());
            // }
            temp = input;
            permute(number, input);
            input.splice(i, 0, ch);
            usedChars.pop();
          }
          return permArr  
        };
    
        var perm = permute(5);
        console.log(perm.length)
        console.log(perm);
    

  • answered 2019-09-15 20:38 zhuber

    Here is a working example - first you find all combinations and for each combinations you calculate its permutations (so that for ["blue", "green"] you also include ["green", "blue"] and so on).

    var values = ['blue', 'green', 'red', 'yellow', 'purple'];
    
    function perms(items) {
      let ret = [];
    
      for (let i = 0; i < items.length; i++) {
        let rest = perms(items.slice(0, i).concat(items.slice(i + 1)));
    
        if (!rest.length) {
          ret.push([items[i]])
        } else {
          for(let j = 0; j < rest.length; j = j + 1) {
            ret.push([items[i]].concat(rest[j]))
          }
        }
      }
    
      return ret;
    }
    
    function combinations(n) {
      let result = [];
      let combine = (items, other = []) => {
        for (var i = 0; i < items.length; i++) {
          let permutations = perms([...other, items[i]]);
          result.push(...permutations);
          
          combine(items.slice(i + 1), [...other, items[i]]);
        }
      }
      combine(values.slice(0, n));
    
      return result;
    }
    
    console.log(combinations(2));

  • answered 2019-09-15 21:13 A. Meshu

    "generate the list of all possible sequences that could be generated for the given input selection from 1 to 5"

    This is how you can do that:

    var values = ['blue', 'green', 'red', 'yellow', 'purple'];
    function combinations() {
      const values = ['blue', 'green', 'red', 'yellow', 'purple'];
      let arr = values.slice(0);
      var result = arr.reduce((acc, v, i) => acc.concat(arr.slice(i+1).map(w => v + ' ' + w)),[]); // this will reduce the duplicated including the doubles (i.e. blue-blue)
      var result1 = arr.reduce((acc, v, i) => acc.concat(arr.slice(i).map(w => v + ' ' + w)),[]); // this will reduce just the duplicated (i.e. blue-green and green blue are the same here)
      var result2 = arr.reduce((acc, v, i) => acc.concat(arr.slice(0).map(w => v + ' ' + w)),[]); // this will show all possibilities  
      console.log(result);
      console.log(result1);
      console.log(result2);
    }
    combinations();

    Is that what you mean?