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