WeixiangYan/CodeTransOcean huggingface dataset:
Task: Knapsack problem_Unbounded

C: #include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *name;
    double value;
    double weight;
    double volume;
} item_t;

item_t items[] = {
    {"panacea", 3000.0, 0.3, 0.025},
    {"ichor",   1800.0, 0.2, 0.015},
    {"gold",    2500.0, 2.0, 0.002},
};

int n = sizeof (items) / sizeof (item_t);
int *count;
int *best;
double best_value;

void knapsack (int i, double value, double weight, double volume) {
    int j, m1, m2, m;
    if (i == n) {
        if (value > best_value) {
            best_value = value;
            for (j = 0; j < n; j++) {
                best[j] = count[j];
            }
        }
        return;
    }
    m1 = weight / items[i].weight;
    m2 = volume / items[i].volume;
    m = m1 < m2 ? m1 : m2;
    for (count[i] = m; count[i] >= 0; count[i]--) {
        knapsack(
            i + 1,
            value + count[i] * items[i].value,
            weight - count[i] * items[i].weight,
            volume - count[i] * items[i].volume
        );
    }
}

int main () {
    count = malloc(n * sizeof (int));
    best = malloc(n * sizeof (int));
    best_value = 0;
    knapsack(0, 0.0, 25.0, 0.25);
    int i;
    for (i = 0; i < n; i++) {
        printf("%d %s\n", best[i], items[i].name);
    }
    printf("best value: %.0f\n", best_value);
    free(count); free(best);
    return 0;
}










Fortran: PROGRAM KNAPSACK

  IMPLICIT NONE
 
  REAL :: totalWeight, totalVolume
  INTEGER :: maxPanacea, maxIchor, maxGold, maxValue = 0
  INTEGER :: i, j, k
  INTEGER :: n(3)  

  TYPE Bounty
    INTEGER :: value
    REAL :: weight
    REAL :: volume
  END TYPE Bounty

  TYPE(Bounty) :: panacea, ichor, gold, sack, current

  panacea = Bounty(3000, 0.3, 0.025)
  ichor   = Bounty(1800, 0.2, 0.015)
  gold    = Bounty(2500, 2.0, 0.002)
  sack    = Bounty(0, 25.0, 0.25)

  maxPanacea = MIN(sack%weight / panacea%weight, sack%volume / panacea%volume)
  maxIchor = MIN(sack%weight / ichor%weight, sack%volume / ichor%volume)
  maxGold = MIN(sack%weight / gold%weight, sack%volume / gold%volume)
  
  DO i = 0, maxPanacea
     DO j = 0, maxIchor
        Do k = 0, maxGold
           current%value = k * gold%value + j * ichor%value + i * panacea%value
           current%weight = k * gold%weight + j * ichor%weight + i * panacea%weight
           current%volume = k * gold%volume + j * ichor%volume + i * panacea%volume       
           IF (current%weight > sack%weight .OR. current%volume > sack%volume) CYCLE
           IF (current%value > maxValue) THEN
              maxValue = current%value
              totalWeight = current%weight
              totalVolume = current%volume
              n(1) = i ; n(2) = j ; n(3) = k
           END IF
        END DO  
     END DO
  END DO

  WRITE(*, "(A,I0)") "Maximum value achievable is ", maxValue
  WRITE(*, "(3(A,I0),A)") "This is achieved by carrying ", n(1), " panacea, ", n(2), " ichor and ", n(3), " gold items"
  WRITE(*, "(A,F4.1,A,F5.3)") "The weight to carry is ", totalWeight, " and the volume used is ", totalVolume
 
END PROGRAM KNAPSACK
Task: Variables



C#: int j;









Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables

C: int j;











Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables


C++: int a;










Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables




Go: x := 3








Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables





Java: int a;
double b;
AClassNameHere c;







Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables







Python: 
example1 = 3
example2 = 3.0
example3 = True
example4 = "hello"


example1 = "goodbye"





Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables








