Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,24 @@
;; utility : make a set of sets from a list
(define (make-set* s)
(or (when (list? s) (make-set (map make-set* s))) s))
;; union of all sets which intersect - O(n^2)
(define (make-big ss)
(make-set
(for/list ((u ss))
(for/fold (big u) ((v ss)) #:when (set-intersect? big v) (set-union big v)))))
;; remove sets which are subset of another one - O(n^2)
(define (remove-small ss)
(for/list ((keep ss))
#:when (for/and ((v ss)) #:continue (set-equal? keep v) (not (set-subset? v keep)))
keep))
(define (consolidate ss) (make-set (remove-small (make-big ss))))
(define S (make-set* ' ((h i k) ( a b) ( b c) (c d) ( f g h))))
→ { { a b } { b c } { c d } { f g h } { h i k } }
(consolidate S)
→ { { a b c d } { f g h i k } }

View file

@ -0,0 +1,10 @@
(define $consolidate
(lambda [$xss]
(match xss (multiset (set char))
{[<cons <cons $m $xs>
<cons <cons ,m $ys>
$rss>>
(consolidate {(unique/m char {m @xs @ys}) @rss})]
[_ xss]})))
(test (consolidate {{'H' 'I' 'K'} {'A' 'B'} {'C' 'D'} {'D' 'B'} {'F' 'G' 'H'}}))

View file

@ -0,0 +1 @@
{"DBAC" "HIKFG"}

View file

@ -0,0 +1,25 @@
func consolidate() { [] }
func consolidate(this, *those) {
gather {
consolidate(those...).each { |that|
if (this & that) { this |= that }
else { take that }
}
take this;
}
}
enum |A="A", B, C, D, _E, F, G, H, I, _J, K|;
func format(ss) {
ss.map{ '(' + .join(' ') + ')' }.join(' ')
}
[
[[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]]
].each { |ss|
say (format(ss), "\n\t==> ", format(consolidate(ss...)));
}

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