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

Source code in the factor programming language

USING: assocs io kernel math qw sequences sorting ;
IN: rosetta-code.largest-int

: pad ( target seq -- padded )
    2dup length / swap <repetition> concat swap head ;
    
: largest-int ( seq -- )
    dup dup [ length ] map supremum    ! find longest length so we know how much to pad
    [ swap pad ] curry map             ! pad the integers
    <enum> sort-values                 ! sort the padded integers
    keys                               ! find the original indices of the sorted integers
    swap nths                          ! order non-padded integers according to their sorted order
    reverse concat print ;             
    
qw{ 1 34 3 98 9 76 45 4 } qw{ 54 546 548 60 } [ largest-int ] bi@


USING: kernel math.order qw sequences sorting ;

: fn ( seq -- str )
    [ 2dup swap [ append ] 2bi@ after? +lt+ +gt+ ? ] sort concat ;


  

You may also check:How to resolve the algorithm Stern-Brocot sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Python programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the SQL programming language
You may also check:How to resolve the algorithm Morse code step by step in the Go programming language
You may also check:How to resolve the algorithm Variable size/Set step by step in the Raku programming language