Power set: Difference between revisions
m
→{{header|Fōrmulæ}}
(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).*/
text='one two three four'
n=words(text)
psi=0
Do k=0 To n
Do while
Parse Var cc elist '/' cc /* get i from comb's result string */
psl=''
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
Say right(psi,2) '{'psl'}' /* show this element of the power set */
End
End
Exit
▲ end /*d*/
comb: Procedure
/***********************************************************************
* Returns the combinations of size digits out of things digits
* e.g. comb(4,2) -> ' 1 2/1 3/1 4/2 3/2 4/3 4/'
***********************************************************************/
Parse Arg things,size
▲ p= !.d
n=2**things-1
list=''
Do u=1 To n
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
If substr(ub,i,1)=1 Then res=res i
End
Return res
End
Else
{{out|output|text= when using the default input:}}
<pre>
Line 4,602 ⟶ 4,623:
We therefore use lists to represent sets which works fine here.
<syntaxhighlight lang="
var sets = [ [1, 2, 3, 4], [], [[]] ]
|