2

EDIT: I will try to be more clear in this question:

I have this array [1,2,3] and I want to generate all permutations like this:

1,2,3 | 1,3,2 | 3,2,1 | 3,1,2 | 2,3,1 | 2,1,3 | 1 | 1,2 | 1,3 | 2 | 2,3 | 2,1 | 3 | 3,1 | 3,2

Do note that we also want single and double digit permutations, which is not addressed in the proposed duplicate.

I tried to adapt Python code to JavaScript:

function my_permutations(lst) {
if (lst.length == 0) {
    return []
}
if (lst.length == 1) {
    return [lst]
}
var l = []
var m;
var remLst;
for (var i = 0; i < lst.length; i++) {
    m = lst[i]
    remLst = lst.slice(0, i).concat(lst.slice(i + 1))
    my_permutations(remLst).forEach(function(element) {
        l.push([m].concat(element))
    });
}
return l
}

console.log(JSON.stringify(my_permutations([1, 2, 3])))

With the new edit in the code I can get all combination of triplet ! But I want more, I want combination with pairs and singletons. How can I do that ?

So not all possible combinations are generated.

How can I fix this?

bosskay972
  • 890
  • 1
  • 6
  • 27
  • 3
    @Stuart that's what I also thought but in this case it's not "give me the solution" but "my solution has a bug". – VLAZ Jun 26 '19 at 10:19
  • 2
    @ZivBen-Or again, this is a "I have code with a problem in it, I want help fixing it", rather than "give me the code". – VLAZ Jun 26 '19 at 10:20
  • 3
    In Hastur's name, people - this is NOT a dupe. OP **already has** code, doesn't need that step. Solving the bug *here* is a completely different solution to the other question... – VLAZ Jun 26 '19 at 10:22
  • @VLAZ OP will probably need to dispute the duplicate and flag for mod attention. I reckon it's the title throwing people off - it's not until you get to the **end** that you see what the 'real' question is. – Lewis Jun 26 '19 at 10:26
  • @Lewis I've already voted for reopening. Other people with reopen vote privileges (not sure how much rep that is) can also vote on this. A mod flag could also help. – VLAZ Jun 26 '19 at 10:27
  • 4
    @VLAZ A mod flag is completely inappropriate here. You shouldn't be recommending that. Lewis flag is going to be declined. – yivi Jun 26 '19 at 11:04
  • I don't know why I'm ban, and I don't understand what's wrong with my question, actually I don't even know why they compare my question with the "original one"(because apparently my question is a duplicate), I want to know how to fix my code first and secondly this is even the same goal the-said topic. If you don't want to answer I'm okay with that I found an alternate solution, even if I always stuck with this code(I don't understand why it doesn't work) but just unban me I have many other questions to ask on this website. If I was unclear, I will try to be clear next time, ask me anytime. – bosskay972 Jun 26 '19 at 15:42
  • 2
    @bosskay972: you're not banned. Someone, or several people, depending on their reputation, thought that this question was a duplicate of another question, and for a while, it was marked as such, which means that it can't receive answers. But as seen in the comments, VLAZ argued that it wasn't a duplicate and convinced enough people to agree that it's now back in normal status. Now, ideally, someone can suggest a fix. – Scott Sauyet Jun 27 '19 at 17:08
  • 1
    @ScottSauyet: they say: "Sorry, we are no longer accepting questions from this account. See the Help Center to learn more.". So, I supposed I am ban, I try to edit a little bit the topic but I don't add a lot informations because the topics is normally understandable. Please If you don't understand something in the post please tell me, but please let me ask other question... – bosskay972 Jun 28 '19 at 15:34
  • Sorry, @bosskay972; I made a bad assumption based on the comment thread above. I don't know anything about banning, but this page may be relevant: https://meta.stackexchange.com/questions/86997 – Scott Sauyet Jun 28 '19 at 18:38
  • When I run the code above (after fixing the undeclared variables, `l`, `m`, and `remlist`), I get the normal result of an `all-permutations-of-this-array* function. But you want something different from that, since you want the set of all permutations of all subsets of your input, or something like that. So the Python you're starting from is not relevant. – Scott Sauyet Jun 28 '19 at 18:48
  • @ScottSauyet how do you fix the undeclared variables ? – bosskay972 Jun 30 '19 at 08:51
  • 1
    @bosskay972: Change `l = []` to `var l = []`, `m = lst[i]` to `var m = lst[i]`, etc. You're already using `var` in the `for` loop; otherwise I would recommend using `let` or, even better, `const`, instead of `var`. Nonetheless, this does not seem to be the algorithm you want. – Scott Sauyet Jul 01 '19 at 02:17
  • @ScottSauyet that's very good ! It's work, not with the combination 1 | 2 | 3 | 2,3 | ... but it partially works ! We have now to find how to get the others combination – bosskay972 Jul 01 '19 at 08:06
  • Right. The algorithm you tried to convert lists the permutations of `n` distinct elements. You want something different. – Scott Sauyet Jul 01 '19 at 11:03
  • Does this answer your question? [Given an array, how to generate all combinations of subset size k?](https://stackoverflow.com/questions/46880094/given-an-array-how-to-generate-all-combinations-of-subset-size-k) – AncientSwordRage Jan 22 '22 at 10:19

1 Answers1

4

Here's a solution

This almost does what you want. The problem is that it contains an empty array :(

function my_permutations(lst) {
  var l = [[]]
  var m;
  var remLst;
  for(var i = 0; i < lst.length; i++) {
    m = lst[i]
    remLst = lst.slice(0, i).concat(lst.slice(i + 1))
    my_permutations(remLst).forEach(function(element){
      l.push([m].concat(element))
    })
  }
  return l
}

console.log(my_permutations([1, 2, 3]))

Here's another way to do it

It's definitely not efficient and uses a lot of new javascript features but I think it's slick.

const rotations = ([l, ...ls], right=[]) =>
  l ? [[l, ...ls, ...right], ...rotations(ls, [...right, l])] : []

const permutations = ([x, ...xs]) =>
  x ? permutations(xs).flatMap((p) => rotations([x, ...p])) : [[]]

const powerset = (xs) =>
  xs.reduce((ys, x) => [...ys, ...ys.map((y) => [x, ...y])], [[]])

const powerPermutations = (xs) =>
  powerset(xs).flatMap(permutations)

console.log(powerPermutations([1, 2, 3]))
Chris Vouga
  • 555
  • 6
  • 8
  • 2
    Thanks a lot for the empty array it's cool with array.shift() I can delete the first element so it's okay for me ! – bosskay972 Jul 02 '19 at 12:05
  • upvoted, if you dont mind answering. you are calling the function recursively for each element , I did not understand why are you doing a concat inside, could you kindly update your answer explaining how the inner loop works – PirateApp Jul 03 '19 at 07:59