Jump to content

Last list item: Difference between revisions

Line 490:
 
=={{header|Ring}}==
===WithoutWith sorting===
<lang ring>
see "working..." + nl
 
List = [6,81,243,14,25,49,123,69,11]
n = 0
 
while true
n++
if nList = 1sort(List)
first = see nl + "List = "[1]
second = showArray(List)[2]
see nl
ok
first = min(List)
ind1 = find(List,first)
del(List,ind1)
second = min(List)
ind2 = find(List,second)
del(List,if ind1 < ind2)
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
del(List,ind2)
ok
sum = first + second
add(List,sum)
if len(List) = 1
exit
ok
if n = 1
see nl + "List = "
showArray(List)
see nl
ok
showList(first,second,sum,List)
end
 
see "Last item is: " +List[1] + nl
see "done..." + nl
 
func showList(first,second,sum,List)
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "List = "
showArray(List)
 
func showArray(array)
txt = ""
Line 540 ⟶ 546:
working...
 
List = [6,81,243,14,25,49,69,81,123,69243,1117]
 
two smallest is = 6 + 11 = 17
List = [81,243,14,25,49,69,81,123,69243,17]
two smallest is = 14 + 17 = 31
List = [81,243,25,49,69,81,123,69243,31]
two smallest is = 25 + 31 = 56
List = [8149,24369,4981,123,69243,56]
two smallest is = 49 + 56 = 105
List = [69,81,243,123,69243,105]
two smallest is = 69 + 81 = 150
List = [243105,123,105243,150]
two smallest is = 105 + 123 = 228
List = [243,150,243,228]
two smallest is = 150 + 228 = 378
List = [243,378]
Line 559 ⟶ 565:
done...
</pre>
===WithWithout sorting===
<lang ring>
see "working..." + nl
 
List = [6,81,243,14,25,49,123,69,11]
n = 0
 
while true
n++
Listif n = sort(List)1
first = see nl + "List[1] = "
second = showArray(List[2])
see nl
ok
first = min(List)
ind1 = find(List,first)
del(List,ind1)
second = min(List)
ind2 = find(List,second)
if ind1 < del(List,ind2)
del(List,ind2)
del(List,ind1)
else
del(List,ind1)
del(List,ind2)
ok
sum = first + second
add(List,sum)
if len(List) = 1
exit
ok
if n = 1
see nl + "List = "
showArray(List)
see nl
ok
showList(first,second,sum,List)
end
 
see "Last item is: " +List[1] + nl
see "done..." + nl
 
func showList(first,second,sum,List)
see "two smallest is = " + first + " + " + second + " = " + sum + nl
see "List = "
showArray(List)
 
func showArray(array)
txt = ""
Line 615:
working...
 
List = [6,81,243,14,25,49,69,81,123,24369,1711]
 
two smallest is = 6 + 11 = 17
List = [81,243,14,25,49,69,81,123,24369,17]
two smallest is = 14 + 17 = 31
List = [81,243,25,49,69,81,123,24369,31]
two smallest is = 25 + 31 = 56
List = [4981,69243,8149,123,24369,56]
two smallest is = 49 + 56 = 105
List = [69,81,243,123,24369,105]
two smallest is = 69 + 81 = 150
List = [105243,123,243105,150]
two smallest is = 105 + 123 = 228
List = [150,243,150,228]
two smallest is = 150 + 228 = 378
List = [243,378]
2,468

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.