Last list item: Difference between revisions
Content added Content deleted
(Added Go) |
(Added Go) |
||
Line 7: | Line 7: | ||
<br>Show the last item on this page. |
<br>Show the last item on this page. |
||
<br><br> |
<br><br> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"sort" |
|||
) |
|||
func main() { |
|||
a := []int{6, 81, 243, 14, 25, 49, 123, 69, 11} |
|||
for len(a) > 1 { |
|||
sort.Ints(a) |
|||
a = append(a, a[0]+a[1]) |
|||
a = a[2:] |
|||
} |
|||
fmt.Println("Last item is", a[0], "\b.") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Last item is 621. |
|||
</pre> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
Revision as of 08:18, 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.
Go
<lang go>package main
import (
"fmt" "sort"
)
func main() {
a := []int{6, 81, 243, 14, 25, 49, 123, 69, 11} for len(a) > 1 { sort.Ints(a) a = append(a, a[0]+a[1]) a = a[2:] } fmt.Println("Last item is", a[0], "\b.")
}</lang>
- Output:
Last item is 621.
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.