VB: Dim variable As datatype
Dim var1,var2,... As datatype




Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables

C: int j;
Task: Variables



C#: int j;
Task: Variables


C++: int a;
Task: Variables





Java: int a;
double b;
AClassNameHere c;
Task: Variables







Python: 
example1 = 3
example2 = 3.0
example3 = True
example4 = "hello"


example1 = "goodbye"
Task: Variables








VB: Dim variable As datatype
Dim var1,var2,... As datatype
Task: Variables




Go: x := 3
Task: Variables



C#: int j;








Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables


C++: int a;









Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables

C: int j;










Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables




Go: x := 3







Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables





Java: int a;
double b;
AClassNameHere c;






Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables







Python: 
example1 = 3
example2 = 3.0
example3 = True
example4 = "hello"


example1 = "goodbye"




Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables








VB: Dim variable As datatype
Dim var1,var2,... As datatype



Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables






PHP: <?php





# NULL typed variable
$null = NULL;				var_dump($null);	// output: null

# defining a boolean
$boolean = true;			var_dump($boolean);	// output: boolean true
$boolean = false;			var_dump($boolean);	// output: boolean false


# bool and boolean is the same
$boolean = (bool)1;			var_dump($boolean);	// output: boolean true
$boolean = (boolean)1;			var_dump($boolean);	// output: boolean true

$boolean = (bool)0;			var_dump($boolean);	// output: boolean false
$boolean = (boolean)0;			var_dump($boolean);	// output: boolean false

# defining an integer
$int = 0;				var_dump($int);		// output: int 0

# defining a float,
$float = 0.01;				var_dump($float);	// output: float 0.01

# which is also identical to "real" and "double"
var_dump((double)$float);					// output: float 0.01
var_dump((real)$float);						// output: float 0.01

# casting back to int (auto flooring the value)
var_dump((int)$float);						// output: int 0
var_dump((int)($float+1));					// output: int 1
var_dump((int)($float+1.9));					// output: int 1

# defining a string
$string = 'string';
var_dump($string);						// output: string 'string' (length=6)

# referencing a variable (there are no pointers in PHP).
$another_string = &$string;
var_dump($another_string);


$string = "I'm the same string!";
var_dump($another_string);

# "deleting" a variable from memory
unset($another_string);

$string = 'string';

$parsed_string = "This is a $string";
var_dump($parsed_string);

$parsed_string .= " with another {$string}";
var_dump($parsed_string);


# with string parsing
$heredoc = <<<HEREDOC
This is the content of \$string: {$string}
HEREDOC;
var_dump($heredoc);


# without string parsing (notice the single quotes surrounding NOWDOC)
$nowdoc = <<<'NOWDOC'
This is the content of \$string: {$string}
NOWDOC;
var_dump($nowdoc);


# as of PHP5, defining an object typed stdClass => standard class
$stdObject = new stdClass();	var_dump($stdObject);

# defining an object typed Foo
class Foo {}
$foo = new Foo();		var_dump($foo);

# defining an empty array
$array = array();		var_dump($array);



$assoc = array(
	0 => $int,
	'integer' => $int,
	1 => $float,
	'float' => $float,
	2 => $string,
	'string' => $string,
	3 => NULL, // <=== key 3 is NULL
	3, // <=== this is a value, not a key (key is 4)
	5 => $stdObject,
	'Foo' => $foo,
);
var_dump($assoc);




















function a_function()
{
	# not reachable
	var_dump(isset($foo));				// output: boolean false

	global $foo;
	# "global" (reachable) inside a_function()'s scope
	var_dump(isset($foo));				// output: boolean true
}

a_function();







Fortran:  program test
 implicit none

 integer :: i  
 integer,dimension(10) :: ivec 
 real :: r 
 real,dimension(10) :: rvec 
 character(len=:),allocatable :: char1, char2  




 i = 1
 r = 3.14


 ivec = 1 
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0


 char1 = 'hello world
 char2 = char1   
 char2(1:1) = 'H'  

