Last list item: Difference between revisions

Content added Content deleted
(→‎{{header|ALGOL 68}}: Revised to meet the task requirements re the positioning of the new element)
Line 10: Line 10:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{improve|Algol 68|Task specifically requires that the interim sum be appended to the '''end''' of the list.}}
Was a translation of the sorted Wren version but the sorting has been removed as per the revised task requirements.
Was a translation of the sorted Wren version but the sorting has been removed as per the revised task requirements.
<lang algol68>BEGIN # find the last element after repeatedly adding the sum #
<lang algol68>BEGIN # find the last element after repeatedely adding the sum #
# of the two smallest elements and removing them #
# of the two smallest elements and removing them #
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
Line 35: Line 34:
)
)
);
);
a[ s1pos ] := sum;
INT m pos := 0;
FOR i FROM s2pos TO a count - 1 DO a[ i ] := a[ i + 1 ] OD;
FOR i TO a count DO
IF i /= s1pos AND i /= s2pos THEN a[ m pos +:= 1 ] := a[ i ] FI
OD;
a[ m pos + 1 ] := sum;
a count -:= 1
a count -:= 1
OD;
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</lang>
END
</lang>
{{out}}
{{out}}
<pre>
<pre>
List: 6 81 243 14 25 49 123 69 11; two smallest: 6 @ 1 and 11 @ 9; sum = 17
List: 6 81 243 14 25 49 123 69 11; two smallest: 6 @ 1 and 11 @ 9; sum = 17
List: 17 81 243 14 25 49 123 69; two smallest: 14 @ 4 and 17 @ 1; sum = 31
List: 81 243 14 25 49 123 69 17; two smallest: 14 @ 3 and 17 @ 8; sum = 31
List: 81 243 31 25 49 123 69; two smallest: 25 @ 4 and 31 @ 3; sum = 56
List: 81 243 25 49 123 69 31; two smallest: 25 @ 3 and 31 @ 7; sum = 56
List: 81 243 56 49 123 69; two smallest: 49 @ 4 and 56 @ 3; sum = 105
List: 81 243 49 123 69 56; two smallest: 49 @ 3 and 56 @ 6; sum = 105
List: 81 243 105 123 69; two smallest: 69 @ 5 and 81 @ 1; sum = 150
List: 81 243 123 69 105; two smallest: 69 @ 4 and 81 @ 1; sum = 150
List: 243 105 123 150; two smallest: 105 @ 2 and 123 @ 3; sum = 228
List: 243 123 105 150; two smallest: 105 @ 3 and 123 @ 2; sum = 228
List: 243 228 150; two smallest: 150 @ 3 and 228 @ 2; sum = 378
List: 243 150 228; two smallest: 150 @ 2 and 228 @ 3; sum = 378
List: 243 378; two smallest: 243 @ 1 and 378 @ 2; sum = 621
List: 243 378; two smallest: 243 @ 1 and 378 @ 2; sum = 621
Last item is 621.
Last item is 621.