On 22.03.2021 23:14, Tim Menzies wrote:
given an array a[1]=10,a[2]=20,a[3]=30 anyone got an iterator that runs over all subsets. for example:
function show(lst) { print ""; for(i in lst ) print(i,lst[i]) }
forsubs("show", a) ==>
10
20
30
40
10, 20
10,30
20,30
10,20,30
(I suppose the "40" was a typo?)
As for a solution, with GNU awk where I can use bit-operations,
I might implement something like
function forsubs (a, n, m, z, w) {
n=length(a)
m=2^n
for (z=1; z<m; z++) {
w=1
for (y=1; y<m; y*=2) {
if (and(z,y)) printf " %s", a[w]
w++
}
printf "\n"
}
}
BEGIN { a[1]=10 ; a[2]=20 ; a[3]=30 ; forsubs(a) }
to create this output
10
20
10 20
30
10 30
20 30
10 20 30
The function relies on some properties (e.g. contiguous indices in a),
and to define another order of the resulting permutation you need some
tweaks.
Janis
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)