TIMTOWTDI letter permutation

...

Notice how the first 6 start with 'a', the next 6 with 'b', etc.

Within each block of 6, you have 2 permutations starting with each of
the remaining letters. You can generalise this pattern to directly
calculate the ith permutation of n letters, given n and i. Your
generator then becomes

1..upto(n.factorial) {|i| puts permutation(n, i)}

with no recursion at all.

martin

What's the practical applicatin of being able to pick-off the i.th
permutation of an array?

Peter J. Fitzgibbons
Applications Manager
Lakewood Homes - "The American Dream Builder"(r)
Peter.Fitzgibbons@Lakewoodhomes.net
(847) 884-8800

···

Endy Tjahjono <endy_c@deadspam.com> wrote:

One I can think of offhand is an easy way to generate uniformly distributed
shuffles without repeats.

martin

···

Peter Fitzgibbons <Peter.Fitzgibbons@lakewoodhomes.net> wrote:

What's the practical applicatin of being able to pick-off the i.th
permutation of an array?