Last list item: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 12:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F add_least_reduce(&lis)
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(&copy(LIST)))</langsyntaxhighlight>
 
{{out}}
Line 39:
=={{header|Ada}}==
===With sorting===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
 
Line 81:
end loop;
Put (List); New_Line;
end Last_List_Item_Sorted;</langsyntaxhighlight>
{{out}}
<pre>
Line 96:
 
===Without sorting===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Containers.Vectors;
 
Line 149:
end loop;
Put (List); New_Line;
end Last_List_Item;</langsyntaxhighlight>
{{out}}
<pre>
Line 167:
{{trans|Wren}}
{{libheader|ALGOL 68-rows}}
<langsyntaxhighlight lang="algol68">BEGIN # find the last element after repeatedely adding the sum #
# of the two smallest elements and removing them #
PR read "rows.incl.a68" PR # row related utilities #
Line 188:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 204:
===Without sorting===
Translation of the sorted Wren version with the sorting removed.
<langsyntaxhighlight lang="algol68">BEGIN # find the last element after repeatedly adding the sum #
# of the two smallest elements and removing them #
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
Line 235:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 250:
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="gwbasic"> 100 DATA 6, 81, 243, 14, 25, 49, 123, 69, 11
110 FOR L = 0 TO 8
120 READ L(L)
Line 293:
470 LET H(I) = H(I + 1)
480 NEXT I
490 RETURN</langsyntaxhighlight>
{{out}}
<pre>
Line 307:
</pre>
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on lastListItem(lst)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
Line 338:
end lastListItem
 
