Last list item: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 12: Line 12:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F add_least_reduce(&lis)
<syntaxhighlight lang="11l">F add_least_reduce(&lis)
L lis.len > 1
L lis.len > 1
V el1 = lis.pop(lis.index(min(lis)))
V el1 = lis.pop(lis.index(min(lis)))
Line 22: Line 22:
V LIST = [6, 81, 243, 14, 25, 49, 123, 69, 11]
V LIST = [6, 81, 243, 14, 25, 49, 123, 69, 11]


print(LIST‘ ==> ’add_least_reduce(&copy(LIST)))</lang>
print(LIST‘ ==> ’add_least_reduce(&copy(LIST)))</syntaxhighlight>


{{out}}
{{out}}
Line 39: Line 39:
=={{header|Ada}}==
=={{header|Ada}}==
===With sorting===
===With sorting===
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Vectors;


Line 81: Line 81:
end loop;
end loop;
Put (List); New_Line;
Put (List); New_Line;
end Last_List_Item_Sorted;</lang>
end Last_List_Item_Sorted;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 96: Line 96:


===Without sorting===
===Without sorting===
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Vectors;


Line 149: Line 149:
end loop;
end loop;
Put (List); New_Line;
Put (List); New_Line;
end Last_List_Item;</lang>
end Last_List_Item;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 167: Line 167:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|ALGOL 68-rows}}
{{libheader|ALGOL 68-rows}}
<lang algol68>BEGIN # find the last element after repeatedely adding the sum #
<syntaxhighlight 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 #
PR read "rows.incl.a68" PR # row related utilities #
PR read "rows.incl.a68" PR # row related utilities #
Line 188: Line 188:
OD;
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 204: Line 204:
===Without sorting===
===Without sorting===
Translation of the sorted Wren version with the sorting removed.
Translation of the sorted Wren version with the sorting removed.
<lang algol68>BEGIN # find the last element after repeatedly adding the sum #
<syntaxhighlight lang="algol68">BEGIN # find the last element after repeatedly 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 235: Line 235:
OD;
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 250: Line 250:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 DATA 6, 81, 243, 14, 25, 49, 123, 69, 11
<syntaxhighlight lang="gwbasic"> 100 DATA 6, 81, 243, 14, 25, 49, 123, 69, 11
110 FOR L = 0 TO 8
110 FOR L = 0 TO 8
120 READ L(L)
120 READ L(L)
Line 293: Line 293:
470 LET H(I) = H(I + 1)
470 LET H(I) = H(I + 1)
480 NEXT I
480 NEXT I
490 RETURN</lang>
490 RETURN</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 307: Line 307:
</pre>
</pre>
=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>on lastListItem(lst)
<syntaxhighlight lang="applescript">on lastListItem(lst)
set astid to AppleScript's text item delimiters
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set AppleScript's text item delimiters to ", "
Line 338: Line 338:
end lastListItem
end lastListItem


lastListItem({6, 81, 243, 14, 25, 49, 123, 69, 11})</lang>
lastListItem({6, 81, 243, 14, 25, 49, 123, 69, 11})</syntaxhighlight>


