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), and does some basic error checking to |
This REXX version works with any integer (negative, zero, positive), and does some basic error checking to |
||
<br>verify that the numbers are integers (and it also normalizes the integers). |
<br>verify that the numbers are indeed integers (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 to be used. */ |
||
@.2 = '{54, 546, 548, 60}' /* " 2nd " " " " " */ |
|||
@.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 |
z=space(translate(@.j, , '])},{([')) /*perform scrubbing on the integer list*/ |
||
_=length(space(z, 0)) + |
_=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 |
big=isOK(word(z,1)) /*assume that first integer is biggest.*/ |
||
do k=2 to words(z); |
do k=2 to words(z); #=isOK(word(z,k)) /*obtain an an integer from the list. */ |
||
L=max(length(big), 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)) /* |
z=space(delword(z, index, 1)) /*delete this maximum integer from list*/ |
||
$=$ || big /*append |
$=$ || big /*append " " " ───► $.*/ |
||
end /*while z |
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 |
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 |
isOK: parse arg ?; if datatype(?,'W') then return abs(?)/1 /*normalize the integer.*/ |
||
say '***error |
say; say '***error*** number ' ? "isn't an integer."; say; exit 13</lang> |
||
'''output'''   using the default internal integer lists. |
'''output'''   using the default internal integer lists. |
||
<pre> |
<pre> |