Last list item: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Added AppleScript.) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(14 intermediate revisions by 11 users not shown) | |||
Line 12:
{{trans|Python}}
<
L lis.len > 1
V el1 = lis.pop(lis.index(min(lis)))
Line 22:
V LIST = [6, 81, 243, 14, 25, 49, 123, 69, 11]
print(LIST‘ ==> ’add_least_reduce(©(LIST)))</
{{out}}
Line 35:
Interim list: [621]
[6, 81, 243, 14, 25, 49, 123, 69, 11] ==> [621]
</pre>
=={{header|Ada}}==
===With sorting===
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Last_List_Item_Sorted is
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);
package Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Integer);
package Vector_Sorting is
new Vectors.Generic_Sorting;
use Ada.Containers, Vectors, Ada.Text_IO, Integer_IO;
procedure Put (List : Vector) is
begin
Put ("[");
for V of List loop
Put (" "); Put (V, Width => 0);
end loop;
Put ("]");
end Put;
List : Vector := 6 & 81 & 243 & 14 & 25 & 49 & 123 & 69 & 11;
begin
Default_Width := 0;
while List.Length >= 2 loop
Vector_Sorting.Sort (List);
Put (List);
declare
Small_1 : constant Integer := List (List.First_Index + 0);
Small_2 : constant Integer := List (List.First_Index + 1);
Sum : constant Integer := Small_1 + Small_2;
begin
List.Delete_First (2);
Put (". Smallest: "); Put (Small_1); Put (" and "); Put (Small_2);
Put (". Sum: "); Put (Sum); New_Line;
List.Append (Sum);
end;
end loop;
Put (List); New_Line;
end Last_List_Item_Sorted;</syntaxhighlight>
{{out}}
<pre>
[ 6 11 14 25 49 69 81 123 243]. Smallest: 6 and 11. Sum: 17
[ 14 17 25 49 69 81 123 243]. Smallest: 14 and 17. Sum: 31
[ 25 31 49 69 81 123 243]. Smallest: 25 and 31. Sum: 56
[ 49 56 69 81 123 243]. Smallest: 49 and 56. Sum: 105
[ 69 81 105 123 243]. Smallest: 69 and 81. Sum: 150
[ 105 123 150 243]. Smallest: 105 and 123. Sum: 228
[ 150 228 243]. Smallest: 150 and 228. Sum: 378
[ 243 378]. Smallest: 243 and 378. Sum: 621
[ 621]
</pre>
===Without sorting===
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
procedure Last_List_Item is
package Integer_IO is new Ada.Text_IO.Integer_IO (Integer);
package Element_Vectors is
new Ada.Containers.Vectors (Index_Type => Positive,
Element_Type => Integer);
use Ada.Containers, Element_Vectors, Ada.Text_IO, Integer_IO;
function Pick_Smallest (List : in out Vector) return Integer is
Value : Integer := Integer'Last;
begin
if List.Is_Empty then
raise Constraint_Error;
end if;
for V of List loop
Value := Integer'Min (Value, V);
end loop;
List.Delete (List.Find_Index (Value));
return Value;
end Pick_Smallest;
procedure Put (List : Vector) is
begin
Put ("[");
for V of List loop
Put (" "); Put (V, Width => 0);
end loop;
Put ("]");
end Put;
List : Vector := 6 & 81 & 243 & 14 & 25 & 49 & 123 & 69 & 11;
begin
Default_Width := 0;
while List.Length >= 2 loop
Put (List); Put (". Smallest: ");
declare
Small_1 : constant Integer := Pick_Smallest (List);
Small_2 : constant Integer := Pick_Smallest (List);
Sum : constant Integer := Small_1 + Small_2;
begin
Put (Small_1); Put (" and "); Put (Small_2);
Put (". Sum: "); Put (Sum); New_Line;
List.Append (Sum);
end;
end loop;
Put (List); New_Line;
end Last_List_Item;</syntaxhighlight>
{{out}}
<pre>
[ 6 81 243 14 25 49 123 69 11]. Smallest: 6 and 11. Sum: 17
[ 81 243 14 25 49 123 69 17]. Smallest: 14 and 17. Sum: 31
[ 81 243 25 49 123 69 31]. Smallest: 25 and 31. Sum: 56
[ 81 243 49 123 69 56]. Smallest: 49 and 56. Sum: 105
[ 81 243 123 69 105]. Smallest: 69 and 81. Sum: 150
[ 243 123 105 150]. Smallest: 105 and 123. Sum: 228
[ 243 150 228]. Smallest: 150 and 228. Sum: 378
[ 243 378]. Smallest: 243 and 378. Sum: 621
[ 621]
</pre>
Line 41 ⟶ 167:
{{trans|Wren}}
{{libheader|ALGOL 68-rows}}
<
# of the two smallest elements and removing them #
PR read "rows.incl.a68" PR # row related utilities #
Line 62 ⟶ 188:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</
{{out}}
<pre>
Line 78 ⟶ 204:
===Without sorting===
Translation of the sorted Wren version with the sorting removed.
<
# of the two smallest elements and removing them #
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
Line 109 ⟶ 235:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</
{{out}}
<pre>
Line 123 ⟶ 249:
</pre>
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 DATA 6, 81, 243, 14, 25, 49, 123, 69, 11
110 FOR L = 0 TO 8
120 READ L(L)
130 NEXT
140 LET L = L - 1
150 GOSUB 180"LAST LIST ITEM"
160 GOSUB 250"PRINT LIST"
170 END
180 IF L = 0 THEN RETURN
190 FOR L = L TO 0 STEP 0
200 GOSUB 340"FIND THE 2 SMALLEST"
210 LET L = L + 1
220 LET L(L) = PREVIOUSSMALLEST + SMALLEST
230 NEXT L
240 RETURN
250 PRINT
260 LET H = 1
270 FOR I = 0 TO L
280 IF NOT H(I) THEN H(I) = H
290 LET H = H(I) + LEN ( STR$ (L(I))) + 1
300 HTAB H(I)
310 PRINT L(I)" ";
320 NEXT I
330 RETURN
340 GOSUB 250"PRINT LIST"
350 GOSUB 370"FIND SMALLEST"
360 LET PREVIOUSSMALLEST = SMALLEST
370 LET S = 0
380 FOR I = 0 TO L
390 LET SMALLEST = L(S)
400 IF L(I) < SMALLEST THEN S = I
410 NEXT I
420 LET SMALLEST = L(S)
430 LET L = L - 1
440 IF S > L THEN RETURN
450 FOR I = S TO L
460 LET L(I) = L(I + 1)
470 LET H(I) = H(I + 1)
480 NEXT I
490 RETURN</syntaxhighlight>
{{out}}
<pre>
6 81 243 14 25 49 123 69 11
81 243 14 25 49 123 69 17
81 243 25 49 123 69 31
81 243 49 123 69 56
81 243 123 69 105
243 123 105 150
243 150 228
243 378
621
</pre>
=={{header|AppleScript}}==
<
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
Line 155 ⟶ 338:
end lastListItem
lastListItem({6, 81, 243, 14, 25, 49, 123, 69, 11})</
{{output}}
<
{6, 81, 243, 14, 25, 49, 123, 69, 11} ← Original
{81, 243, 14, 25, 49, 123, 69, 17} ← 6 + 11 = 17
Line 167 ⟶ 350:
{243, 150, 228} ← 105 + 123 = 228
{243, 378} ← 150 + 228 = 378
{621} ← 243 + 378 = 621"</
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">lst: [6 81 243 14 25 49 123 69 11]
initial: lst
while [1 < size lst][
remove 'lst min1: <= min lst
remove 'lst min2: <= min lst
append 'lst min1 + min2
print ["List:" lst]
]
print ""
print [initial "=>" lst]</syntaxhighlight>
{{out}}
<pre>List: [81 243 14 25 49 123 69 17]
List: [81 243 25 49 123 69 31]
List: [81 243 49 123 69 56]
List: [81 243 123 69 105]
List: [243 123 105 150]
List: [243 150 228]
List: [243 378]
List: [621]
[6 81 243 14 25 49 123 69 11] => [621]</pre>
=={{header|AutoHotkey}}==
<
steps := "Initial List `t`t`t" List2str(List) "`n"
while List.Count() > 1
Line 200 ⟶ 410:
unsorted_List .= v ", "
return "[" Trim(unsorted_List, ", ") "]"
}</
{{out}}
<pre>Initial List [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 213 ⟶ 423:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LAST_LIST_ITEM.AWK
BEGIN {
Line 233 ⟶ 443:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 251 ⟶ 461:
===With and without sorting===
<
W_sort ← {{i←2↑∧𝕩, (𝕩/˜¬𝕩∊i)∾+´i}⍟(↕≠𝕩)𝕩}
Line 258 ⟶ 468:
•Show¨W_sort list
•Show¨WO_sort list</
<syntaxhighlight lang="text">⟨ 6 81 243 14 25 49 123 69 11 ⟩
⟨ 81 243 14 25 49 123 69 17 ⟩
⟨ 81 243 25 49 123 69 31 ⟩
Line 276 ⟶ 486:
⟨ 243 150 228 ⟩
⟨ 243 378 ⟩
⟨ 621 ⟩</
=={{header|C}}==
===With sorting===
{{trans|Wren}}
<
#include <stdlib.h>
Line 310 ⟶ 520:
printf("Last item is %d.\n", a[0]);
return 0;
}</
{{out}}
Line 334 ⟶ 544:
===Without sorting===
<
int findMin(int a[], int asize, int *pmin) {
Line 370 ⟶ 580:
printf("Last item is %d.\n", a[0]);
return 0;
}</
{{out}}
Line 392 ⟶ 602:
Last item is 621.
</pre>
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <list>
using namespace std;
void PrintContainer(const auto& container)
{
cout << "[ ";
for_each(container.begin(), container.end(), [](auto item){cout << item << " ";});
cout << "]";
}
int main()
{
list<int> numbers{6, 81, 243, 14, 25, 49, 123, 69, 11};
// a lambda to remove the minimum item
auto removeMin = [](auto& container)
{
auto minIterator = min_element(container.begin(), container.end());
auto minValue = *minIterator;
container.erase(minIterator);
return minValue;
};
while(numbers.size() > 1)
{
PrintContainer(numbers);
auto minValue = removeMin(numbers);
auto nextMinValue = removeMin(numbers);
auto sum = minValue + nextMinValue;
numbers.push_back(sum);
cout << " => " << minValue << " + " << nextMinValue << " = " << sum << "\n";
}
cout << "Final list: "; PrintContainer(numbers); cout << "\n";
}
</syntaxhighlight>
{{out}}
<pre>
[ 6 81 243 14 25 49 123 69 11 ] => 6 + 11 = 17
[ 81 243 14 25 49 123 69 17 ] => 14 + 17 = 31
[ 81 243 25 49 123 69 31 ] => 25 + 31 = 56
[ 81 243 49 123 69 56 ] => 49 + 56 = 105
[ 81 243 123 69 105 ] => 69 + 81 = 150
[ 243 123 105 150 ] => 105 + 123 = 228
[ 243 150 228 ] => 150 + 228 = 378
[ 243 378 ] => 243 + 378 = 621
Final list: [ 621 ]
</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Uses standard Delphi object "TList" to manipulate the array.
<syntaxhighlight lang="Delphi">
procedure FindSmallest(LS: TList; var Index,Value: Integer);
{Find smallest value in LIst}
var I: integer;
begin
Value:=High(integer);
for I:=0 to LS.Count-1 do
if integer(LS[I])<Value then
begin
Value:=integer(LS[I]);
Index:=I;
end;
end;
procedure ShowArray(Memo: TMemo; LS: TList);
{Display the contents of specified array}
var I: integer;
var S: string;
begin
S:='[';
for I:=0 to LS.Count-1 do
begin
if I>0 then S:=S+' ';
S:=S+IntToStr(Integer(LS[I]));
end;
S:=S+']';
Memo.Lines.Add(S);
end;
procedure LastItem(Memo: TMemo; IA: array of integer);
{Repeatedly remove the two lowest values}
{Add them together and add to the list}
var LS: TList;
var I,Inx,Val1,Val2: integer;
begin
LS:=TList.Create;
try
for I:=0 to High(IA) do LS.Add(Pointer(IA[I]));
while LS.Count>1 do
begin
ShowArray(Memo,LS);
FindSmallest(LS,Inx,Val1);
LS.Delete(Inx);
FindSmallest(LS,Inx,Val2);
LS.Delete(Inx);
LS.Add(Pointer(Val1 + Val2))
end;
Memo.Lines.Add(IntToStr(integer(LS[0])));
finally LS.Free; end;
end;
{Supplied test array}
const IntArray: array [0..8] of integer=(6, 81, 243, 14, 25, 49, 123, 69, 11);
procedure DoLastItem(Memo: TMemo);
{Do last item problem}
begin
LastItem(Memo,IntArray);
end;
</syntaxhighlight>
{{out}}
<pre>
[6 81 243 14 25 49 123 69 11]
[81 243 14 25 49 123 69 17]
[81 243 25 49 123 69 31]
[81 243 49 123 69 56]
[81 243 123 69 105]
[243 123 105 150]
[243 150 228]
[243 378]
621
</pre>
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
sequences sequences.extras ;
Line 413 ⟶ 760:
V{ 6 81 243 14 25 49 123 69 11 }
[ dup length 1 > ] [ dup list. step ] while
last "Last item is %d.\n" printf</
{{out}}
<pre>
Line 436 ⟶ 783:
=={{header|FreeBASIC}}==
<
redim as integer list(0 to 8)
Line 473 ⟶ 820:
next i
print
wend</
{{out}}<pre>
6 81 243 14 25 49 123 69 11
Line 489 ⟶ 836:
{{trans|Wren}}
===With sorting===
<
import (
Line 507 ⟶ 854:
}
fmt.Println("Last item is", a[0], "\b.")
}</
{{out}}
Line 531 ⟶ 878:
===Without sorting===
<
import "fmt"
Line 562 ⟶ 909:
}
fmt.Println("Last item is", a[0], "\b.")
}</
{{out}}
Line 584 ⟶ 931:
Last item is 621.
</pre>
=={{header|J}}==
Here we maintain the unsorted order in the intermediate steps:<syntaxhighlight lang="j"> rplc&(' 0';'')"1": ((-. , +/@]) 2 {. /:~)^:a: 81 243 14 25 49 123 69 17
81 243 14 25 49 123 69 17
81 243 25 49 123 69 31
81 243 49 123 69 56
81 243 123 69 105
243 123 105 150
243 150 228
243 378
621</syntaxhighlight>
Alternative interpretation:<syntaxhighlight lang="j"> ([[:echo,:#~1<#)@((-. , +/@]) 2 {. /:~)^:_] 81 243 14 25 49 123 69 17
81 243 25 49 123 69 31
81 243 49 123 69 56
81 243 123 69 105
243 123 105 150
243 150 228
243 378
621</syntaxhighlight>
=={{header|jq}}==
Line 590 ⟶ 957:
'''Works with gojq, the Go implementation of jq'''
===With sorting===
<
foreach range(1; length) as $i ( {a: .};
.a |= sort
Line 602 ⟶ 969:
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_with_sorting</
{{out}}
<pre>
Line 624 ⟶ 991:
</pre>
===Without sorting===
<
. as $in
| reduce range(0; length) as $i ( null;
Line 651 ⟶ 1,018:
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_without_sorting
</syntaxhighlight>
{{out}}
<pre>
Line 674 ⟶ 1,041:
=={{header|Julia}}==
<
function addleastreduce!(lis)
Line 685 ⟶ 1,052:
@show list, addleastreduce!(copy(list))
</
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 700 ⟶ 1,067:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===With and without sorting===
<
Print[list]
Line 712 ⟶ 1,079:
,
{Length[list] - 1}
]</
{{out}}
<pre>{6,81,243,14,25,49,123,69,11}
Line 728 ⟶ 1,095:
===With sorting===
We sort in descending order as it is more efficient.
<
import algorithm, strformat
Line 743 ⟶ 1,110:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</
{{out}}
Line 759 ⟶ 1,126:
We could use the function <code>minIndex</code> from module <code>sequtils</code> but it would be less efficient. We used a single loop instead. Please note that we remove the elements using function <code>del</code> which is O(1) rather than function <code>delete</code> which is O(n).
<
import strformat
Line 784 ⟶ 1,151:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</
{{out}}
Line 799 ⟶ 1,166:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 813 ⟶ 1,180:
printf " %3d ", $min;
$min;
}</
{{out}}
<pre> Original 6 81 243 14 25 49 123 69 11
Line 828 ⟶ 1,195:
=={{header|Phix}}==
===With sorting===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">81</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">243</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">123</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">69</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">}</span>
Line 838 ⟶ 1,205:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Last item is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">list</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 852 ⟶ 1,219:
</pre>
===Without sorting===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">81</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">243</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">123</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">69</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">}</span>
Line 868 ⟶ 1,235:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Last item is %d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">list</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 884 ⟶ 1,251:
=={{header|Python}}==
{{trans|Julia}}
<
def add_least_reduce(lis):
Line 896 ⟶ 1,263:
print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
</
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 908 ⟶ 1,275:
[6, 81, 243, 14, 25, 49, 123, 69, 11] ==> [621]
</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ behead
-1 swap rot witheach
[ 2dup > if
[ i^ swap
2swap drop ]
drop ]
drop 1+ ] is least ( [ --> n )
' [ 6 81 243 14 25 49 123 69 11 ]
[ dup echo cr
dup size 1 > while
dup least pluck
swap
dup least pluck
rot + join
again ]
0 peek echo
</syntaxhighlight>
{{out}}
<pre>[ 6 81 243 14 25 49 123 69 11 ]
[ 81 243 14 25 49 123 69 17 ]
[ 81 243 25 49 123 69 31 ]
[ 81 243 49 123 69 56 ]
[ 81 243 123 69 105 ]
[ 243 123 105 150 ]
[ 243 150 228 ]
[ 243 378 ]
[ 621 ]
621</pre>
=={{header|Raku}}==
Uses no sorting; does not modify overall list order while processing.
<syntaxhighlight lang="raku"
say push @list: get(min @list) + get(min @list) while @list > 1;
Line 919 ⟶ 1,321:
printf " %3d ", $min;
$min;
}</
{{out}}
<pre> Original [6 81 243 14 25 49 123 69 11]
Line 934 ⟶ 1,336:
=={{header|REXX}}==
===With sorting===
<
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Line 970 ⟶ 1,372:
swl=swl wa.i
End
Return strip(swl)</
{{out}}
<Pre>Sorted list: 6 11 14 25 49 69 81 123 243
Line 990 ⟶ 1,392:
Last item: 621</pre>
===Without sorting===
<
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Line 1,013 ⟶ 1,415:
End
list=subword(list,1,j-1) subword(list,j+1)
Return m</
{{out}}
<pre>List: 6 81 243 14 25 49 123 69 11
Line 1,036 ⟶ 1,438:
=={{header|Ring}}==
===With sorting===
<
see "working..." + nl
Line 1,086 ⟶ 1,488:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,111 ⟶ 1,513:
</pre>
===Without sorting===
<
see "working..." + nl
Line 1,155 ⟶ 1,557:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,178 ⟶ 1,580:
Last item is: 621
done...
</pre>
=={{header|RPL}}==
≪ '''WHILE''' DUP SIZE 1 > '''REPEAT'''
DUP LIST→ → len
≪ 0 1 '''FOR''' j
2 len j - '''START'''
len j - ROLL '''IF''' DUP2 < '''THEN''' SWAP '''END NEXT'''
len ROLLD
'''NEXT'''
len ROLL len ROLL + len 1 - →LIST
≫ '''END'''
≫ ''''LASTL'''' STO
{{in}}
<pre>
{ 6 11 14 25 49 69 81 123 243 } LASTL
</pre>
{{out}}
<pre>
9: { 6 11 14 25 49 69 81 123 243 }
8: { 243 123 14 25 49 69 81 17 }
7: { 243 123 81 25 49 69 31 }
6: { 243 123 81 69 49 56 }
5: { 243 123 81 69 105 }
4: { 243 123 105 150 }
3: { 243 150 228 }
2: { 243 378 }
1: { 621 }
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">p original = [6, 81, 243, 14, 25, 49, 123, 69, 11]
until original.size == 1 do
mins = original.min(2)
mins.each{|el| original.delete_at(original.index(el)) }
p original << mins.sum
end
</syntaxhighlight>
{{out}}
<pre>[6, 81, 243, 14, 25, 49, 123, 69, 11]
[81, 243, 14, 25, 49, 123, 69, 17]
[81, 243, 25, 49, 123, 69, 31]
[81, 243, 49, 123, 69, 56]
[81, 243, 123, 69, 105]
[243, 123, 105, 150]
[243, 150, 228]
[243, 378]
[621]
</pre>
=={{header|Sidef}}==
===With sorting===
<
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 1,189 ⟶ 1,639:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</
{{out}}
<pre>
Line 1,203 ⟶ 1,653:
===Without sorting===
<
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 1,217 ⟶ 1,667:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</
{{out}}
<pre>
Line 1,230 ⟶ 1,680:
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
===With sorting===
<syntaxhighlight lang="v (vlang)">fn main() {
mut a := [6, 81, 243, 14, 25, 49, 123, 69, 11]
for a.len > 1 {
Line 1,244 ⟶ 1,694:
}
println("Last item is ${a[0]}.")
}</
{{out}}
Line 1,268 ⟶ 1,718:
===Without sorting===
<syntaxhighlight lang="v (vlang)">fn find_min(a []int) (int, int) {
mut ix := 0
mut min := a[0]
Line 1,294 ⟶ 1,744:
}
println("Last item is ${a[0]}.")
}</
{{out}}
Line 1,319 ⟶ 1,769:
=={{header|Wren}}==
===With sorting===
<
while (a.count > 1) {
Line 1,330 ⟶ 1,780:
}
System.print("Last item is %(a[0]).")</
{{out}}
Line 1,354 ⟶ 1,804:
===Without sorting===
<
var ix = 0
var min = a[0]
Line 1,381 ⟶ 1,831:
}
System.print("Last item is %(a[0]).")</
{{out}}
Line 1,405 ⟶ 1,855:
=={{header|XPL0}}==
<
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
End:= 8; \last index
Line 1,426 ⟶ 1,876:
List(End):= Sum;
];
]</
{{out}}
|