Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/Equilibrium-index/Ada/equilibrium-index-1.ada
Normal file
18
Task/Equilibrium-index/Ada/equilibrium-index-1.ada
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
with Ada.Containers.Vectors;
|
||||
|
||||
generic
|
||||
type Index_Type is range <>;
|
||||
type Element_Type is private;
|
||||
Zero : Element_Type;
|
||||
with function "+" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
with function "-" (Left, Right : Element_Type) return Element_Type is <>;
|
||||
with function "=" (Left, Right : Element_Type) return Boolean is <>;
|
||||
type Array_Type is private;
|
||||
with function Element (From : Array_Type; Key : Index_Type) return Element_Type is <>;
|
||||
package Equilibrium is
|
||||
package Index_Vectors is new Ada.Containers.Vectors
|
||||
(Index_Type => Positive, Element_Type => Index_Type);
|
||||
|
||||
function Get_Indices (From : Array_Type) return Index_Vectors.Vector;
|
||||
|
||||
end Equilibrium;
|
||||
20
Task/Equilibrium-index/Ada/equilibrium-index-2.ada
Normal file
20
Task/Equilibrium-index/Ada/equilibrium-index-2.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package body Equilibrium is
|
||||
|
||||
function Get_Indices (From : Array_Type) return Index_Vectors.Vector is
|
||||
Result : Index_Vectors.Vector;
|
||||
Right_Sum, Left_Sum : Element_Type := Zero;
|
||||
begin
|
||||
for Index in Index_Type'Range loop
|
||||
Right_Sum := Right_Sum + Element (From, Index);
|
||||
end loop;
|
||||
for Index in Index_Type'Range loop
|
||||
Right_Sum := Right_Sum - Element (From, Index);
|
||||
if Left_Sum = Right_Sum then
|
||||
Index_Vectors.Append (Result, Index);
|
||||
end if;
|
||||
Left_Sum := Left_Sum + Element (From, Index);
|
||||
end loop;
|
||||
return Result;
|
||||
end Get_Indices;
|
||||
|
||||
end Equilibrium;
|
||||
52
Task/Equilibrium-index/Ada/equilibrium-index-3.ada
Normal file
52
Task/Equilibrium-index/Ada/equilibrium-index-3.ada
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
with Ada.Text_IO;
|
||||
with Equilibrium;
|
||||
with Ada.Containers.Vectors;
|
||||
|
||||
procedure Main is
|
||||
subtype Index_Type is Positive range 1 .. 7;
|
||||
package Vectors is new Ada.Containers.Vectors
|
||||
(Element_Type => Integer, Index_Type => Index_Type);
|
||||
type Plain_Array is array (Index_Type) of Integer;
|
||||
function Element (From : Plain_Array; Key : Index_Type) return Integer is
|
||||
begin
|
||||
return From (Key);
|
||||
end Element;
|
||||
|
||||
package Vector_Equilibrium is new Equilibrium
|
||||
(Index_Type => Index_Type,
|
||||
Element_Type => Integer,
|
||||
Zero => 0,
|
||||
Array_Type => Vectors.Vector,
|
||||
Element => Vectors.Element);
|
||||
package Array_Equilibrium is new Equilibrium
|
||||
(Index_Type => Index_Type,
|
||||
Element_Type => Integer,
|
||||
Zero => 0,
|
||||
Array_Type => Plain_Array);
|
||||
|
||||
My_Vector : Vectors.Vector;
|
||||
My_Array : Plain_Array := (-7, 1, 5, 2, -4, 3, 0);
|
||||
Vector_Result : Vector_Equilibrium.Index_Vectors.Vector;
|
||||
Array_Result : Array_Equilibrium.Index_Vectors.Vector :=
|
||||
Array_Equilibrium.Get_Indices (My_Array);
|
||||
begin
|
||||
Vectors.Append (My_Vector, -7);
|
||||
Vectors.Append (My_Vector, 1);
|
||||
Vectors.Append (My_Vector, 5);
|
||||
Vectors.Append (My_Vector, 2);
|
||||
Vectors.Append (My_Vector, -4);
|
||||
Vectors.Append (My_Vector, 3);
|
||||
Vectors.Append (My_Vector, 0);
|
||||
Vector_Result := Vector_Equilibrium.Get_Indices (My_Vector);
|
||||
Ada.Text_IO.Put_Line ("Results:");
|
||||
Ada.Text_IO.Put ("Array: ");
|
||||
for I in Array_Result.First_Index .. Array_Result.Last_Index loop
|
||||
Ada.Text_IO.Put (Integer'Image (Array_Equilibrium.Index_Vectors.Element (Array_Result, I)));
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("Vector: ");
|
||||
for I in Vector_Result.First_Index .. Vector_Result.Last_Index loop
|
||||
Ada.Text_IO.Put (Integer'Image (Vector_Equilibrium.Index_Vectors.Element (Vector_Result, I)));
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
end Main;
|
||||
68
Task/Equilibrium-index/Ada/equilibrium-index-4.ada
Normal file
68
Task/Equilibrium-index/Ada/equilibrium-index-4.ada
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Equilibrium is
|
||||
|
||||
type Integer_Sequence is array (Positive range <>) of Integer;
|
||||
function Seq_Img (From : Integer_Sequence) return String is
|
||||
begin
|
||||
if From'First /= From'Last then
|
||||
return " " & Integer'Image (From (From'First)) &
|
||||
Seq_Img (From (From'First + 1 .. From'Last));
|
||||
else
|
||||
return " " & Integer'Image (From (From'First));
|
||||
end if;
|
||||
end Seq_Img;
|
||||
|
||||
type Boolean_Sequence is array (Positive range <>) of Boolean;
|
||||
function Seq_Img (From : Boolean_Sequence) return String is
|
||||
begin
|
||||
if From'First > From'Last then
|
||||
return "";
|
||||
end if;
|
||||
if From (From'First) then
|
||||
return Integer'Image (From'First) &
|
||||
Seq_Img (From (From'First + 1 .. From'Last));
|
||||
else
|
||||
return Seq_Img (From (From'First + 1 .. From'Last));
|
||||
end if;
|
||||
end Seq_Img;
|
||||
|
||||
function Get_Indices (From : Integer_Sequence) return Boolean_Sequence is
|
||||
Result : Boolean_Sequence (From'Range) := (others => False);
|
||||
Left_Sum, Right_Sum : Integer := 0;
|
||||
begin
|
||||
for Index in From'Range loop
|
||||
Right_Sum := Right_Sum + From (Index);
|
||||
end loop;
|
||||
for Index in From'Range loop
|
||||
Right_Sum := Right_Sum - From (Index);
|
||||
Result (Index) := Left_Sum = Right_Sum;
|
||||
Left_Sum := Left_Sum + From (Index);
|
||||
end loop;
|
||||
return Result;
|
||||
end Get_Indices;
|
||||
|
||||
X1 : Integer_Sequence := (-7, 1, 5, 2, -4, 3, 0);
|
||||
X1_Result : Boolean_Sequence := Get_Indices (X1);
|
||||
X2 : Integer_Sequence := ( 2, 4, 6);
|
||||
X2_Result : Boolean_Sequence := Get_Indices (X2);
|
||||
X3 : Integer_Sequence := ( 2, 9, 2);
|
||||
X3_Result : Boolean_Sequence := Get_Indices (X3);
|
||||
X4 : Integer_Sequence := ( 1, -1, 1, -1, 1 ,-1, 1);
|
||||
X4_Result : Boolean_Sequence := Get_Indices (X4);
|
||||
|
||||
begin
|
||||
Ada.Text_IO.Put_Line ("Results:");
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("X1:" & Seq_Img (X1));
|
||||
Ada.Text_IO.Put_Line ("Eqs:" & Seq_Img (X1_Result));
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("X2:" & Seq_Img (X2));
|
||||
Ada.Text_IO.Put_Line ("Eqs:" & Seq_Img (X2_Result));
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("X3:" & Seq_Img (X3));
|
||||
Ada.Text_IO.Put_Line ("Eqs:" & Seq_Img (X3_Result));
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put_Line ("X4:" & Seq_Img (X4));
|
||||
Ada.Text_IO.Put_Line ("Eqs:" & Seq_Img (X4_Result));
|
||||
end Equilibrium;
|
||||
Loading…
Add table
Add a link
Reference in a new issue