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
25
Task/Ordered-Partitions/EchoLisp/ordered-partitions.echolisp
Normal file
25
Task/Ordered-Partitions/EchoLisp/ordered-partitions.echolisp
Normal 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 )))
|
||||
20
Task/Ordered-Partitions/Sidef/ordered-partitions.sidef
Normal file
20
Task/Ordered-Partitions/Sidef/ordered-partitions.sidef
Normal 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"
|
||||
}
|
||||
20
Task/Ordered-Partitions/jq/ordered-partitions-1.jq
Normal file
20
Task/Ordered-Partitions/jq/ordered-partitions-1.jq
Normal 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))] );
|
||||
3
Task/Ordered-Partitions/jq/ordered-partitions-2.jq
Normal file
3
Task/Ordered-Partitions/jq/ordered-partitions-2.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
([],[0,0,0],[1,1,1],[2,0,2])
|
||||
| . as $test_case
|
||||
| "partitions \($test_case):" , ($test_case | partition), ""
|
||||
23
Task/Ordered-Partitions/jq/ordered-partitions-3.jq
Normal file
23
Task/Ordered-Partitions/jq/ordered-partitions-3.jq
Normal 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]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue