Last list item: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}: alt) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
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 39:
=={{header|Ada}}==
===With sorting===
<
with Ada.Containers.Vectors;
Line 81:
end loop;
Put (List); New_Line;
end Last_List_Item_Sorted;</
{{out}}
<pre>
Line 96:
===Without sorting===
<
with Ada.Containers.Vectors;
Line 149:
end loop;
Put (List); New_Line;
end Last_List_Item;</
{{out}}
<pre>
Line 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 188:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</
{{out}}
<pre>
Line 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 235:
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</
{{out}}
<pre>
Line 250:
=={{header|Applesoft BASIC}}==
<
110 FOR L = 0 TO 8
120 READ L(L)
Line 293:
470 LET H(I) = H(I + 1)
480 NEXT I
490 RETURN</
{{out}}
<pre>
Line 307:
</pre>
=={{header|AppleScript}}==
<
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})</
{{output}}
<
{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"</
=={{header|AutoHotkey}}==
<
steps := "Initial List `t`t`t" List2str(List) "`n"
while List.Count() > 1
Line 383:
unsorted_List .= v ", "
return "[" Trim(unsorted_List, ", ") "]"
}</
{{out}}
<pre>Initial List [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 396:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LAST_LIST_ITEM.AWK
BEGIN {
Line 416:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 434:
===With and without sorting===
<
W_sort ← {{i←2↑∧𝕩, (𝕩/˜¬𝕩∊i)∾+´i}⍟(↕≠𝕩)𝕩}
Line 441:
•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 459:
⟨ 243 150 228 ⟩
⟨ 243 378 ⟩
⟨ 621 ⟩</
=={{header|C}}==
===With sorting===
{{trans|Wren}}
<
#include <stdlib.h>
Line 493:
printf("Last item is %d.\n", a[0]);
return 0;
}</
{{out}}
Line 517:
===Without sorting===
<
int findMin(int a[], int asize, int *pmin) {
Line 553:
printf("Last item is %d.\n", a[0]);
return 0;
}</
{{out}}
Line 578:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
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</
{{out}}
<pre>
Line 619:
=={{header|FreeBASIC}}==
<
redim as integer list(0 to 8)
Line 656:
next i
print
wend</
{{out}}<pre>
6 81 243 14 25 49 123 69 11
Line 672:
{{trans|Wren}}
===With sorting===
<
import (
Line 690:
}
fmt.Println("Last item is", a[0], "\b.")
}</
{{out}}
Line 714:
===Without sorting===
<
import "fmt"
Line 745:
}
fmt.Println("Last item is", a[0], "\b.")
}</
{{out}}
Line 769:
=={{header|J}}==
Here we maintain the unsorted order in the intermediate steps:<
81 243 14 25 49 123 69 17
81 243 25 49 123 69 31
Line 777:
243 150 228
243 378
621</
Alternative interpretation:<
81 243 25 49 123 69 31
81 243 49 123 69 56
Line 786:
243 150 228
243 378
621</
=={{header|jq}}==
Line 793:
'''Works with gojq, the Go implementation of jq'''
===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</
{{out}}
<pre>
Line 827:
</pre>
===Without sorting===
<
. as $in
| reduce range(0; length) as $i ( null;
Line 854:
[6, 81, 243, 14, 25, 49, 123, 69, 11]
| task_without_sorting
</syntaxhighlight>
{{out}}
<pre>
Line 877:
=={{header|Julia}}==
<
function addleastreduce!(lis)
Line 888:
@show list, addleastreduce!(copy(list))
</
<pre>
Interim list: [81, 243, 14, 25, 49, 123, 69, 17]
Line 903:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
===With and without sorting===
<
Print[list]
Line 915:
,
{Length[list] - 1}
]</
{{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.
<
import algorithm, strformat
Line 946:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</
{{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).
<
import strformat
Line 987:
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</
{{out}}
Line 1,002:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 1,016:
printf " %3d ", $min;
$min;
}</
{{out}}
<pre> Original 6 81 243 14 25 49 123 69 11
Line 1,031:
=={{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 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>
<!--</
{{out}}
<pre>
Line 1,055:
</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 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>
<!--</
{{out}}
<pre>
Line 1,087:
=={{header|Python}}==
{{trans|Julia}}
<
def add_least_reduce(lis):
Line 1,099:
print(LIST, ' ==> ', add_least_reduce(LIST.copy()))
</
<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"
say push @list: get(min @list) + get(min @list) while @list > 1;
Line 1,122:
printf " %3d ", $min;
$min;
}</
{{out}}
<pre> Original [6 81 243 14 25 49 123 69 11]
Line 1,137:
=={{header|REXX}}==
===With sorting===
<
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)</
{{out}}
<Pre>Sorted list: 6 11 14 25 49 69 81 123 243
Line 1,193:
Last item: 621</pre>
===Without sorting===
<
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</
{{out}}
<pre>List: 6 81 243 14 25 49 123 69 11
Line 1,239:
=={{header|Ring}}==
===With sorting===
<
see "working..." + nl
Line 1,289:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,314:
</pre>
===Without sorting===
<
see "working..." + nl
Line 1,358:
txt = txt + "]"
see txt + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,385:
=={{header|Sidef}}==
===With sorting===
<
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 1,392:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</
{{out}}
<pre>
Line 1,406:
===Without sorting===
<
var list = [6, 81, 243, 14, 25, 49, 123, 69, 11]
Line 1,420:
list << a.sum
say "#{a.map{'%3s' % _}.join(' ')} : #{list}"
}</
{{out}}
<pre>
Line 1,436:
{{trans|Go}}
===With sorting===
<
mut a := [6, 81, 243, 14, 25, 49, 123, 69, 11]
for a.len > 1 {
Line 1,447:
}
println("Last item is ${a[0]}.")
}</
{{out}}
Line 1,471:
===Without sorting===
<
mut ix := 0
mut min := a[0]
Line 1,497:
}
println("Last item is ${a[0]}.")
}</
{{out}}
Line 1,522:
=={{header|Wren}}==
===With sorting===
<
while (a.count > 1) {
Line 1,533:
}
System.print("Last item is %(a[0]).")</
{{out}}
Line 1,557:
===Without sorting===
<
var ix = 0
var min = a[0]
Line 1,584:
}
System.print("Last item is %(a[0]).")</
{{out}}
Line 1,608:
=={{header|XPL0}}==
<
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
End:= 8; \last index
Line 1,629:
List(End):= Sum;
];
]</
{{out}}
|