lastListItem({6, 81, 243, 14, 25, 49, 123, 69, 11})</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Steps:
{6, 81, 243, 14, 25, 49, 123, 69, 11} ← Original
{81, 243, 14, 25, 49, 123, 69, 17} ← 6 + 11 = 17
Line 350:
{243, 150, 228} ← 105 + 123 = 228
{243, 378} ← 150 + 228 = 378
{621} ← 243 + 378 = 621"</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">List := [6, 81, 243, 14, 25, 49, 123, 69, 11]
steps := "Initial List `t`t`t" List2str(List) "`n"
while List.Count() > 1
Line 383:
unsorted_List .= v ", "
return "[" Trim(unsorted_List, ", ") "]"
}</langsyntaxhighlight>
{{out}}
<pre>Initial List [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 396:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LAST_LIST_ITEM.AWK
BEGIN {
Line 416:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 434:
===With and without sorting===
 
<langsyntaxhighlight lang="bqn">list ← ⟨6, 81, 243, 14, 25, 49, 123, 69, 11⟩
 
W_sort ← {{i←2↑∧𝕩, (𝕩/˜¬𝕩∊i)∾+´i}⍟(↕≠𝕩)𝕩}
Line 441:
 
•Show¨W_sort list
•Show¨WO_sort list</langsyntaxhighlight>
<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 459:
⟨ 243 150 228 ⟩
⟨ 243 378 ⟩
⟨ 621 ⟩</langsyntaxhighlight>
 
=={{header|C}}==
===With sorting===
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 493:
printf("Last item is %d.\n", a[0]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 517:
 
===Without sorting===
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int findMin(int a[], int asize, int *pmin) {
Line 553:
printf("Last item is %d.\n", a[0]);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 578:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.statistics prettyprint
sequences sequences.extras ;
 
Line 596:
V{ 6 81 243 14 25 49 123 69 11 }
[ dup length 1 > ] [ dup list. step ] while
last "Last item is %d.\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 619:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define HUGE 99999999
 
redim as integer list(0 to 8)
Line 656:
next i
print
wend</langsyntaxhighlight>
{{out}}<pre>
6 81 243 14 25 49 123 69 11
Line 672:
{{trans|Wren}}
===With sorting===
<langsyntaxhighlight lang="go">package main
 
import (
Line 690:
}
fmt.Println("Last item is", a[0], "\b.")
}</langsyntaxhighlight>
 
{{out}}
Line 714:
 
===Without sorting===
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 745:
}
fmt.Println("Last item is", a[0], "\b.")
}</langsyntaxhighlight>
 
{{out}}
Line 769:
 
=={{header|J}}==
Here we maintain the unsorted order in the intermediate steps:<langsyntaxhighlight Jlang="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
Line 777:
243 150 228
243 378
621</langsyntaxhighlight>
 
Alternative interpretation:<langsyntaxhighlight Jlang="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
Line 786:
243 150 228
243 378
621</langsyntaxhighlight>
 
=={{header|jq}}==
Line 793:
'''Works with gojq, the Go implementation of jq'''
===With sorting===
<langsyntaxhighlight lang="jq">def task_with_sorting:
foreach range(1; length) as $i ( {a: .};
.a |= sort
Line 805:
 
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_with_sorting</langsyntaxhighlight>
{{out}}
<pre>
Line 827:
</pre>
===Without sorting===
<langsyntaxhighlight lang="jq">def min_index:
. as $in
| reduce range(0; length) as $i ( null;
Line 854:
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_without_sorting
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 877:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
function addleastreduce!(lis)
Line 888:
 
@show list, addleastreduce!(copy(list))
</langsyntaxhighlight>{{out}}
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 903:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===With and without sorting===
<langsyntaxhighlight Mathematicalang="mathematica">list = {6, 81, 243, 14, 25, 49, 123, 69, 11};
 
Print[list]
Line 915:
,
{Length[list] - 1}
]</langsyntaxhighlight>
{{out}}
<pre>{6,81,243,14,25,49,123,69,11}
Line 931:
===With sorting===
We sort in descending order as it is more efficient.
<langsyntaxhighlight Nimlang="nim"># With sorting.
import algorithm, strformat
 
Line 946:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</langsyntaxhighlight>
 
{{out}}
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).
 
<langsyntaxhighlight Nimlang="nim"># Without sorting.
import strformat
 
Line 987:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</langsyntaxhighlight>
 
{{out}}
Line 1,002:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,016:
printf " %3d ", $min;
$min;
}</langsyntaxhighlight>
{{out}}
<pre> Original 6 81 243 14 25 49 123 69 11
Line 1,031:
=={{header|Phix}}==
===With sorting===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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 1,041:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,055:
</pre>
===Without sorting===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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 1,071:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,087:
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">""" Rosetta code task: Last list item """
 
def add_least_reduce(lis):
Line 1,099:
 
print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
</langsyntaxhighlight>{{out}}
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 1,114:
=={{header|Raku}}==
Uses no sorting; does not modify overall list order while processing.
<syntaxhighlight lang="raku" perl6line>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;
Line 1,122:
printf " %3d ", $min;
$min;
}</langsyntaxhighlight>
{{out}}
<pre> Original [6 81 243 14 25 49 123 69 11]
Line 1,137:
=={{header|REXX}}==
===With sorting===
<langsyntaxhighlight lang="rexx">/* REXX */
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Line 1,173:
swl=swl wa.i
End
Return strip(swl)</langsyntaxhighlight>
{{out}}
<Pre>Sorted list: 6 11 14 25 49 69 81 123 243
Line 1,193:
Last item: 621</pre>
===Without sorting===
<langsyntaxhighlight lang="rexx">/* REXX */
List = '6 81 243 14 25 49 123 69 11'
Do Until words(list)=1
Line 1,216:
End
list=subword(list,1,j-1) subword(list,j+1)
Return m</langsyntaxhighlight>
{{out}}
<pre>List: 6 81 243 14 25 49 123 69 11
Line 1,239:
=={{header|Ring}}==
===With sorting===
<langsyntaxhighlight lang="ring">
see "working..." + nl
 
Line 1,289:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,314:
</pre>
===Without sorting===
<langsyntaxhighlight lang="ring">
see "working..." + nl
Line 1,358:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,385:
=={{header|Sidef}}==
===With sorting===
<langsyntaxhighlight lang="ruby">var k = 2
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
Line 1,392:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,406:
 
===Without sorting===
<langsyntaxhighlight lang="ruby">var k = 2
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
Line 1,420:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,436:
{{trans|Go}}
===With sorting===
<langsyntaxhighlight lang="vlang">fn main() {
mut a := [6, 81, 243, 14, 25, 49, 123, 69, 11]
for a.len > 1 {
Line 1,447:
}
println("Last item is ${a[0]}.")
}</langsyntaxhighlight>
 
{{out}}
Line 1,471:
 
===Without sorting===
<langsyntaxhighlight lang="vlang">fn find_min(a []int) (int, int) {
mut ix := 0
mut min := a[0]
Line 1,497:
}
println("Last item is ${a[0]}.")
}</langsyntaxhighlight>
 
{{out}}
Line 1,522:
=={{header|Wren}}==
===With sorting===
<langsyntaxhighlight lang="ecmascript">var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
while (a.count > 1) {
Line 1,533:
}
 
System.print("Last item is %(a[0]).")</langsyntaxhighlight>
 
{{out}}
Line 1,557:
 
===Without sorting===
<langsyntaxhighlight lang="ecmascript">var findMin = Fn.new { |a|
var ix = 0
var min = a[0]
Line 1,584:
}
 
System.print("Last item is %(a[0]).")</langsyntaxhighlight>
 
{{out}}
Line 1,608:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int List, End, Sum, Item, Smallest, I, SI;
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
End:= 8; \last index
Line 1,629:
List(End):= Sum;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits