Anonymous user
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), and does some basic error checking to
<br>verify that the numbers are indeed integers (and it also normalizes the integers).
<lang rexx>/*REXX program constructs the largest integer from a list using concatenation. */
@.
/* [↓] process all the integer lists.*/
do j=1 while @.j\==.; $= /*keep truckin' until lists exhausted. */
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the
_=length(space(z, 0)) +
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
do k=2 to words(z);
L=max(length(big), length(#))
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)) /*
$=$ || big /*append
end /*while z
say right($,digits()) ' max for: ' @.j /*show the maximum integer and the list*/
end /*j*/ /* [↑] process each list of
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1
say; say '***error
'''output'''   using the default internal integer lists.
<pre>
|