end program test
Task: Variables

C: int j;









COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables



C#: int j;







COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables


C++: int a;








COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables





Java: int a;
double b;
AClassNameHere c;





COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables







Python: 
example1 = 3
example2 = 3.0
example3 = True
example4 = "hello"


example1 = "goodbye"



COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables








VB: Dim variable As datatype
Dim var1,var2,... As datatype


COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables




Go: x := 3






COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Variables






PHP: <?php





# NULL typed variable
$null = NULL;				var_dump($null);	// output: null

# defining a boolean
$boolean = true;			var_dump($boolean);	// output: boolean true
$boolean = false;			var_dump($boolean);	// output: boolean false


# bool and boolean is the same
$boolean = (bool)1;			var_dump($boolean);	// output: boolean true
$boolean = (boolean)1;			var_dump($boolean);	// output: boolean true

$boolean = (bool)0;			var_dump($boolean);	// output: boolean false
$boolean = (boolean)0;			var_dump($boolean);	// output: boolean false

# defining an integer
$int = 0;				var_dump($int);		// output: int 0

# defining a float,
$float = 0.01;				var_dump($float);	// output: float 0.01

# which is also identical to "real" and "double"
var_dump((double)$float);					// output: float 0.01
var_dump((real)$float);						// output: float 0.01

# casting back to int (auto flooring the value)
var_dump((int)$float);						// output: int 0
var_dump((int)($float+1));					// output: int 1
var_dump((int)($float+1.9));					// output: int 1

# defining a string
$string = 'string';
var_dump($string);						// output: string 'string' (length=6)

# referencing a variable (there are no pointers in PHP).
$another_string = &$string;
var_dump($another_string);


$string = "I'm the same string!";
var_dump($another_string);

# "deleting" a variable from memory
unset($another_string);

$string = 'string';

$parsed_string = "This is a $string";
var_dump($parsed_string);

$parsed_string .= " with another {$string}";
var_dump($parsed_string);


# with string parsing
$heredoc = <<<HEREDOC
This is the content of \$string: {$string}
HEREDOC;
var_dump($heredoc);


# without string parsing (notice the single quotes surrounding NOWDOC)
$nowdoc = <<<'NOWDOC'
This is the content of \$string: {$string}
NOWDOC;
var_dump($nowdoc);


# as of PHP5, defining an object typed stdClass => standard class
$stdObject = new stdClass();	var_dump($stdObject);

# defining an object typed Foo
class Foo {}
$foo = new Foo();		var_dump($foo);

# defining an empty array
$array = array();		var_dump($array);



$assoc = array(
	0 => $int,
	'integer' => $int,
	1 => $float,
	'float' => $float,
	2 => $string,
	'string' => $string,
	3 => NULL, // <=== key 3 is NULL
	3, // <=== this is a value, not a key (key is 4)
	5 => $stdObject,
	'Foo' => $foo,
);
var_dump($assoc);




















function a_function()
{
	# not reachable
	var_dump(isset($foo));				// output: boolean false

	global $foo;
	# "global" (reachable) inside a_function()'s scope
	var_dump(isset($foo));				// output: boolean true
}

a_function();
Task: Variables






PHP: <?php





# NULL typed variable
$null = NULL;				var_dump($null);	// output: null

# defining a boolean
$boolean = true;			var_dump($boolean);	// output: boolean true
$boolean = false;			var_dump($boolean);	// output: boolean false


# bool and boolean is the same
$boolean = (bool)1;			var_dump($boolean);	// output: boolean true
$boolean = (boolean)1;			var_dump($boolean);	// output: boolean true

$boolean = (bool)0;			var_dump($boolean);	// output: boolean false
$boolean = (boolean)0;			var_dump($boolean);	// output: boolean false

# defining an integer
$int = 0;				var_dump($int);		// output: int 0

# defining a float,
$float = 0.01;				var_dump($float);	// output: float 0.01

