Power set: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 1,701: | Line 1,701: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program to display a power set, items may be anything (no blanks)*/ |
|||
parse arg set /*let user specify the items. */ |
|||
if set=='' then set='one two three four' /*None specified? Use default*/ |
|||
N=words(set) /*number of items in the list.*/ |
|||
ps='{}' /*start with a null power set.*/ |
|||
do chunk=1 for N /*traipse through the items. */ |
|||
ps=ps combN(N,chunk) /*N items, a CHUNK at a time. */ |
|||
end /*chunk*/ |
|||
w=words(ps) |
|||
do k=1 for w /*show combinations, one/line.*/ |
|||
say right(k,length(w)) word(ps,k) |
|||
end /*k*/ |
|||
exit /*stick a fork in it, we done.*/ |
|||
/*─────────────────────────────────────$COMBN subroutine────────────────*/ |
|||
combN: procedure expose $ set; parse arg x,y; $= |
|||
!.=0; base=x+1; bbase=base-y; ym=y-1; do p=1 for y; !.p=p; end |
|||
do j=1; L= |
|||
do d=1 for y; _=!.d; L=L','word(set,_); end |
|||
$=$ '{'strip(L,'L',",")'}' |
|||
!.y=!.y+1; if !.y==base then if .combU(ym) then leave |
|||
end /*j*/ |
|||
return strip($) /*return with partial powerset*/ |
|||
.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*/ |
|||
return 0</lang> |
|||
'''output''' when using the default input: |
|||
<pre style="overflow:scroll"> |
|||
1 {} |
|||
2 {one} |
|||
3 {two} |
|||
4 {three} |
|||
5 {four} |
|||
6 {one,two} |
|||
7 {one,three} |
|||
8 {one,four} |
|||
9 {two,three} |
|||
10 {two,four} |
|||
11 {three,four} |
|||
12 {one,two,three} |
|||
13 {one,two,four} |
|||
14 {one,three,four} |
|||
15 {two,three,four} |
|||
16 {one,two,three,four} |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |