Power set: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 2,503: Line 2,503:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program to display a power set, items may be anything (no blanks)*/
<lang rexx>/*REXX pgm displays a power set, items may be anything (but can't have blanks)*/
parse arg S /*let user specify the set. */
parse arg S /*allow the user specify optional set. */
if S='' then S='one two three four' /*None specified? Use default*/
if S='' then S='one two three four' /*None specified? Then use the default*/
N=words(S) /*number of items in the list.*/
N=words(S) /*the number of items in the list (set)*/
ps='{}' /*start with a null power set.*/
@='{}' /*start process with a null power set. */
do chunk=1 for N /*traipse through the items. */
do chunk=1 for N /*traipse through the items in the set.*/
ps=ps combN(N,chunk) /*N items, a CHUNK at a time. */
@=@ combN(N,chunk) /*take N items, a CHUNK at a time. */
end /*chunk*/
end /*chunk*/
w=length(2**N) /*the number of items in the power set.*/
w=words(ps)
do k=1 for w /*show combinations, one/line.*/
do k=1 for words(@) /* [↓] show combinations, one per line*/
say right(k,length(w)) word(ps,k)
say right(k,w) word(@,k) /*display a single combination to term.*/
end /*k*/
end /*k*/
exit /*stick a fork in it, we done.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────────────$COMBN subroutine────────────────*/
combN: procedure expose $ S; parse arg x,y; $=
combN: procedure expose S; parse arg x,y; base=x+1; bbase=base-y; !.=0
!.=0; base=x+1; bbase=base-y; ym=y-1; do p=1 for y; !.p=p; end
do p=1 for y; !.p=p; end /*p*/
$=
do j=1; L=
do d=1 for y; _=!.d; L=L','word(S,_); end
do j=1; L=
$=$ '{'strip(L,'L',",")'}'
do d=1 for y; L=L','word(S,!.d)
!.y=!.y+1; if !.y==base then if .combU(ym) then leave
end /*d*/
$=$ '{'strip(L,'L',",")'}'
!.y=!.y+1; if !.y==base then if .combU(y-1) then leave
end /*j*/
end /*j*/
return strip($) /*return with partial powerset*/
return strip($) /*return with a partial powerset chunk.*/
/*────────────────────────────────────────────────────────────────────────────*/

.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1; p=!.d
p=!.d; do u=d to y; !.u=p+1
do u=d to y; !.u=p+1; if !.u==bbase+u then return .combU(u-1)
if !.u==bbase+u then return .combU(u-1)
p=!.u
p=!.u
end /*u*/
end /*u*/
return 0</lang>
return 0</lang>
{{out}} when using the default input:
{{out}} when using the default input:
<pre>
<pre style="overflow:scroll">
1 {}
1 {}
2 {one}
2 {one}