September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,10 +0,0 @@
package Power_Set is
type Set is array (Positive range <>) of Positive;
Empty_Set: Set(1 .. 0);
generic
with procedure Visit(S: Set);
procedure All_Subsets(S: Set); -- calles Visit once for each subset of S
end Power_Set;

View file

@ -1,20 +0,0 @@
package body Power_Set is
procedure All_Subsets(S: Set) is
procedure Visit_Sets(Unmarked: Set; Marked: Set) is
Tail: Set := Unmarked(Unmarked'First+1 .. Unmarked'Last);
begin
if Unmarked = Empty_Set then
Visit(Marked);
else
Visit_Sets(Tail, Marked & Unmarked(Unmarked'First));
Visit_Sets(Tail, Marked);
end if;
end Visit_Sets;
begin
Visit_Sets(S, Empty_Set);
end All_Subsets;
end Power_Set;

View file

@ -1,28 +0,0 @@
with Ada.Text_IO, Ada.Command_Line, Power_Set;
procedure Print_Power_Set is
procedure Print_Set(Items: Power_Set.Set) is
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;
procedure Print_All_Subsets is new Power_Set.All_Subsets(Print_Set);
Set: Power_Set.Set(1 .. Ada.Command_Line.Argument_Count);
begin
for I in Set'Range loop -- initialize set
Set(I) := I;
end loop;
Print_All_Subsets(Set); -- do the work
end;

View file

@ -0,0 +1,28 @@
with Ada.Text_IO, Ada.Command_Line;
use Ada.Text_IO, Ada.Command_Line;
procedure powerset is
procedure print_subset (set : natural) is
-- each i'th binary digit of "set" indicates if the i'th integer belongs to "set" or not.
k : natural := set;
first : boolean := true;
begin
Put ("{");
for i in 1..Argument_Count loop
if k mod 2 = 1 then
if first then
first := false;
else
Put (",");
end if;
Put (Argument (i));
end if;
k := k / 2; -- we go to the next bit of "set"
end loop;
Put_Line("}");
end print_subset;
begin
for i in 0..2**Argument_Count-1 loop
print_subset (i);
end loop;
end powerset;