Power set: Difference between revisions

686 bytes removed ,  2 months ago
m
 
(5 intermediate revisions by 3 users not shown)
Line 3,644:
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program displays a power set; items may be anything (but can't have blanks).*/
parse arg S Parse Arg text /*allow the user specify optional set. */
ifIf Stext='' Then then S= 'one two three four' /*Not specified? Then use the default.*/
text='one two three four'
@= '{}' /*start process with a null power set. */
n=words(text)
N= words(S); do chunk=1 for N /*traipse through the items in the set.*/
psi=0
@=@ combN(N, chunk) /*take N items, a CHUNK at a time. */
Do k=0 To n end /* loops from 0 to n elements of a set /*chunk*/
w= length cc=comb(2**Nn,k) /* returns /*the numbercombinations of items1 inthrough thek power set.*/
Do while do k=1 for wordspos(@'/',cc) >0 /* [↓]as long showas combinations,there is a 1combination per line.*/
Parse Var cc elist '/' cc /* get i from comb's result string */
say right(k, w) word(@, k) /*display a single combination to term.*/
psl='' end /*k initialize the list of words */
exit 0 psi=psi+1 /* index of this set /*stick a fork in it, we're all done. */
Do While elist<>'' /* loop through elements end /*d*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
combN: procedure expose S; parse argvar x,y;elist e elist /* base=get xan +element 1;(a digit) bbase= base - y*/
psl=psl','word(text,e) /* add corresponding test word to set */
!.= 0
End
do p=1 for y; !.p= p
psl=substr(psl,2) /* get rid of leading comma end /*p*/
Say right(psi,2) '{'psl'}' /* show this element of the power set */
$= /* [↓] build powerset*/
End
do j=1; L=
End
do d=1 for y; L= L','word(S, !.d)
Exit
end /*d*/
comb: Procedure
$= $ '{'strip(L, "L", ',')"}"; !.y= !.y + 1
/***********************************************************************
if !.y==base then if .combU(y - 1) then leave
* Returns the combinations of size digits out of things digits
end /*j*/
* e.g. comb(4,2) -> ' 1 2/1 3/1 4/2 3/2 4/3 4/'
return strip($) /*return with a partial power set chunk*/
1 2/ do p=1 3/ for y;1 4/ 2 3/ 2 4/ 3 !.p=4 p/
/*──────────────────────────────────────────────────────────────────────────────────────*/
***********************************************************************/
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1
Parse Arg things,size
p= !.d
n=2**things-1
do u=d to y; !.u= p + 1; if !.u==bbase+u then return .combU(u-1)
list=''
p= !.u /* ↑ */
Do u=1 To n
end /*u*/ /*recurse──►───┘ */
co=combinations(u)
return 0</syntaxhighlight>
If co>'' Then
list=list '/' combinations(u)
End
Return substr(space(list),2) '/' /* remove leading / */
 
combinations: Procedure Expose things size
Parse Arg u
nc=0
bu=x2b(d2x(u))
bu1=space(translate(bu,' ',0),0)
If length(bu1)=size Then Do
ub=reverse(bu)
res=''
Do i=1 To things
!.c= 0i
If substr(ub,i,1)=1 Then res=res i
End
Return res
End
Else
Return return 0''</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 4,602 ⟶ 4,623:
 
We therefore use lists to represent sets which works fine here.
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Powerset
 
var sets = [ [1, 2, 3, 4], [], [[]] ]
2,120

edits