Last list item: Difference between revisions

Place "with sorting" before "without sorting".
(Place "with sorting" before "without sorting".)
Line 398:
 
=={{header|Nim}}==
 
===With sorting===
We sort in descending order as it is more efficient.
<lang Nim># With sorting.
import algorithm, strformat
 
proc extractAndAddTwoSmallest(list: var seq[int]) =
list.sort(Descending)
stdout.write &"Descending sorted list: {list}"
let min1 = list.pop()
let min2 = list.pop()
echo &"; two smallest: {min1} and {min2}; sum = {min1 + min2}"
list.add min1 + min2
 
var list = @[6, 81, 243, 14, 25, 49, 123, 69, 11]
 
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</lang>
 
{{out}}
<pre>Descending sorted list: @[243, 123, 81, 69, 49, 25, 14, 11, 6]; two smallest: 6 and 11; sum = 17
Descending sorted list: @[243, 123, 81, 69, 49, 25, 17, 14]; two smallest: 14 and 17; sum = 31
Descending sorted list: @[243, 123, 81, 69, 49, 31, 25]; two smallest: 25 and 31; sum = 56
Descending sorted list: @[243, 123, 81, 69, 56, 49]; two smallest: 49 and 56; sum = 105
Descending sorted list: @[243, 123, 105, 81, 69]; two smallest: 69 and 81; sum = 150
Descending sorted list: @[243, 150, 123, 105]; two smallest: 105 and 123; sum = 228
Descending sorted list: @[243, 228, 150]; two smallest: 150 and 228; sum = 378
Descending sorted list: @[378, 243]; two smallest: 243 and 378; sum = 621
Last item is 621.</pre>
 
===Without sorting===
We could use the function <code>minIndex</code> from module <code>sequtils</code> but it would be less efficient. We used a single loop instead. Please note that we remove the elements using function <code>del</code> which is O(1) rather than function <code>delete</code> which is O(n).
Line 437 ⟶ 468:
List: @[243, 150, 228]; two smallest: 150@1 and 228@2; sum = 378
List: @[243, 378]; two smallest: 243@0 and 378@1; sum = 621
Last item is 621.</pre>
 
===With sorting===
We sort in descending order as it is more efficient.
<lang Nim># With sorting.
import algorithm, strformat
 
proc extractAndAddTwoSmallest(list: var seq[int]) =
list.sort(Descending)
stdout.write &"Descending sorted list: {list}"
let min1 = list.pop()
let min2 = list.pop()
echo &"; two smallest: {min1} and {min2}; sum = {min1 + min2}"
list.add min1 + min2
 
var list = @[6, 81, 243, 14, 25, 49, 123, 69, 11]
 
while list.len >= 2:
list.extractAndAddTwoSmallest()
echo &"Last item is {list[0]}."</lang>
 
{{out}}
<pre>Descending sorted list: @[243, 123, 81, 69, 49, 25, 14, 11, 6]; two smallest: 6 and 11; sum = 17
Descending sorted list: @[243, 123, 81, 69, 49, 25, 17, 14]; two smallest: 14 and 17; sum = 31
Descending sorted list: @[243, 123, 81, 69, 49, 31, 25]; two smallest: 25 and 31; sum = 56
Descending sorted list: @[243, 123, 81, 69, 56, 49]; two smallest: 49 and 56; sum = 105
Descending sorted list: @[243, 123, 105, 81, 69]; two smallest: 69 and 81; sum = 150
Descending sorted list: @[243, 150, 123, 105]; two smallest: 105 and 123; sum = 228
Descending sorted list: @[243, 228, 150]; two smallest: 150 and 228; sum = 378
Descending sorted list: @[378, 243]; two smallest: 243 and 378; sum = 621
Last item is 621.</pre>
 
Anonymous user