rotate the last entries of the array first

e.g. for an input of ("abc", 2)
aa, ab, ac, ba, bb, bc, ca, cb, cc

This groups the permutations by common prefixes.
This commit is contained in:
Orangenschalen 2017-12-02 14:47:36 +01:00
parent 8695552fe6
commit 444f11161a

View file

@ -87,7 +87,7 @@ export function* permutationsWithReplacement(arr, n) {
for (let _ of range(Math.pow(len, n))) {
yield counters.map(i=>arr[i])
for (let i of range(counters.length)) {
if (index.mod(Math.pow(len, i)) === 0)
if (index.mod(Math.pow(len, counters.length - 1 - i)) === 0)
counters[i] = (counters[i] + 1).mod(len)
}
index++