Last list item: Difference between revisions

Content added Content deleted
Line 490: Line 490:


=={{header|Ring}}==
=={{header|Ring}}==
===Without sorting===
===With sorting===
<lang ring>
<lang ring>
see "working..." + nl
see "working..." + nl

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

while true
while true
n++
n++
if n = 1
List = sort(List)
see nl + "List = "
first = List[1]
showArray(List)
second = List[2]
see nl
ok
first = min(List)
ind1 = find(List,first)
ind1 = find(List,first)
del(List,ind1)
second = min(List)
ind2 = find(List,second)
ind2 = find(List,second)
del(List,ind2)
if ind1 < ind2
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
del(List,ind2)
ok
sum = first + second
sum = first + second
add(List,sum)
add(List,sum)
if len(List) = 1
if len(List) = 1
exit
exit
ok
if n = 1
see nl + "List = "
showArray(List)
see nl
ok
ok
showList(first,second,sum,List)
showList(first,second,sum,List)
end
end

see "Last item is: " +List[1] + nl
see "Last item is: " +List[1] + nl
see "done..." + nl
see "done..." + nl

func showList(first,second,sum,List)
func showList(first,second,sum,List)
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "List = "
see "List = "
showArray(List)
showArray(List)

func showArray(array)
func showArray(array)
txt = ""
txt = ""
Line 540: Line 546:
working...
working...


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


two smallest is = 6 + 11 = 17
two smallest is = 6 + 11 = 17
List = [81,243,14,25,49,123,69,17]
List = [14,25,49,69,81,123,243,17]
two smallest is = 14 + 17 = 31
two smallest is = 14 + 17 = 31
List = [81,243,25,49,123,69,31]
List = [25,49,69,81,123,243,31]
two smallest is = 25 + 31 = 56
two smallest is = 25 + 31 = 56
List = [81,243,49,123,69,56]
List = [49,69,81,123,243,56]
two smallest is = 49 + 56 = 105
two smallest is = 49 + 56 = 105
List = [81,243,123,69,105]
List = [69,81,123,243,105]
two smallest is = 69 + 81 = 150
two smallest is = 69 + 81 = 150
List = [243,123,105,150]
List = [105,123,243,150]
two smallest is = 105 + 123 = 228
two smallest is = 105 + 123 = 228
List = [243,150,228]
List = [150,243,228]
two smallest is = 150 + 228 = 378
two smallest is = 150 + 228 = 378
List = [243,378]
List = [243,378]
Line 559: Line 565:
done...
done...
</pre>
</pre>
===With sorting===
===Without sorting===
<lang ring>
<lang ring>
see "working..." + nl
see "working..." + nl

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

while true
while true
n++
n++
List = sort(List)
if n = 1
first = List[1]
see nl + "List = "
second = List[2]
showArray(List)
see nl
ok
first = min(List)
ind1 = find(List,first)
ind1 = find(List,first)
del(List,ind1)
second = min(List)
ind2 = find(List,second)
ind2 = find(List,second)
if ind1 < ind2
del(List,ind2)
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
del(List,ind2)
ok
sum = first + second
sum = first + second
add(List,sum)
add(List,sum)
if len(List) = 1
if len(List) = 1
exit
exit
ok
if n = 1
see nl + "List = "
showArray(List)
see nl
ok
ok
showList(first,second,sum,List)
showList(first,second,sum,List)
end
end

see "Last item is: " +List[1] + nl
see "Last item is: " +List[1] + nl
see "done..." + nl
see "done..." + nl

func showList(first,second,sum,List)
func showList(first,second,sum,List)
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "List = "
see "List = "
showArray(List)
showArray(List)

func showArray(array)
func showArray(array)
txt = ""
txt = ""
Line 615: Line 615:
working...
working...


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


two smallest is = 6 + 11 = 17
two smallest is = 6 + 11 = 17
List = [14,25,49,69,81,123,243,17]
List = [81,243,14,25,49,123,69,17]
two smallest is = 14 + 17 = 31
two smallest is = 14 + 17 = 31
List = [25,49,69,81,123,243,31]
List = [81,243,25,49,123,69,31]
two smallest is = 25 + 31 = 56
two smallest is = 25 + 31 = 56
List = [49,69,81,123,243,56]
List = [81,243,49,123,69,56]
two smallest is = 49 + 56 = 105
two smallest is = 49 + 56 = 105
List = [69,81,123,243,105]
List = [81,243,123,69,105]
two smallest is = 69 + 81 = 150
two smallest is = 69 + 81 = 150
List = [105,123,243,150]
List = [243,123,105,150]
two smallest is = 105 + 123 = 228
two smallest is = 105 + 123 = 228
List = [150,243,228]
List = [243,150,228]
two smallest is = 150 + 228 = 378
two smallest is = 150 + 228 = 378
List = [243,378]
List = [243,378]