dankogai / js-combinatorics

power set, combination, permutation and more in JavaScript
MIT License
742 stars 97 forks source link

baseN should return [[]] rather than [] for inputs of length 0 #32

Closed dbkaplun closed 6 years ago

dbkaplun commented 8 years ago

I found this when debugging a corner case in the solve24 module and I was able to trace it down to a call to baseN([], n) which results in [] rather than the expected [[]]. Here's an example to help explain: let's say we want to generate all possible ways to perform some set ops of binary operations (like +,-,*,/) on ops.length+1 numbers. To do so, one might call baseN(ops, ops.length) to find all ops.length-size combinations of ops for applying to the ops.length+1 numbers. However, in the case that ops is empty, there is still exactly one possible combination of ops, namely itself. The current, incorrect behavior of baseN([], n) returning [] should be changed to return [[]] instead.