Power set: Difference between revisions

1,181 bytes removed ,  10 years ago
Line 8:
 
=={{header|Ada}}==
 
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
This solution prints the power set of words read from the command line.
<lang ada>with Ada.Text_IO, Ada.Command_Line;
procedure Power_Set is
type Universe is (A,B,C,D,E);
type List is array (Positive range <>) of Positive;
Empty: List(1 .. 0);
-- The type Set are subsets of Universe
type Set is array (Universe) of Boolean;
procedure Print_All_Subsets(Set: List; Printable: List:= Empty) is
Empty : constant Set := (others => False);
function Cardinality (X : Set) return Natural is
Nprocedure Print_Set(Items: NaturalList) :=is 0;
First: Boolean := True;
begin
Ada.Text_IO.Put("{ ");
for Item of Items loop
if First then
First := False; -- no comma needed
else
Ada.Text_IO.Put(", "); -- comma, to separate the items
end if;
Ada.Text_IO.Put(Ada.Command_Line.Argument(Item));
end loop;
Ada.Text_IO.Put_Line(" }");
end Print_Set;
Tail: List := Set(Set'First+1 .. Set'Last);
begin
forif ISet in= X'RangeEmpty loopthen
Print_Set(Printable);
if X (I) then
N := N + 1;else
Print_All_Subsets(Tail, Printable & Set(Set'First));
end if;
Print_All_Subsets(Tail, Printable);
end loop;
return N;
end Cardinality;
function Element (X : Set; Position : Positive) return Universe is
N : Natural := 0;
begin
for I in X'Range loop
if X (I) then
N := N + 1;
if N = Position then
return I;
end if;
end if;
end loop;
raise Constraint_Error;
end Element;
procedure Put (X : Set) is
Empty : Boolean := True;
begin
for I in X'Range loop
if X (I) then
if Empty then
Empty := False;
Put (Universe'Image (I));
else
Put ("," & Universe'Image (I));
end if;
end if;
end loop;
if Empty then
Put ("empty");
end if;
end PutPrint_All_Subsets;
Set: List(1 .. Ada.Command_Line.Argument_Count);
-- Set_Of_Set are sets of subsets of Universe
type Set_Of_Sets is array (Positive range <>) of Set;
function Power (X : Set) return Set_Of_Sets is
Length : constant Natural := Cardinality (X);
Index : array (1..Length) of Integer := (others => 0);
Result : Set_Of_Sets (1..2**Length) := (others => Empty);
begin
for N in Result'Range loop
for I in 1..Length loop -- Index determines the sample N
exit when Index (I) = 0;
Result (N) (Element (X, Index (I))) := True;
end loop;
Next : for I in 1..Length loop -- Computing the index of the following sample
if Index (I) < Length then
Index (I) := Index (I) + 1;
if I = 1 or else Index (I - 1) > Index (I) then
for J in reverse 2..I loop
Index (J - 1) := Index (J) + 1;
end loop;
exit Next;
end if;
end if;
end loop Next;
end loop;
return Result;
end Power;
P : Set_Of_Sets := Power ((A|C|E => True, others => False));
begin
for I in PSet'Range loop -- initialize set
New_LineSet(I) := I;
Put (P (I));
end loop;
Print_All_Subsets(Set); -- do the work
end Power_Set;</lang>
 
This example computes the power set of {A,C,E}. Sample output:
{{out}}
<pre>
 
empty
<pre>>./power_set cat dog mouse
A
{ cat, dog, mouse }
C
{ cat, dog }
E
{ cat, mouse }
A,C
{ cat }
A,E
{ dog, mouse }
C,E
{ dog }
A,C,E
{ mouse }
</pre>
{ }
>./power_set 1 2
{ 1, 2 }
{ 1 }
{ 2 }
{ }</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
Anonymous user