Largest int from concatenated ints: Difference between revisions

(GP)
Line 15:
* [http://www.quora.com/Algorithms/What-is-the-most-efficient-way-to-arrange-the-given-numbers-to-form-the-biggest-number Algorithms: What is the most efficient way to arrange the given numbers to form the biggest number?].
* [http://stackoverflow.com/questions/14532105/constructing-the-largest-number-possible-by-rearranging-a-list/14539943#14539943 Constructing the largest number possible by rearranging a list]
 
=={{header|Ada}}==
 
The algorithmic idea is to apply a twisted comparison function:
 
<lang Ada>function Order(Left, Right: Natural) return Boolean is
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );</lang>
This function converts the parameters Left and Right to strings and returns True if (Left before Right)
exceeds (Right before Left). It needs Ada 2012 -- the code for older versions of Ada would be more verbose.
 
The rest is straightforward: Run your favourite sorting subprogram that allows to use the function "Order" instead of standard comparison operators ("<" or ">" or so) and print the results:
 
<lang Ada>with Ada.Text_IO, Ada.Containers.Generic_Array_Sort;
 
procedure Largest_Int_From_List is
function Img(N: Natural) return String is
S: String := Integer'Image(N);
begin
return S(S'First+1 .. S'Last); -- First character is ' '
end Img;
function Order(Left, Right: Natural) return Boolean is
( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );
type Arr_T is array(Positive range <>) of Natural;
procedure Sort is new Ada.Containers.Generic_Array_Sort
(Positive, Natural, Arr_T, Order);
procedure Print_Sorted(A: Arr_T) is
B: Arr_T := A;
begin
Sort(B);
for Number of B loop
Ada.Text_IO.Put(Img(Number));
end loop;
Ada.Text_IO.New_Line;
end Print_Sorted;
begin
Print_Sorted((1, 34, 3, 98, 9, 76, 45, 4));
Print_Sorted((54, 546, 548, 60));
end Largest_Int_From_List;</lang>
 
=={{header|AWK}}==
Anonymous user