How to resolve the algorithm Largest int from concatenated ints step by step in the Picat 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 Picat 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 Picat programming language

Source code in the picat programming language

s_perm1(L, Num) => 
  permutation(L,P),
  Num = [I.to_string() : I in P].flatten().to_integer().

s_perm2(L, Num) => 
  Perms = permutations(L),
  Num = max([  [I.to_string() : I in P].flatten().to_integer() : P in Perms]).

s_sort_conc(L,Num) =>
  Num = [to_string(I) : I in qsort(L,f3)].join('').to_integer().

% sort function for s_sort_conc/2
f3(N1,N2) => 
  N1S = N1.to_string(),
  N2S = N2.to_string(),
  (N1S ++ N2S).to_integer() >= (N2S ++ N1S).to_integer().

% qsort(List, SortFunction)
% returns a sorted list according to the sort function SortFunction.
qsort([],_F)    = [].
qsort([H|T],F) = qsort([E : E in T, call(F,E,H)], F) 
                 ++ [H] ++
                 qsort([E : E in T, not call(F,E,H)],F).

s_extend(L,Num) =>
  LS =  [I.to_string() : I in L],
  MaxLen = 2*max([I.length : I in LS]),
  L2 = [],
  foreach(I in LS)
    I2 = I,
    % extend to a larger length
    while(I2.length < MaxLen)
     I2 := I2 ++ I
    end,
    % keep info of the original number
    L2 := L2 ++ [[I2,I]]
  end,
  Num = [I[2] : I in qsort(L2,f4)].join('').to_integer().

% sort function for s_extend/2
f4(N1,N2) => N1[1].to_integer() >= N2[1].to_integer().

import util.

go =>
  Ls = [[1, 34, 3, 98, 9, 76, 45, 4],
        [54, 546, 548, 60],
        [97, 9, 13, 979],
        [9, 1, 95, 17, 5]
        ],
  foreach(L in Ls)
    test(L)
  end,
  nl.

  
% Test all implementations
test(L) =>
  println(l=L),

  maxof_inc(s_perm1(L,Num1), Num1),
  println(s_perm1=Num1),

  s_perm2(L,Num2),
  println(s_perm2=Num2),

  s_sort_conc(L,Num3),
  println(s_sort_conc=Num3),

  s_extend(L,Num4),
  println(s_extent=Num4),
  nl.

go2 =>
  garbage_collect(100_000_000),
  _ = random2(),
  N = 2000,
  println(nums=N),
  L = [random(1,1000) : _ in 1..N],
  S = join([I.to_string : I in L],''),
  println(str_len=S.len),  

  nl,
  println("s_sort_conc:"),
  time(s_sort_conc(L,_Num3)),

  println("s_extend:"),
  time(s_extend(L,_Num4)),

  nl.

  

You may also check:How to resolve the algorithm Knapsack problem/Unbounded step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Image convolution step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Self numbers step by step in the Go programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the GAP programming language