Largest int from concatenated ints: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, changed wording in the REXX section header.
(→‎{{header|JavaScript}}: enables code formatting)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, changed wording in the REXX section header.)
Line 1,429:
 
=={{header|REXX}}==
The algorithm used is based on exact comparisons (left to right)   with   ''right digit fill''   of the   ''left digit''.  
<br>This allows the integers to be of any size.
 
This REXX version works with any integer (negative, zero, positive), &nbsp; and does some basic error checking to
<br>verify that the numbers are indeed integers &nbsp; (and it also normalizes the integers).
<lang rexx>/*REXX program constructs the largest integer from a list using concatenation. */
@. =.; @.1 = '{1, 34, 3, 98, 9, 76, 45, 4}' /*the 1st integer list to be used. */
@.2 = '{54, 546, 548, 60}' /* " 2nd " " " " " */
@.3 = '{ 4, 45, 54, 5}' /* " 3rd " " " " " */
/* [↓] process all the integer lists.*/
do j=1 while @.j\==.; $= /*keep truckin' until lists exhausted. */
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the numberinteger list.*/
_=length(space(z, 0)) + 12 /*determine the largest possibility. */
if _>digits() then numeric digits _ /*use enough decimal digits for maximum*/
/* [↓] examine each number in the list*/
do while z\==''; index=1 /*keep examining the list until done.*/
big=isOK(word(z,1)) /*assume that first numberinteger is biggest. */
 
do k=2 to words(z); #=isOK(word(z,k)) /*obtain an an integer from /*getthe an integerlist. */
L=max(length(big), length(#)) /*get the maximum length of the integer*/
if left(#, L, left(#,1)) <<= left(big, L, left(big,1)) then iterate
big=#; index=k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
 
z=space(delword(z, index, 1)) /*removedelete this maximum # integer from the list.*/
$=$ || big /*append this maximum #" number to " " ───► $. */
end /*while z ··· */ /* [↑] process all integers in a list.*/
 
say right($,digits()) ' max for: ' @.j /*show the maximum integer and the list*/
end /*j*/ /* [↑] process each list of numbersintegers. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1; say /*normalize the integer.*/
say; say '***error!*** number ' ? "isn't an integer."; say; exit 13</lang>
'''output''' &nbsp using the default internal integer lists.
<pre>