# which is also identical to "real" and "double"
var_dump((double)$float);					// output: float 0.01
var_dump((real)$float);						// output: float 0.01

# casting back to int (auto flooring the value)
var_dump((int)$float);						// output: int 0
var_dump((int)($float+1));					// output: int 1
var_dump((int)($float+1.9));					// output: int 1

# defining a string
$string = 'string';
var_dump($string);						// output: string 'string' (length=6)

# referencing a variable (there are no pointers in PHP).
$another_string = &$string;
var_dump($another_string);


$string = "I'm the same string!";
var_dump($another_string);

# "deleting" a variable from memory
unset($another_string);

$string = 'string';

$parsed_string = "This is a $string";
var_dump($parsed_string);

$parsed_string .= " with another {$string}";
var_dump($parsed_string);


# with string parsing
$heredoc = <<<HEREDOC
This is the content of \$string: {$string}
HEREDOC;
var_dump($heredoc);


# without string parsing (notice the single quotes surrounding NOWDOC)
$nowdoc = <<<'NOWDOC'
This is the content of \$string: {$string}
NOWDOC;
var_dump($nowdoc);


# as of PHP5, defining an object typed stdClass => standard class
$stdObject = new stdClass();	var_dump($stdObject);

# defining an object typed Foo
class Foo {}
$foo = new Foo();		var_dump($foo);

# defining an empty array
$array = array();		var_dump($array);



$assoc = array(
	0 => $int,
	'integer' => $int,
	1 => $float,
	'float' => $float,
	2 => $string,
	'string' => $string,
	3 => NULL, // <=== key 3 is NULL
	3, // <=== this is a value, not a key (key is 4)
	5 => $stdObject,
	'Foo' => $foo,
);
var_dump($assoc);




















function a_function()
{
	# not reachable
	var_dump(isset($foo));				// output: boolean false

	global $foo;
	# "global" (reachable) inside a_function()'s scope
	var_dump(isset($foo));				// output: boolean true
}

a_function();








Ada: Name: declare   
   A : constant Integer := 42;  
   X : String := "Hello"; 
   Y : Integer;           
   Z : Integer renames Y: 
   function F (X: Integer) return Integer is
     
     X: Integer := Z;  
   begin
     ...
   end F;  
begin
   Y := 1; 
   declare
     X: Float := -42.0E-10;  
   begin
     ...
   end;
end Name;
Task: Variables






PHP: <?php





# NULL typed variable
$null = NULL;				var_dump($null);	// output: null

# defining a boolean
$boolean = true;			var_dump($boolean);	// output: boolean true
$boolean = false;			var_dump($boolean);	// output: boolean false


# bool and boolean is the same
$boolean = (bool)1;			var_dump($boolean);	// output: boolean true
$boolean = (boolean)1;			var_dump($boolean);	// output: boolean true

$boolean = (bool)0;			var_dump($boolean);	// output: boolean false
$boolean = (boolean)0;			var_dump($boolean);	// output: boolean false

# defining an integer
$int = 0;				var_dump($int);		// output: int 0

# defining a float,
$float = 0.01;				var_dump($float);	// output: float 0.01

# which is also identical to "real" and "double"
var_dump((double)$float);					// output: float 0.01
var_dump((real)$float);						// output: float 0.01

# casting back to int (auto flooring the value)
var_dump((int)$float);						// output: int 0
var_dump((int)($float+1));					// output: int 1
var_dump((int)($float+1.9));					// output: int 1

# defining a string
$string = 'string';
var_dump($string);						// output: string 'string' (length=6)

# referencing a variable (there are no pointers in PHP).
$another_string = &$string;
var_dump($another_string);


$string = "I'm the same string!";
var_dump($another_string);

# "deleting" a variable from memory
unset($another_string);

$string = 'string';

$parsed_string = "This is a $string";
var_dump($parsed_string);

$parsed_string .= " with another {$string}";
var_dump($parsed_string);


