Power set: Difference between revisions

Content added Content deleted
Line 337: Line 337:
</lang>
</lang>


=={{header|AutoHotkey}}==
=={{header|AWK}}==
<lang AWK>cat power_set.awk
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=147 discussion]
#!/usr/local/bin/gawk -f
<lang autohotkey>a = 1,a,-- ; elements separated by commas
StringSplit a, a, `, ; a0 = #elements, a1,a2,... = elements of the set


# User defined function
t = {
function tochar(l,n, r) {
Loop % (1<<a0) { ; generate all 0-1 sequences
while (l) { n--; if (l%2 != 0) r = r sprintf(" %c ",49+n); l = int(l/2) }; return r
x := A_Index-1
Loop % a0
t .= (x>>A_Index-1) & 1 ? a%A_Index% "," : ""
t .= "}`n{" ; new subsets in new lines
}
}

MsgBox % RegExReplace(SubStr(t,1,StrLen(t)-1),",}","}")</lang>
# For each input
{ for (i=0;i<=2^NF-1;i++) if (i == 0) printf("empty\n"); else printf("(%s)\n",tochar(i,NF)) }
</lang>

{{out}}
<pre>
$ gawk -f power_set.awk
1 2 3 4
empty
( 4 )
( 3 )
( 4 3 )
( 2 )
( 4 2 )
( 3 2 )
( 4 3 2 )
( 1 )
( 4 1 )
( 3 1 )
( 4 3 1 )
( 2 1 )
( 4 2 1 )
( 3 2 1 )
( 4 3 2 1 )
</pre>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==