Largest int from concatenated ints: Difference between revisions

Content added Content deleted
(Updated to work with version 1.4 of Nim.)
m (→‎{{header|REXX}}: simplified some code, added/changed whitespace and comments, used a smaller font for the output for the 2nd example.)
Line 1,937: Line 1,937:


The absolute value is used for negative numbers.
The absolute value is used for negative numbers.

===simple integers===
===simple integers===
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
Line 1,944: Line 1,943:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
w=0 /* [↓] process all the integer lists.*/
w=0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z=space(@.j) /*keep truckin' until lists exhausted. */
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx=1; big=norm(1) /*keep examining the list until done.*/
do while z\=''; idx= 1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #=norm(k) /*obtain an a number from the list. */
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L=max(length(big), length(#) ) /*get the maximum length of the integer*/
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=#; idx=k /*we found a new biggie (and the index)*/
big= #; idx= k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
end /*k*/ /* [↑] find max concatenated integer. */
z=delword(z, idx, 1) /*delete this maximum integer from list*/
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$=$ || big /*append " " " ───► $. */
$= $ || big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $
end /*j*/ /* [↑] process each list of integers. */
end /*j*/ /* [↑] process each list of integers. */
exit /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #=word(z, i); er=' ***error*** '; if left(#, 1)=="-" then #=substr(#, 2)
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#,'W') then do; say er 'number' # "isn't an integer."; exit 13; end
if \datatype(#,'W') then do; say er # "isn't an integer."; exit 13; end; return #/1</lang>
return # / 1 /*it's an integer, then normalize it. */</lang>
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
<pre>
<pre>
Line 1,983: Line 1,981:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
@.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
@.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
w=0 /* [↓] process all the integer lists.*/
w= 0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z=space(@.j) /*keep truckin' until lists exhausted. */
do j=1 while @.j\==.; z= space(@.j) /*keep truckin' until lists exhausted. */
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx=1; big=norm(1) /*keep examining the list until done.*/
do while z\=''; idx=1; big= norm(1) /*keep examining the list until done.*/
do k=2 to words(z); #=norm(k) /*obtain an a number from the list. */
do k=2 to words(z); #= norm(k) /*obtain an a number from the list. */
L=max(length(big), length(#) ) /*get the maximum length of the integer*/
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=#; idx=k /*we found a new biggie (and the index)*/
big=#; idx= k /*we found a new biggie (and the index)*/
end /*k*/ /* [↑] find max concatenated integer. */
end /*k*/ /* [↑] find max concatenated integer. */
z=delword(z, idx, 1) /*delete this maximum integer from list*/
z= delword(z, idx, 1) /*delete this maximum integer from list*/
$=$ || big /*append " " " ───► $. */
$= $ || big /*append " " " ───► $. */
end /*while z*/ /* [↑] process all integers in a list.*/
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $
Line 1,999: Line 1,997:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #=word(z, i); er=' ***error*** '; if left(#, 1)=="-" then #=substr(#, 2)
norm: arg i; #= word(z, i); er= '***error***'; if left(#, 1)=="-" then #= substr(#, 2)
if \datatype(#,'N') then do; say er 'number' # "isn't an number."; exit 13; end
if \datatype(#, 'N') then signal er13 /*go and tell err msg.*/
else #=# / 1 /*a #, so normalize it*/
else #= # / 1 /*a #, so normalize it*/
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/
numeric digits pow + length(mand) /*expand digs, adjust#*/
numeric digits pow + length(mand) /*expand digs, adjust#*/
end
end
if \datatype(#,'W') then do; say er 'number' # "isn't an integer."; exit 13; end
if datatype(#, 'W') then return # / 1
return #/1</lang>
er13: say er # "isn't an integer."; exit 13</lang>
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}
{{out|output|text=&nbsp; when using the default (internal) integer lists:}}

<pre>
(Output shown at three-quarter size.)

<pre style="font-size:75%">
largest concatenatated integer from 1 34 3 98 9 76 45 4 is 998764543431
largest concatenatated integer from 1 34 3 98 9 76 45 4 is 998764543431
largest concatenatated integer from 54 546 548 60 is 6054854654
largest concatenatated integer from 54 546 548 60 is 6054854654