Last list item: Difference between revisions

Added Easylang
m (→‎{{header|Quackery}}: tweaked code)
(Added Easylang)
 
(7 intermediate revisions by 6 users not shown)
Line 351:
{243, 378} ← 150 + 228 = 378
{621} ← 243 + 378 = 621"</syntaxhighlight>
 
=={{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}}==
Line 575 ⟶ 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|EasyLang}}==
<syntaxhighlight>
l[] = [ 6 81 243 14 25 49 123 69 11 ]
while len l[] > 1
sum = 0
for k to 2
for i to len l[] - 1
if l[i] < l[$]
swap l[i] l[$]
.
.
sum += l[$]
len l[] -1
.
l[] &= sum
print l[]
.
</syntaxhighlight>
{{out}}
<pre>
[ 69 81 243 14 25 49 123 17 ]
[ 123 81 243 69 25 49 31 ]
[ 123 81 243 69 49 56 ]
[ 123 81 243 69 105 ]
[ 123 105 243 150 ]
[ 243 150 228 ]
[ 243 378 ]
[ 621 ]
</pre>
 
 
=={{header|Factor}}==
Line 1,416 ⟶ 1,611:
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>
 
Line 1,557 ⟶ 1,800:
=={{header|Wren}}==
===With sorting===
<syntaxhighlight lang="ecmascriptwren">var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]
 
while (a.count > 1) {
Line 1,592 ⟶ 1,835:
 
===Without sorting===
<syntaxhighlight lang="ecmascriptwren">var findMin = Fn.new { |a|
var ix = 0
var min = a[0]
1,983

edits