Anonymous user
Largest int from concatenated ints: Difference between revisions
Largest int from concatenated ints (view source)
Revision as of 17:50, 3 February 2021
, 3 years ago→{{header|REXX}}: simplified some code, added/changed whitespace and comments, used a smaller font for the output for the 2nd example.
(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:
The absolute value is used for negative numbers.
===simple integers===
<lang rexx>/*REXX program constructs the largest integer from an integer list using concatenation.*/
Line 1,944 ⟶ 1,943:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
w=0 /* [↓] process all the integer lists.*/
do j=1 while @.j\==.; z= space(@.j)
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx= 1;
do k=2 to words(z); #= norm(k)
L= max(length(big), length(#) )
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big= #; idx= k
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1)
$= $ ||
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is ─────► " $
end /*j*/ /* [↑] process each list of integers. */
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i);
if \datatype(#,'W') then do; say er
{{out|output|text= when using the default (internal) integer lists:}}
<pre>
Line 1,983 ⟶ 1,981:
@.3 = ' 4 45 54 5' /* " 3rd " " " " " */
@.4 = ' 4 45 54 5 6.6e77' /* " 4th " " " " " */
w=
do j=1 while @.j\==.; z= space(@.j)
w=max(w, length(z) ); $= /*obtain maximum width to align output.*/
do while z\=''; idx=1; big= norm(1)
do k=2 to words(z); #= norm(k)
L= max(length(big), length(#) )
if left(#, L, left(#, 1) ) <<= left(big, L, left(big, 1) ) then iterate
big=#; idx=
end /*k*/ /* [↑] find max concatenated integer. */
z= delword(z, idx, 1)
$= $ ||
end /*while z*/ /* [↑] process all integers in a list.*/
say 'largest concatenatated integer from ' left( space(@.j), w) " is " $
Line 1,999 ⟶ 1,997:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
norm: arg i; #= word(z, i);
if \datatype(#, 'N') then
else #= # / 1
if pos('E',#)>0 then do; parse var # mant "E" pow /*Has exponent? Expand*/
numeric digits pow + length(mand) /*expand digs, adjust#*/
end
if
er13: say er #
{{out|output|text= when using the default (internal) integer lists:}}
(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 54 546 548 60 is 6054854654
|