Last list item: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
|||
Line 535: | Line 535: | ||
Two smallest: 150 + 228 = 378 |
Two smallest: 150 + 228 = 378 |
||
List: [243 378] |
List: [243 378] |
||
Two smallest: 243 + 378 = 621 |
|||
Last item is 621. |
|||
</pre> |
|||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
===With sorting=== |
|||
<lang jq>def task_with_sorting: |
|||
foreach range(1; length) as $i ( {a: .}; |
|||
.a |= sort |
|||
| .emit = "Sorted list: \(.a)\n" |
|||
| (.a[0] + .a[1]) as $sum |
|||
| .emit += "Two smallest: \(.a[0]) + \(.a[1]) = \($sum)" |
|||
| .a += [$sum] |
|||
| .a |= .[2:] ; |
|||
.emit, |
|||
(select(.a|length==1) | "Last item is \(.a[0]).") ); |
|||
[6, 81, 243, 14, 25, 49, 123, 69, 11] |
|||
| task_with_sorting</lang> |
|||
{{out}} |
|||
<pre> |
|||
Sorted list: [6,11,14,25,49,69,81,123,243] |
|||
Two smallest: 6 + 11 = 17 |
|||
Sorted list: [14,17,25,49,69,81,123,243] |
|||
Two smallest: 14 + 17 = 31 |
|||
Sorted list: [25,31,49,69,81,123,243] |
|||
Two smallest: 25 + 31 = 56 |
|||
Sorted list: [49,56,69,81,123,243] |
|||
Two smallest: 49 + 56 = 105 |
|||
Sorted list: [69,81,105,123,243] |
|||
Two smallest: 69 + 81 = 150 |
|||
Sorted list: [105,123,150,243] |
|||
Two smallest: 105 + 123 = 228 |
|||
Sorted list: [150,228,243] |
|||
Two smallest: 150 + 228 = 378 |
|||
Sorted list: [243,378] |
|||
Two smallest: 243 + 378 = 621 |
|||
Last item is 621. |
|||
</pre> |
|||
===Without sorting=== |
|||
<lang jq>def min_index: |
|||
. as $in |
|||
| reduce range(0; length) as $i ( null; |
|||
$in[$i] as $x |
|||
| if . == null then {ix: 0, min: $x} |
|||
elif $x < .min then {ix: $i, min: $x} |
|||
else . end) |
|||
| .ix; |
|||
def task_without_sorting: |
|||
# Output: [min, remainder] |
|||
def remove_min: |
|||
min_index as $ix |
|||
| [.[$ix], (.[:$ix] + .[$ix + 1:])]; |
|||
foreach range(1; length) as $i ( {a: .}; |
|||
.emit = "Unsorted list: \(.a)\n" |
|||
| (.a | remove_min) as [$m1, $x] |
|||
| ($x | remove_min) as [$m2, $y] |
|||
| ($m1 + $m2) as $sum |
|||
| .emit += "Two smallest: \($m1) + \($m2) = \($sum)" |
|||
| .a = $y + [$sum] ; |
|||
.emit, |
|||
(select(.a|length==1) | "Last item is \(.a[0]).") ); |
|||
[6, 81, 243, 14, 25, 49, 123, 69, 11] |
|||
| task_without_sorting |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Unsorted list: [6,81,243,14,25,49,123,69,11] |
|||
Two smallest: 6 + 11 = 17 |
|||
Unsorted list: [81,243,14,25,49,123,69,17] |
|||
Two smallest: 14 + 17 = 31 |
|||
Unsorted list: [81,243,25,49,123,69,31] |
|||
Two smallest: 25 + 31 = 56 |
|||
Unsorted list: [81,243,49,123,69,56] |
|||
Two smallest: 49 + 56 = 105 |
|||
Unsorted list: [81,243,123,69,105] |
|||
Two smallest: 69 + 81 = 150 |
|||
Unsorted list: [243,123,105,150] |
|||
Two smallest: 105 + 123 = 228 |
|||
Unsorted list: [243,150,228] |
|||
Two smallest: 150 + 228 = 378 |
|||
Unsorted list: [243,378] |
|||
Two smallest: 243 + 378 = 621 |
Two smallest: 243 + 378 = 621 |
||
Last item is 621. |
Last item is 621. |