4

So I'm working on a project where I have (the equivalent of) 15 boolean variables, with associated "costs" and other attributes.

I want to iterate through every permutation of these 15 boolean variables (e.g. all 0s, all 1s, etc.) so I can perform further analysis.

My result will presumably be an array with all 2^15 = 32k permutations (which is fine). Having each element be a string is probably fine.

I've tried searching for solutions (like this: Permutations in JavaScript?), but those aren't exactly what I'm looking for.

Greatly prefer a JavaScript solution, although I know using something like itertools in Python could perform something similar. As I originally tried to hack something together, I was trying to think of a recursive solution (beats having 15 for-loops), but I could use a bit of help.

Thanks a bunch!

Community
  • 1
  • 1
Guest
  • 43
  • 3
  • 2
    You're not looking for "permutations". If you want all possible combinations, you need the power set of your options. – Bergi Dec 19 '16 at 23:16
  • You seem to want a cartesian product of `[true,false]` with itself 15 times. See [Cartesian product of multiple arrays in JavaScript](http://stackoverflow.com/q/12303989/1529630) – Oriol Dec 20 '16 at 00:22

1 Answers1

0

I wrote a little library that can do this. It uses ES2015 generators.

https://github.com/acarl005/generatorics

const G = require('generatorics')

const states = G.baseN([ true, false ], 15)

// get the next iteration with .next() and so on...
console.log(states.next().value)
console.log(states.next().value)
console.log(states.next().value)

// or use a loop
for (let state of states) {
  console.log(state)
}
Andy Carlson
  • 3,633
  • 24
  • 43