How to resolve the algorithm Largest int from concatenated ints step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest int from concatenated ints step by step in the Ada programming language

Table of Contents

Problem Statement

Given a set of positive integers, write a function to order the integers in such a way that the concatenation of the numbers forms the largest possible integer and return this integer. Use the following two sets of integers as tests   and   show your program output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest int from concatenated ints step by step in the Ada programming language

Source code in the ada programming language

function Order(Left, Right: Natural) return Boolean is
      ( (Img(Left) & Img(Right)) > (Img(Right) & Img(Left)) );


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;


  

You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Pascal programming language
You may also check:How to resolve the algorithm Parallel brute force step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Power set step by step in the VBScript programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the AWK programming language
You may also check:How to resolve the algorithm Reflection/List methods step by step in the Python programming language