How to resolve the algorithm Largest int from concatenated ints step by step in the Gambas 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 Gambas 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 Gambas programming language
Source code in the gambas programming language
'Largest int from concatenated ints
Public Sub Main()
Dim iList1 As Integer[] = [1, 34, 3, 98, 9, 76, 45, 4] 'Integer list 1
Dim iList2 As Integer[] = [54, 546, 548, 60] 'Integer list 2
Calc(iList1) 'Send List 1 to Calc routine
Calc(iList2) 'Send List 2 to Calc routine
End
'_________________________________________________________________________________________
Public Sub Calc(iList As Integer[])
Dim siCount1, siCount2, siCounter As Short 'Counters
Dim sList As New String[] 'To hold converted integers
Dim bTrigger As Boolean 'To trigger a found match
For Each siCount1 In iList 'For each integer in the list..
sList.Add(Str(siCount1)) 'Convert to a string and add to sList
If Len(Str(siCount1)) > siCounter Then 'If the length of the string is greater than siCounter then..
siCounter = Len(Str(siCount1)) 'siCounter = length of the string
End If
Next
For siCount1 = 0 To sList.Max 'For each item in sList
If Len(sList[siCount1]) < siCounter Then 'If the length of the string is less that siCounter then..
sList[siCount1] &= Right(sList[siCount1], 1) 'Add the same digit to the string e.g. in list 1 "9" becomes "99", list 2 "54" becomes "544"
End If
Next
sList.Sort(gb.Descent) 'Sort the list in decending order
For siCount1 = 0 To sList.Max 'For each item in sList
bTrigger = False 'Set bTrigger to False
For siCount2 = 0 To iList.Max 'Loop through each item in iList
If Val(sList[siCount1]) = iList[siCount2] Then 'If the value of each is the same e.g. "98" = 98 then
bTrigger = True 'Set bTrigger to True
Continue 'Exit the loop
Endif
Next
If Not bTrigger Then 'If there was no match e.g. there is no "99" then..
sList[siCount1] = Left(sList[siCount1], siCounter - 1) 'Strip out the end digit e.g. "99" becomes 9 again
End If
Next
Print Val(sList.Join("")) 'Join all items in sList together and print
End
You may also check:How to resolve the algorithm Guess the number step by step in the XLISP programming language
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the bc programming language
You may also check:How to resolve the algorithm Cheryl's birthday step by step in the Nim programming language
You may also check:How to resolve the algorithm Read a configuration file step by step in the COBOL programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the MAXScript programming language