# with string parsing
$heredoc = <<<HEREDOC
This is the content of \$string: {$string}
HEREDOC;
var_dump($heredoc);


# without string parsing (notice the single quotes surrounding NOWDOC)
$nowdoc = <<<'NOWDOC'
This is the content of \$string: {$string}
NOWDOC;
var_dump($nowdoc);


# as of PHP5, defining an object typed stdClass => standard class
$stdObject = new stdClass();	var_dump($stdObject);

# defining an object typed Foo
class Foo {}
$foo = new Foo();		var_dump($foo);

# defining an empty array
$array = array();		var_dump($array);



$assoc = array(
	0 => $int,
	'integer' => $int,
	1 => $float,
	'float' => $float,
	2 => $string,
	'string' => $string,
	3 => NULL, // <=== key 3 is NULL
	3, // <=== this is a value, not a key (key is 4)
	5 => $stdObject,
	'Foo' => $foo,
);
var_dump($assoc);




















function a_function()
{
	# not reachable
	var_dump(isset($foo));				// output: boolean false

	global $foo;
	# "global" (reachable) inside a_function()'s scope
	var_dump(isset($foo));				// output: boolean true
}

a_function();






COBOL: MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5
Task: Sort three variables



C#: using System;
public class Program
{
    public static void Main()
    {
        (int x, int y, int z) = (77444, -12, 0);

        
        if (x > y) (x, y) = (y, x);
        if (x > z) (x, z) = (z, x);
        if (y > z) (y, z) = (z, y);
        Console.WriteLine((x, y, z));

        var (a, b, c) = (
            "lions, tigers, and",
            "bears, oh my!",
            "(from the 'Wizard of OZ')");
        
        
        Sort(ref a, ref b, ref c);
        Console.WriteLine((a, b, c));
    }
    
    public static void Sort<T>(ref T a, ref T b, ref T c)
        where T : IComparable<T>
    {
        if (a.CompareTo(b) > 0) (a, b) = (b, a);
        if (a.CompareTo(c) > 0) (a, c) = (c, a);
        if (b.CompareTo(c) > 0) (b, c) = (c, b);
    }
}









Ada: with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Sort_Three is

   generic
      type Element_Type is private;
      with function "<" (Left, Right : in Element_Type) return Boolean;
   procedure Generic_Sort (X, Y, Z : in out Element_Type);

   procedure Generic_Sort (X, Y, Z : in out Element_Type)
   is
      procedure Swap (Left, Right : in out Element_Type) is
         T : constant Element_Type := Left;
      begin
         Left := Right;
         Right := T;
      end Swap;
   begin
      if Y < X then Swap (X, Y); end if;
      if Z < Y then Swap (Y, Z); end if;
      if Y < X then Swap (X, Y); end if;
   end Generic_Sort;

   procedure Test_Unbounded_Sort is
      use Ada.Text_IO;
      use Ada.Strings.Unbounded;

      X : Unbounded_String := To_Unbounded_String ("lions, tigers, and");
      Y : Unbounded_String := To_Unbounded_String ("bears, oh my!");
      Z : Unbounded_String := To_Unbounded_String ("(from the ""Wizard of OZ"")");

      procedure Sort is
         new Generic_Sort (Unbounded_String, "<");
   begin
      Sort (X, Y, Z);
      Put_Line (To_String (X));
      Put_Line (To_String (Y));
      Put_Line (To_String (Z));
      New_Line;
   End Test_Unbounded_Sort;

   procedure Test_Integer_Sort is

      use Ada.Text_IO;

      procedure Sort is
         new Generic_Sort (Integer, "<");

      X : Integer := 77444;
      Y : Integer :=   -12;
      Z : Integer :=     0;
   begin
      Sort (X, Y, Z);
      Put_Line (X'Image);
      Put_Line (Y'Image);
      Put_Line (Z'Image);
      New_Line;
   end Test_Integer_Sort;

begin
   Test_Unbounded_Sort;
   Test_Integer_Sort;
end Sort_Three;