Power set: Difference between revisions

901 bytes removed ,  2 months ago
m
m (RPL: add section)
 
(7 intermediate revisions by 3 users not shown)
Line 1,642:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Power_set}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
No program needed. Power set is intrinsically supported in Fōrmulæ.
In '''[https://formulae.org/?example=Power_set this]''' page you can see the program(s) related to this task and their results.
 
'''Case 1.''' Power set of the set {1, 2, 3, 4}
 
[[File:Fōrmulæ - Power set 01.png]]
 
[[File:Fōrmulæ - Power set 02.png]]
 
'''Case 2.''' The power set of the empty set is the set which contains itself.
 
[[File:Fōrmulæ - Power set 03.png]]
 
[[File:Fōrmulæ - Power set 04.png]]
 
'''Case 3.''' The power set of the set which contains only the empty set, has two subsets, the empty set and the set which contains the empty set
 
[[File:Fōrmulæ - Power set 05.png]]
 
[[File:Fōrmulæ - Power set 06.png]]
 
'''Case 4.''' Even when it is intrinsically supported, a program can be written:
 
[[File:Fōrmulæ - Power set 07.png]]
 
[[File:Fōrmulæ - Power set 08.png]]
 
[[File:Fōrmulæ - Power set 09.png]]
 
=={{header|GAP}}==
Line 3,618 ⟶ 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,576 ⟶ 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