Hi Bakul,
For permutations I just use this k program:
$ cat perm.k
p:{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}
perm:{x@p[#x]}
I was thinking it looks like APL and it turns out to be a descendant.
https://en.wikipedia.org/wiki/K_(programming_language)
That pushed me into writing an awk-permutation generator. It's
inefficient as it keeps altering $0 which cascades into re-working $1,
etc., even if that's lazily done. Still, it sufficies for the small
cases I typically need.
$ cat perm
#! /bin/awk -f
function perm(s, f, l, t) {
if (!NF) {
print substr(s, 2)
return
}
l = $0
for (f = 1; f <= NF; f++) {
t = $f
$f = ""; $0 = $0
perm(s " " t)
$0 = l
}
}
{ perm("") }
$
$ ./perm <<<''
$ ./perm <<<1
1
$ ./perm <<<'1 2'
1 2
2 1
$ ./perm <<<'1 2 3'
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
$
$ time ./perm <<<'1 2 3 4 5 6 7 8 9' | wc -l
362880
real 0m5.421s
user 0m5.406s
sys 0m0.082s
$
$ dc <<<'9 8*7*6*5*4*3*2*p'
362880
$
--
Cheers, Ralph.