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,25 @@
(lib 'list) ;; (combinations L k)
;; add a combination to each partition in ps
(define (pproduct c ps) (for/list ((x ps)) (cons c x)))
;; apply to any type of set S
;; ns is list of cardinals for each partition
;; for all combinations Ci of n objects from S
;; set S <- LS minus Ci , set n <- next n , and recurse
(define (_partitions S ns )
(cond
([empty? (rest ns)] (list (combinations S (first ns))))
(else
(for/fold (parts null)
([c (combinations S (first ns))])
(append
parts
(pproduct c (_partitions (set-substract S c) (rest ns))))))))
;; task : S = ( 0 , 1 ... n-1) args = ns
(define (partitions . args)
(for-each
writeln
(_partitions (range 1 (1+ (apply + args))) args )))

View file

@ -0,0 +1,20 @@
func part(_, {.is_empty}) { [[]] }
func partitions({.is_empty}) { [[]] }
func part(s, args) {
gather {
s.combinations(args[0], { |c|
part(s - c, args.ft(1)).each{|r| take([c] + r) }
})
}
}
func partitions(args) {
part(@(1..args.sum), args)
}
[[],[0,0,0],[1,1,1],[2,0,2]].each { |test_case|
say "partitions #{test_case}:"
partitions(test_case).each{|part| say part }
print "\n"
}

View file

@ -0,0 +1,20 @@
# Generate a stream of the distinct combinations of r items taken from the input array.
def combination(r):
if r > length or r < 0 then empty
elif r == length then .
else ( [.[0]] + (.[1:]|combination(r-1))),
( .[1:]|combination(r))
end;
# Input: a mask, that is, an array of lengths.
# Output: a stream of the distinct partitions defined by the mask.
def partition:
# partition an array of entities, s, according to a mask presented as input:
def p(s):
if length == 0 then []
else . as $mask
| (s | combination($mask[0])) as $c
| [$c] + ($mask[1:] | p(s - $c))
end;
. as $mask | p( [range(1; 1 + ($mask|add))] );

View file

@ -0,0 +1,3 @@
([],[0,0,0],[1,1,1],[2,0,2])
| . as $test_case
| "partitions \($test_case):" , ($test_case | partition), ""

View file

@ -0,0 +1,23 @@
$ jq -M -n -c -r -f Ordered_partitions.jq
partitions []:
[]
partitions [0,0,0]:
[[],[],[]]
partitions [1,1,1]:
[[1],[2],[3]]
[[1],[3],[2]]
[[2],[1],[3]]
[[2],[3],[1]]
[[3],[1],[2]]
[[3],[2],[1]]
partitions [2,0,2]:
[[1,2],[],[3,4]]
[[1,3],[],[2,4]]
[[1,4],[],[2,3]]
[[2,3],[],[1,4]]
[[2,4],[],[1,3]]
[[3,4],[],[1,2]]