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 |
<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: | ||
) |
) |
||
); |
); |
||
INT m pos := 0; |
|||
FOR i |
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 |
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: |
List: 81 243 14 25 49 123 69 17; two smallest: 14 @ 3 and 17 @ 8; sum = 31 |
||
List: 81 243 |
List: 81 243 25 49 123 69 31; two smallest: 25 @ 3 and 31 @ 7; sum = 56 |
||
List: 81 243 |
List: 81 243 49 123 69 56; two smallest: 49 @ 3 and 56 @ 6; sum = 105 |
||
List: 81 243 |
List: 81 243 123 69 105; two smallest: 69 @ 4 and 81 @ 1; sum = 150 |
||
List: 243 |
List: 243 123 105 150; two smallest: 105 @ 3 and 123 @ 2; sum = 228 |
||
List: 243 |
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. |