September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

2
Task/Set/Jq/set-1.jq Normal file
View file

@ -0,0 +1,2 @@
{"a":true, "b":true } == {"b":true, "a":true}.
{"a":true} + {"b":true } == { "a":true, "b":true}

19
Task/Set/Jq/set-10.jq Normal file
View file

@ -0,0 +1,19 @@
# If A and B are sets, then A-B is emitted
def difference(A;B):
(A|length) as $al
| (B|length) as $bl
| if $al == 0 then [] elif $bl == 0 then A
else
reduce range(0; $al + $bl) as $k
( [0, 0, []];
.[0] as $i | .[1] as $j
| if $i < $al and $j < $bl then
if A[$i] == B[$j] then [ $i+1, $j+1, .[2] ]
elif A[$i] < B[$j] then [ $i+1, $j, .[2] + [A[$i]] ]
else [ $i , $j+1, .[2] ]
end
elif $i < $al then [ $i+1, $j, .[2] + [A[$i]] ]
else .
end
) | .[2]
end ;

22
Task/Set/Jq/set-11.jq Normal file
View file

@ -0,0 +1,22 @@
# merge input array with array x by comparing the heads of the arrays in turn;
# if both arrays are sorted, the result will be sorted:
def merge(x):
length as $length
| (x|length) as $xl
| if $length == 0 then x
elif $xl == 0 then .
else
. as $in
| reduce range(0; $xl + $length) as $z
# state [ix, xix, ans]
( [0, 0, []];
if .[0] < $length and ((.[1] < $xl and $in[.[0]] <= x[.[1]]) or .[1] == $xl)
then [(.[0] + 1), .[1], (.[2] + [$in[.[0]]]) ]
else [.[0], (.[1] + 1), (.[2] + [x[.[1]]]) ]
end
) | .[2]
end ;
def union(A;B):
A|merge(B)
| reduce .[] as $m ([]; if length == 0 or .[length-1] != $m then . + [$m] else . end);

10
Task/Set/Jq/set-12.jq Normal file
View file

@ -0,0 +1,10 @@
def subset(A;B):
# TCO
def _subset:
if .[0]|length == 0 then true
elif .[1]|length == 0 then false
elif .[0][0] == .[1][0] then [.[0][1:], .[1][1:]] | _subset
elif .[0][0] < .[1][0] then false
else [ .[0], .[1][1:] ] | _subset
end;
[A,B] | _subset;

11
Task/Set/Jq/set-13.jq Normal file
View file

@ -0,0 +1,11 @@
def intersect:
.[0] as $A | .[1] as $B
| ($A|length) as $al
| ($B|length) as $bl
| if $al == 0 or $bl == 0 then false
else
($B | bsearch($A[0])) as $b
| if $b >= 0 then true
else [$A[1:], $B[- (1 + $b) :]] | intersect
end
end;

2
Task/Set/Jq/set-2.jq Normal file
View file

@ -0,0 +1,2 @@
def is_stringset:
. as $in | type == "object" and reduce keys[] as $key (true; . and $in[$key] == true);

1
Task/Set/Jq/set-3.jq Normal file
View file

@ -0,0 +1 @@
T | has(m)

4
Task/Set/Jq/set-4.jq Normal file
View file

@ -0,0 +1,4 @@
# Set-intersection: A ∩ B
def stringset_intersection(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . + {($k):true} else . end);

4
Task/Set/Jq/set-5.jq Normal file
View file

@ -0,0 +1,4 @@
# stringset_difference: A \ B
def stringset_difference(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . else . + {($k):true} end);

4
Task/Set/Jq/set-6.jq Normal file
View file

@ -0,0 +1,4 @@
# A ⊆ B iff string_subset(A;B)
def stringset_subset(A;B):
reduce (A|keys)[] as $k
(true; . and (B|has($k)));

5
Task/Set/Jq/set-7.jq Normal file
View file

@ -0,0 +1,5 @@
def is_set:
. as $in
| type == "array" and
reduce range(0;length-1) as $i
(true; if . then $in[$i] < $in[$i+1] else false end);

1
Task/Set/Jq/set-8.jq Normal file
View file

@ -0,0 +1 @@
def is_member(m): bsearch(m) > -1;

18
Task/Set/Jq/set-9.jq Normal file
View file

@ -0,0 +1,18 @@
# If A and B are sets, then intersection(A;B) emits their intersection:
def intersection(A;B):
(A|length) as $al
| (B|length) as $bl
| if $al == 0 or $bl == 0 then []
else
reduce range(0; $al + $bl) as $k
( [0, 0, []];
.[0] as $i | .[1] as $j
| if $i < $al and $j < $bl then
if A[$i] == B[$j] then [ $i+1 , $j+1, .[2] + [A[$i]]]
elif A[$i] < B[$j] then [ $i+1 , $j, .[2] ]
else [ $i , $j+1, .[2] ]
end
else .
end
) | .[2]
end ;