Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
24
Task/Set-consolidation/EchoLisp/set-consolidation.echolisp
Normal file
24
Task/Set-consolidation/EchoLisp/set-consolidation.echolisp
Normal 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 } }
|
||||
|
||||
10
Task/Set-consolidation/Egison/set-consolidation-1.egison
Normal file
10
Task/Set-consolidation/Egison/set-consolidation-1.egison
Normal 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'}}))
|
||||
1
Task/Set-consolidation/Egison/set-consolidation-2.egison
Normal file
1
Task/Set-consolidation/Egison/set-consolidation-2.egison
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"DBAC" "HIKFG"}
|
||||
25
Task/Set-consolidation/Sidef/set-consolidation.sidef
Normal file
25
Task/Set-consolidation/Sidef/set-consolidation.sidef
Normal 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...)));
|
||||
}
|
||||
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