Last list item: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Added Go)
Line 40: Line 40:
504
504
done...
done...
</pre>

=={{header|Wren}}==
<lang ecmascript>var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]

while (a.count > 1) {
a.sort()
a.add(a[0] + a[1])
a = a[2..-1]
}

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

{{out}}
<pre>
Last item is 621.
</pre>
</pre>

Revision as of 08:10, 23 October 2021

Last list item is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

List = [6, 81, 243, 14, 25, 49, 123, 69, 11]
Find two smallest items, summarize them, add to the end of list and delete them.
Repeat it until list contains one element.
Show the last item on this page.

Ring

<lang ring> see "working..." + nl see "Last item is:" + nl

List = [6,81,243,14,25,49,123,69,11] Temp = []

while true

     Temp = sort(List)
     first = Temp[1]
     second = Temp[2]
     ind1 = find(List,first)
     ind2 = find(List,second)
     del(List,ind2)
     del(List,ind1)
     sum = first + second 
     add(List,sum)
     if len(List) = 1
        exit
     ok

end

see "" + List[1] + nl see "done..." + nl </lang>

Output:
working...
Last item is:
504
done...

Wren

<lang ecmascript>var a = [6, 81, 243, 14, 25, 49, 123, 69, 11]

while (a.count > 1) {

   a.sort()
   a.add(a[0] + a[1])
   a = a[2..-1]

}

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

Output:
Last item is 621.