Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
7
Task/Set-consolidation/Jq/set-consolidation-1.jq
Normal file
7
Task/Set-consolidation/Jq/set-consolidation-1.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def to_set: unique;
|
||||
|
||||
def union(A; B): (A + B) | unique;
|
||||
|
||||
# boolean
|
||||
def intersect(A;B):
|
||||
reduce A[] as $x (false; if . then . else (B|index($x)) end) | not | not;
|
||||
30
Task/Set-consolidation/Jq/set-consolidation-2.jq
Normal file
30
Task/Set-consolidation/Jq/set-consolidation-2.jq
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Input: [i, j, sets] with i < j
|
||||
# Return [i,j] for a pair that can be combined, else null
|
||||
def combinable:
|
||||
.[0] as $i | .[1] as $j | .[2] as $sets
|
||||
| ($sets|length) as $length
|
||||
| if intersect($sets[$i]; $sets[$j]) then [$i, $j]
|
||||
elif $i < $j - 1 then (.[0] += 1 | combinable)
|
||||
elif $j < $length - 1 then [0, $j+1, $sets] | combinable
|
||||
else null
|
||||
end;
|
||||
|
||||
# Given an array of arrays, remove the i-th and j-th elements,
|
||||
# and add their union:
|
||||
def update(i;j):
|
||||
if i > j then update(j;i)
|
||||
elif i == j then del(.[i])
|
||||
else
|
||||
union(.[i]; .[j]) as $c
|
||||
| union(del(.[j]) | del(.[i]); [$c])
|
||||
end;
|
||||
|
||||
# Input: a set of sets
|
||||
def consolidate:
|
||||
if length <= 1 then .
|
||||
else
|
||||
([0, 1, .] | combinable) as $c
|
||||
| if $c then update($c[0]; $c[1]) | consolidate
|
||||
else .
|
||||
end
|
||||
end;
|
||||
11
Task/Set-consolidation/Jq/set-consolidation-3.jq
Normal file
11
Task/Set-consolidation/Jq/set-consolidation-3.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def tests:
|
||||
[["A", "B"], ["C","D"]],
|
||||
[["A","B"], ["B","D"]],
|
||||
[["A","B"], ["C","D"], ["D","B"]],
|
||||
[["H","I","K"], ["A","B"], ["C","D"], ["D","B"], ["F","G","H"]]
|
||||
;
|
||||
|
||||
def test:
|
||||
tests | to_set | consolidate;
|
||||
|
||||
test
|
||||
5
Task/Set-consolidation/Jq/set-consolidation-4.jq
Normal file
5
Task/Set-consolidation/Jq/set-consolidation-4.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
$ jq -c -n -f Set_consolidation.rc
|
||||
[["A","B"],["C","D"]]
|
||||
[["A","B","D"]]
|
||||
[["A","B","C","D"]]
|
||||
[["A","B","C","D"],["F","G","H","I","K"]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue