Common sorted list: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|REXX}}: added the computer programming language REXX.)
(Added Wren)
Line 73: Line 73:
<pre>
<pre>
common sorted list elements are: [1,3,4,5,7,8,9]
common sorted list elements are: [1,3,4,5,7,8,9]
</pre>

=={{header|Wren}}==
{{libheader|Wren-seq}}
{{libheader|Wren-sort}}
<lang ecmascript>import "/seq" for Lst
import "/sort" for Sort

var distinctUnion2 = Fn.new { |l1, l2| Lst.distinct(l1 + l2) }

var distinctUnionN = Fn.new { |ll|
var n = ll.count
if (n == 0) return ll
if (n == 1) return ll[0]
var first2 = distinctUnion2.call(ll[0], ll[1])
if (n == 2) return first2
return ll.skip(2).reduce(first2) { |acc, l| distinctUnion2.call(acc, l) }
}

var ll = [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]]
System.print("Distinct sorted union of %(ll) is:")
var u = distinctUnionN.call(ll)
Sort.insertion(u)
System.print(u)</lang>

{{out}}
<pre>
Distinct sorted union of [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]] is:
[1, 3, 4, 5, 7, 8, 9]
</pre>
</pre>

Revision as of 10:25, 24 February 2021

Common sorted list is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given an integer array nums, the goal is create common sorted list with unique elements.


Example

nums = [5,1,3,8,9,4,8,7], [3,5,9,8,4], [1,3,7,9]

output = [1,3,4,5,7,8,9]

REXX

<lang rexx>/*REXX pgm creates and displays a common sorted list of a specified collection of sets*/ parse arg a /*obtain optional arguments from the CL*/ if a= | a="," then a= '[5,1,3,8,9,4,8,7] [3,5,9,8,4] [1,3,7,9]' /*default sets.*/ x= translate(a, ,'],[') /*extract elements from collection sets*/ se= words(x)

  1. = 0; $= /*#: number of unique elements; $: list*/

$= /*the list of common elements (so far).*/

   do j=1  for se;  _= word(x, j)               /*traipse through all elements in sets.*/
   if wordpos(_, $)>0  then iterate             /*Is element in the new list? Yes, skip*/
   $= $ _;   #= # + 1;    @.#= _                /*add to list; bump counter; assign──►@*/
   end   /*j*/

$= call eSort # /*use any short (small) exchange sort.*/

                do k=1  for #;   $= $  @.k      /*rebuild the $ list, it's been sorted.*/
                end   /*k*/

say 'the list of sorted common elements in all sets: ' "["translate(space($), ',', " ")']' exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ eSort: procedure expose @.; arg h 1 z; do while h>1; h= h%2; do i=1 for z-h; j= i; k= h+i

       do while @.k<@.j; t=@.j; @.j=@.k; @.k=t; if h>=j then leave; j=j-h; k=k-h; end;end
       end;     return                          /*this sort was used 'cause of brevity.*/</lang>
output   when using the default inputs:
the list of sorted common elements in all sets:  [1,3,4,5,7,8,9]

Ring

<lang ring> nums = [[5,1,3,8,9,4,8,7],[3,5,9,8,4],[1,3,7,9]] sumNums = []

for n = 1 to len(nums)

   for m = 1 to len(nums[n])
       add(sumNums,nums[n][m])
   next

next

sumNums = sort(sumNums) for n = len(sumNums) to 2 step -1

   if sumNums[n] = sumNums[n-1]
      del(sumNums,n)
   ok

next

sumNums = sort(sumNums)

see "common sorted list elements are: " showArray(sumNums)

func showArray(array)

    txt = ""
    see "["
    for n = 1 to len(array)
        txt = txt + array[n] + ","
    next
    txt = left(txt,len(txt)-1)
    txt = txt + "]"
    see txt

</lang>

Output:
common sorted list elements are: [1,3,4,5,7,8,9]

Wren

Library: Wren-seq
Library: Wren-sort

<lang ecmascript>import "/seq" for Lst import "/sort" for Sort

var distinctUnion2 = Fn.new { |l1, l2| Lst.distinct(l1 + l2) }

var distinctUnionN = Fn.new { |ll|

   var n = ll.count
   if (n == 0) return ll
   if (n == 1) return ll[0]
   var first2 = distinctUnion2.call(ll[0], ll[1])
   if (n == 2) return first2
   return ll.skip(2).reduce(first2) { |acc, l| distinctUnion2.call(acc, l) }

}

var ll = [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]] System.print("Distinct sorted union of %(ll) is:") var u = distinctUnionN.call(ll) Sort.insertion(u) System.print(u)</lang>

Output:
Distinct sorted union of [[5, 1, 3, 8, 9, 4, 8, 7], [3, 5, 9, 8, 4], [1, 3, 7, 9]] is:
[1, 3, 4, 5, 7, 8, 9]