Last list item: Difference between revisions
Content added Content deleted
Line 396: | Line 396: | ||
(list, addleastreduce!(copy(list))) = ([6, 81, 243, 14, 25, 49, 123, 69, 11], [621]) |
(list, addleastreduce!(copy(list))) = ([6, 81, 243, 14, 25, 49, 123, 69, 11], [621]) |
||
</pre> |
</pre> |
||
=={{header|Nim}}== |
|||
===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). |
|||
<lang Nim># Without sorting. |
|||
import strformat |
|||
proc extractAndAddTwoSmallest(list: var seq[int]) = |
|||
var min1, min2 = int.high |
|||
var imin1, imin2 = -1 |
|||
for i, val in list: |
|||
if val < min1: |
|||
min2 = min1 |
|||
min1 = val |
|||
imin2 = imin1 |
|||
imin1 = i |
|||
elif val < min2: |
|||
min2 = val |
|||
imin2 = i |
|||
echo &"List: {list}; two smallest: {min1}@{imin1} and {min2}@{imin2}; sum = {min1 + min2}" |
|||
if imin1 > imin2: swap imin1, imin2 # Make sure "imin2" is the greatest index. |
|||
list.del imin2 |
|||
list.del imin1 |
|||
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>List: @[6, 81, 243, 14, 25, 49, 123, 69, 11]; two smallest: 6@0 and 11@8; sum = 17 |
|||
List: @[69, 81, 243, 14, 25, 49, 123, 17]; two smallest: 14@3 and 17@7; sum = 31 |
|||
List: @[69, 81, 243, 123, 25, 49, 31]; two smallest: 25@4 and 31@6; sum = 56 |
|||
List: @[69, 81, 243, 123, 49, 56]; two smallest: 49@4 and 56@5; sum = 105 |
|||
List: @[69, 81, 243, 123, 105]; two smallest: 69@0 and 81@1; sum = 150 |
|||
List: @[123, 105, 243, 150]; two smallest: 105@1 and 123@0; sum = 228 |
|||
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> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |