Largest int from concatenated ints: Difference between revisions

Content added Content deleted
(→‎{{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: Line 1,429:


=={{header|REXX}}==
=={{header|REXX}}==
The algorithm used is based on exact comparisons (left to right)   with   ''right digit fill''   of the   ''left digit''.  
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.
<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
This REXX version works with any integer (negative, zero, positive), &nbsp; and does some basic error checking to
<br>verify that the numbers are integers &nbsp; (and it also normalizes the integers).
<br>verify that the numbers are indeed integers &nbsp; (and it also normalizes the integers).
<lang rexx>/*REXX program constructs largest integer from a list using concatenation. */
<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.*/
@.=.; @.1 = '{1, 34, 3, 98, 9, 76, 45, 4}' /*the 1st integer list to be used. */
@.2 = '{54, 546, 548, 60}' /* " 2nd " " */
@.2 = '{54, 546, 548, 60}' /* " 2nd " " " " " */
@.3 = '{ 4, 45, 54, 5}' /* " 3rd " " */
@.3 = '{ 4, 45, 54, 5}' /* " 3rd " " " " " */
/* [↓] process all the integer lists.*/
/* [↓] process all the integer lists.*/
do j=1 while @.j\==.; $= /*keep truckin' until lists exhausted. */
do j=1 while @.j\==.; $= /*keep truckin' until lists exhausted. */
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the number list.*/
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the integer list*/
_=length(space(z, 0)) + 1 /*determine the largest possibility. */
_=length(space(z, 0)) + 2 /*determine the largest possibility. */
if _>digits() then numeric digits _ /*use enough decimal digits for maximum*/
if _>digits() then numeric digits _ /*use enough decimal digits for maximum*/
/* [↓] examine each number in the list*/
/* [↓] examine each number in the list*/
do while z\==''; index=1 /*keep examining the list until done.*/
do while z\==''; index=1 /*keep examining the list until done.*/
big=isOK(word(z,1)) /*assume that first number is biggest. */
big=isOK(word(z,1)) /*assume that first integer is biggest.*/


do k=2 to words(z); #=isOK(word(z,k)) /*get an integer. */
do k=2 to words(z); #=isOK(word(z,k)) /*obtain an an integer from the list. */
L=max(length(big), length(#)) /*get maximum length*/
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
if left(#, L, left(#,1)) <<= left(big, L, left(big,1)) then iterate
big=#; index=k /*we found a new biggie (and the index)*/
big=#; index=k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
end /*k*/ /* [↑] find max concatenated integer. */


z=space(delword(z, index, 1)) /*remove this maximum # from the list.*/
z=space(delword(z, index, 1)) /*delete this maximum integer from list*/
$=$ || big /*append this maximum # number to $. */
$=$ || big /*append " " " ───► $.*/
end /*while z ··· */ /* [↑] process all integers in a list.*/
end /*while z*/ /* [↑] process all integers in a list.*/


say right($,digits()) ' max for: ' @.j /*show maximum integer and list*/
say right($,digits()) ' max for: ' @.j /*show the maximum integer and the list*/
end /*j*/ /* [↑] process each list of numbers. */
end /*j*/ /* [↑] process each list of integers. */
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1; say /*normalize*/
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1 /*normalize the integer.*/
say '***error!*** number ' ? "isn't an integer."; say; exit 13</lang>
say; say '***error*** number ' ? "isn't an integer."; say; exit 13</lang>
'''output''' &nbsp using the default internal integer lists.
'''output''' &nbsp using the default internal integer lists.
<pre>
<pre>