{{output}}
{{output}}
<lang applescript>"Steps:
<syntaxhighlight lang="applescript">"Steps:
{6, 81, 243, 14, 25, 49, 123, 69, 11} ← Original
{6, 81, 243, 14, 25, 49, 123, 69, 11} ← Original
{81, 243, 14, 25, 49, 123, 69, 17} ← 6 + 11 = 17
{81, 243, 14, 25, 49, 123, 69, 17} ← 6 + 11 = 17
Line 350: Line 350:
{243, 150, 228} ← 105 + 123 = 228
{243, 150, 228} ← 105 + 123 = 228
{243, 378} ← 150 + 228 = 378
{243, 378} ← 150 + 228 = 378
{621} ← 243 + 378 = 621"</lang>
{621} ← 243 + 378 = 621"</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>List := [6, 81, 243, 14, 25, 49, 123, 69, 11]
<syntaxhighlight lang="autohotkey">List := [6, 81, 243, 14, 25, 49, 123, 69, 11]
steps := "Initial List `t`t`t" List2str(List) "`n"
steps := "Initial List `t`t`t" List2str(List) "`n"
while List.Count() > 1
while List.Count() > 1
Line 383: Line 383:
unsorted_List .= v ", "
unsorted_List .= v ", "
return "[" Trim(unsorted_List, ", ") "]"
return "[" Trim(unsorted_List, ", ") "]"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Initial List [6, 81, 243, 14, 25, 49, 123, 69, 11]
<pre>Initial List [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 396: Line 396:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LAST_LIST_ITEM.AWK
# syntax: GAWK -f LAST_LIST_ITEM.AWK
BEGIN {
BEGIN {
Line 416: Line 416:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 434: Line 434:
===With and without sorting===
===With and without sorting===


<lang bqn>list ← ⟨6, 81, 243, 14, 25, 49, 123, 69, 11⟩
<syntaxhighlight lang="bqn">list ← ⟨6, 81, 243, 14, 25, 49, 123, 69, 11⟩


W_sort ← {{i←2↑∧𝕩, (𝕩/˜¬𝕩∊i)∾+´i}⍟(↕≠𝕩)𝕩}
W_sort ← {{i←2↑∧𝕩, (𝕩/˜¬𝕩∊i)∾+´i}⍟(↕≠𝕩)𝕩}
Line 441: Line 441:


•Show¨W_sort list
•Show¨W_sort list
•Show¨WO_sort list</lang>
•Show¨WO_sort list</syntaxhighlight>
<lang>⟨ 6 81 243 14 25 49 123 69 11 ⟩
<syntaxhighlight lang="text">⟨ 6 81 243 14 25 49 123 69 11 ⟩
⟨ 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 25 49 123 69 31 ⟩
Line 459: Line 459:
⟨ 243 150 228 ⟩
⟨ 243 150 228 ⟩
⟨ 243 378 ⟩
⟨ 243 378 ⟩
⟨ 621 ⟩</lang>
⟨ 621 ⟩</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
===With sorting===
===With sorting===
{{trans|Wren}}
{{trans|Wren}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 493: Line 493:
printf("Last item is %d.\n", a[0]);
printf("Last item is %d.\n", a[0]);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 517: Line 517:


===Without sorting===
===Without sorting===
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int findMin(int a[], int asize, int *pmin) {
int findMin(int a[], int asize, int *pmin) {
Line 553: Line 553:
printf("Last item is %d.\n", a[0]);
printf("Last item is %d.\n", a[0]);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 578: Line 578:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting io kernel math math.statistics prettyprint
<syntaxhighlight lang="factor">USING: formatting io kernel math math.statistics prettyprint
sequences sequences.extras ;
sequences sequences.extras ;


Line 596: Line 596:
V{ 6 81 243 14 25 49 123 69 11 }
V{ 6 81 243 14 25 49 123 69 11 }
[ dup length 1 > ] [ dup list. step ] while
[ dup length 1 > ] [ dup list. step ] while
last "Last item is %d.\n" printf</lang>
last "Last item is %d.\n" printf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 619: Line 619:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#define HUGE 99999999
<syntaxhighlight lang="freebasic">#define HUGE 99999999


redim as integer list(0 to 8)
redim as integer list(0 to 8)
Line 656: Line 656:
next i
next i
print
print
wend</lang>
wend</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
6 81 243 14 25 49 123 69 11
6 81 243 14 25 49 123 69 11
Line 672: Line 672:
{{trans|Wren}}
{{trans|Wren}}
===With sorting===
===With sorting===
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 690: Line 690:
}
}
fmt.Println("Last item is", a[0], "\b.")
fmt.Println("Last item is", a[0], "\b.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 714: Line 714:


===Without sorting===
===Without sorting===
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 745: Line 745:
}
}
fmt.Println("Last item is", a[0], "\b.")
fmt.Println("Last item is", a[0], "\b.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 769: Line 769:


=={{header|J}}==
=={{header|J}}==
Here we maintain the unsorted order in the intermediate steps:<lang J> rplc&(' 0';'')"1": ((-. , +/@]) 2 {. /:~)^:a: 81 243 14 25 49 123 69 17
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 14 25 49 123 69 17
81 243 25 49 123 69 31
81 243 25 49 123 69 31
Line 777: Line 777:
243 150 228
243 150 228
243 378
243 378
621</lang>
621</syntaxhighlight>


Alternative interpretation:<lang J> ([[:echo,:#~1<#)@((-. , +/@]) 2 {. /:~)^:_] 81 243 14 25 49 123 69 17
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 25 49 123 69 31
81 243 49 123 69 56
81 243 49 123 69 56
Line 786: Line 786:
243 150 228
243 150 228
243 378
243 378
621</lang>
621</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 793: Line 793:
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
===With sorting===
===With sorting===
<lang jq>def task_with_sorting:
<syntaxhighlight lang="jq">def task_with_sorting:
foreach range(1; length) as $i ( {a: .};
foreach range(1; length) as $i ( {a: .};
.a |= sort
.a |= sort
Line 805: Line 805:


[6, 81, 243, 14, 25, 49, 123, 69, 11]
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_with_sorting</lang>
| task_with_sorting</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 827: Line 827:
</pre>
</pre>
===Without sorting===
===Without sorting===
<lang jq>def min_index:
<syntaxhighlight lang="jq">def min_index:
. as $in
. as $in
| reduce range(0; length) as $i ( null;
| reduce range(0; length) as $i ( null;
Line 854: Line 854:
[6, 81, 243, 14, 25, 49, 123, 69, 11]
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_without_sorting
| task_without_sorting
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 877: Line 877:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
<syntaxhighlight lang="julia">list = [6, 81, 243, 14, 25, 49, 123, 69, 11]


function addleastreduce!(lis)
function addleastreduce!(lis)
Line 888: Line 888:


@show list, addleastreduce!(copy(list))
@show list, addleastreduce!(copy(list))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 903: Line 903:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===With and without sorting===
===With and without sorting===
<lang Mathematica>list = {6, 81, 243, 14, 25, 49, 123, 69, 11};
<syntaxhighlight lang="mathematica">list = {6, 81, 243, 14, 25, 49, 123, 69, 11};


Print[list]
Print[list]
Line 915: Line 915:
,
,
{Length[list] - 1}
{Length[list] - 1}
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>{6,81,243,14,25,49,123,69,11}
<pre>{6,81,243,14,25,49,123,69,11}
Line 931: Line 931:
===With sorting===
===With sorting===
We sort in descending order as it is more efficient.
We sort in descending order as it is more efficient.
<lang Nim># With sorting.
<syntaxhighlight lang="nim"># With sorting.
import algorithm, strformat
import algorithm, strformat


Line 946: Line 946:
while list.len >= 2:
while list.len >= 2:
list.extractAndAddTwoSmallest()
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</lang>
echo &"Last item is {list[0]}."</syntaxhighlight>


{{out}}
{{out}}
Line 962: Line 962:
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).
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).


<lang Nim># Without sorting.
<syntaxhighlight lang="nim"># Without sorting.
import strformat
import strformat


Line 987: Line 987:
while list.len >= 2:
while list.len >= 2:
list.extractAndAddTwoSmallest()
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</lang>
echo &"Last item is {list[0]}."</syntaxhighlight>


{{out}}
{{out}}
Line 1,002: Line 1,002:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,016: Line 1,016:
printf " %3d ", $min;
printf " %3d ", $min;
$min;
$min;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> Original 6 81 243 14 25 49 123 69 11
<pre> Original 6 81 243 14 25 49 123 69 11
Line 1,031: Line 1,031:
=={{header|Phix}}==
=={{header|Phix}}==
===With sorting===
===With sorting===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 1,041: Line 1,041:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,055: Line 1,055:
</pre>
</pre>
===Without sorting===
===Without sorting===
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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 1,071: Line 1,071:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,087: Line 1,087:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Julia}}
{{trans|Julia}}
<lang python>""" Rosetta code task: Last list item """
<syntaxhighlight lang="python">""" Rosetta code task: Last list item """


def add_least_reduce(lis):
def add_least_reduce(lis):
Line 1,099: Line 1,099:


print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 1,114: Line 1,114:
=={{header|Raku}}==
=={{header|Raku}}==
Uses no sorting; does not modify overall list order while processing.
Uses no sorting; does not modify overall list order while processing.
<lang perl6>say ' Original ', my @list = 6, 81, 243, 14, 25, 49, 123, 69, 11;
<syntaxhighlight lang="raku" line>say ' Original ', my @list = 6, 81, 243, 14, 25, 49, 123, 69, 11;


say push @list: get(min @list) + get(min @list) while @list > 1;
say push @list: get(min @list) + get(min @list) while @list > 1;
Line 1,122: Line 1,122:
printf " %3d ", $min;
printf " %3d ", $min;
$min;
$min;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> Original [6 81 243 14 25 49 123 69 11]
<pre> Original [6 81 243 14 25 49 123 69 11]
Line 1,137: Line 1,137:
=={{header|REXX}}==
=={{header|REXX}}==
===With sorting===
===With sorting===
<lang rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
List = '6 81 243 14 25 49 123 69 11'
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Do Until words(list)=1
Line 1,173: Line 1,173:
swl=swl wa.i
swl=swl wa.i
End
End
Return strip(swl)</lang>
Return strip(swl)</syntaxhighlight>
{{out}}
{{out}}
<Pre>Sorted list: 6 11 14 25 49 69 81 123 243
<Pre>Sorted list: 6 11 14 25 49 69 81 123 243
Line 1,193: Line 1,193:
Last item: 621</pre>
Last item: 621</pre>
===Without sorting===
===Without sorting===
<lang rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
List = '6 81 243 14 25 49 123 69 11'
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Do Until words(list)=1
Line 1,216: Line 1,216:
End
End
list=subword(list,1,j-1) subword(list,j+1)
list=subword(list,1,j-1) subword(list,j+1)
Return m</lang>
Return m</syntaxhighlight>
{{out}}
{{out}}
<pre>List: 6 81 243 14 25 49 123 69 11
<pre>List: 6 81 243 14 25 49 123 69 11
Line 1,239: Line 1,239:
=={{header|Ring}}==
=={{header|Ring}}==
===With sorting===
===With sorting===
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl


Line 1,289: Line 1,289:
txt = txt + "]"
txt = txt + "]"
see txt + nl
see txt + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,314: Line 1,314:
</pre>
</pre>
===Without sorting===
===Without sorting===
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
Line 1,358: Line 1,358:
txt = txt + "]"
txt = txt + "]"
see txt + nl
see txt + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,385: Line 1,385:
=={{header|Sidef}}==
=={{header|Sidef}}==
===With sorting===
===With sorting===
<lang ruby>var k = 2
<syntaxhighlight lang="ruby">var k = 2
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]


Line 1,392: Line 1,392:
list << a.sum
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,406: Line 1,406:


===Without sorting===
===Without sorting===
<lang ruby>var k = 2
<syntaxhighlight lang="ruby">var k = 2
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]


Line 1,420: Line 1,420:
list << a.sum
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,436: Line 1,436:
{{trans|Go}}
{{trans|Go}}
===With sorting===
===With sorting===
<lang vlang>fn main() {
<syntaxhighlight lang="vlang">fn main() {
mut a := [6, 81, 243, 14, 25, 49, 123, 69, 11]
mut a := [6, 81, 243, 14, 25, 49, 123, 69, 11]
for a.len > 1 {
for a.len > 1 {
Line 1,447: Line 1,447:
}
}
println("Last item is ${a[0]}.")
println("Last item is ${a[0]}.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,471: Line 1,471:


===Without sorting===
===Without sorting===
<lang vlang>fn find_min(a []int) (int, int) {
<syntaxhighlight lang="vlang">fn find_min(a []int) (int, int) {
mut ix := 0
mut ix := 0
mut min := a[0]
mut min := a[0]
Line 1,497: Line 1,497:
}
}
println("Last item is ${a[0]}.")
println("Last item is ${a[0]}.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,522: Line 1,522:
=={{header|Wren}}==
=={{header|Wren}}==
===With sorting===
===With sorting===
<lang ecmascript>var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]
<syntaxhighlight lang="ecmascript">var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]


while (a.count > 1) {
while (a.count > 1) {
Line 1,533: Line 1,533:
}
}


System.print("Last item is %(a[0]).")</lang>
System.print("Last item is %(a[0]).")</syntaxhighlight>


{{out}}
{{out}}
Line 1,557: Line 1,557:


===Without sorting===
===Without sorting===
<lang ecmascript>var findMin = Fn.new { |a|
<syntaxhighlight lang="ecmascript">var findMin = Fn.new { |a|
var ix = 0
var ix = 0
var min = a[0]
var min = a[0]
Line 1,584: Line 1,584:
}
}


System.print("Last item is %(a[0]).")</lang>
System.print("Last item is %(a[0]).")</syntaxhighlight>


{{out}}
{{out}}
Line 1,608: Line 1,608:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int List, End, Sum, Item, Smallest, I, SI;
<syntaxhighlight lang="xpl0">int List, End, Sum, Item, Smallest, I, SI;
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
End:= 8; \last index
End:= 8; \last index
Line 1,629: Line 1,629:
List(End):= Sum;
List(End):= Sum;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}