|
|
Line 398: |
Line 398: |
|
|
|
|
|
=={{header|Nim}}== |
|
=={{header|Nim}}== |
|
|
|
|
⚫ |
|
|
⚫ |
We sort in descending order as it is more efficient. |
|
⚫ |
<lang Nim># With sorting. |
|
⚫ |
import algorithm, strformat |
|
|
|
|
⚫ |
proc extractAndAddTwoSmallest(list: var seq[int]) = |
|
⚫ |
|
|
⚫ |
stdout.write &"Descending sorted list: {list}" |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
echo &"; two smallest: {min1} and {min2}; sum = {min1 + min2}" |
|
⚫ |
|
|
|
|
|
⚫ |
var list = @[6, 81, 243, 14, 25, 49, 123, 69, 11] |
|
|
|
|
⚫ |
|
|
⚫ |
list.extractAndAddTwoSmallest() |
|
⚫ |
echo &"Last item is {list[0]}."</lang> |
|
|
|
|
⚫ |
|
|
⚫ |
<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 |
|
⚫ |
|
|
|
|
|
===Without sorting=== |
|
===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). |
|
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: |
Line 468: |
|
List: @[243, 150, 228]; two smallest: 150@1 and 228@2; sum = 378 |
|
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 |
|
List: @[243, 378]; two smallest: 243@0 and 378@1; sum = 621 |
⚫ |
|
|
|
|
|
⚫ |
|
|
⚫ |
We sort in descending order as it is more efficient. |
|
⚫ |
<lang Nim># With sorting. |
|
⚫ |
import algorithm, strformat |
|
|
|
|
⚫ |
proc extractAndAddTwoSmallest(list: var seq[int]) = |
|
⚫ |
|
|
⚫ |
stdout.write &"Descending sorted list: {list}" |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
echo &"; two smallest: {min1} and {min2}; sum = {min1 + min2}" |
|
⚫ |
|
|
|
|
|
⚫ |
var list = @[6, 81, 243, 14, 25, 49, 123, 69, 11] |
|
|
|
|
⚫ |
|
|
⚫ |
list.extractAndAddTwoSmallest() |
|
⚫ |
echo &"Last item is {list[0]}."</lang> |
|
|
|
|
⚫ |
|
|
⚫ |
<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> |
|
Last item is 621.</pre> |
|
|
|
|