How to resolve the algorithm Power set step by step in the ALGOL 68 programming language
How to resolve the algorithm Power set step by step in the ALGOL 68 programming language
Table of Contents
Problem Statement
A set is a collection (container) of certain values, without any particular order, and no repeated values. It corresponds with a finite set in mathematics. A set can be implemented as an associative array (partial mapping) in which the value of each key-value pair is ignored. Given a set S, the power set (or powerset) of S, written P(S), or 2S, is the set of all subsets of S.
By using a library or built-in set type, or by defining a set type with necessary operations, write a function with a set S as input that yields the power set 2S of S.
For example, the power set of {1,2,3,4} is For a set which contains n elements, the corresponding power set has 2n elements, including the edge cases of empty set. The power set of the empty set is the set which contains itself (20 = 1): And 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 (21 = 2):
Extra credit: Demonstrate that your language supports these last two powersets.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Power set step by step in the ALGOL 68 programming language
Source code in the algol programming language
MODE MEMBER = INT;
PROC power set = ([]MEMBER s)[][]MEMBER:(
[2**UPB s]FLEX[1:0]MEMBER r;
INT upb r := 0;
r[upb r +:= 1] := []MEMBER(());
FOR i TO UPB s DO
MEMBER e = s[i];
FOR j TO upb r DO
[UPB r[j] + 1]MEMBER x;
x[:UPB x-1] := r[j];
x[UPB x] := e; # append to the end of x #
r[upb r +:= 1] := x # append to end of r #
OD
OD;
r[upb r] := s;
r
);
# Example: #
test:(
[][]MEMBER set = power set((1, 2, 4));
FOR member TO UPB set DO
INT upb = UPB set[member];
FORMAT repr set = $"("f( upb=0 | $$ | $n(upb-1)(d", ")d$ )");"$;
printf(($"set["d"] = "$,member, repr set, set[member],$l$))
OD
)
You may also check:How to resolve the algorithm Special variables step by step in the Arturo programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the REXX programming language
You may also check:How to resolve the algorithm Arrays step by step in the Clojure programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Go programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Common Lisp programming language