Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View 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;

View 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;

View 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

View 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"]]