From: William Morgan Date: 2005-04-29T06:59:30+09:00 Subject: Re: TIMTOWTDI letter permutation Excerpts from Trans's mail of 28 Apr 2005 (EDT): > Don't leave us hang'n! What's this groovy i.th solution? The traditional solution breaks i into a sum of multiples of the factorials of 1 to n-1 (the "pattern"), then successively pulls out those multiples as indexes into the string. Kinda hard to explain, but here's how you do it: def permutation(s, i) s = s.dup pat = (1 .. s.length).map do |j| r = i % j i /= j r end pat.reverse.map { |j| s.slice!(j).chr }.join end (Note that the first element of the pattern is always 0.) -- William