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
36
Task/Combinations/ERRE/combinations.erre
Normal file
36
Task/Combinations/ERRE/combinations.erre
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
PROGRAM COMBINATIONS
|
||||
|
||||
CONST M_MAX=3,N_MAX=5
|
||||
|
||||
DIM COMBINATION[M_MAX],STACK[100,1]
|
||||
|
||||
PROCEDURE GENERATE(M)
|
||||
LOCAL I
|
||||
IF (M>M_MAX) THEN
|
||||
FOR I=1 TO M_MAX DO
|
||||
PRINT(COMBINATION[I];" ";)
|
||||
END FOR
|
||||
PRINT
|
||||
ELSE
|
||||
FOR N=1 TO N_MAX DO
|
||||
IF ((M=1) OR (N>COMBINATION[M-1])) THEN
|
||||
COMBINATION[M]=N
|
||||
! --- PUSH STACK -----------
|
||||
STACK[SP,0]=M STACK[SP,1]=N
|
||||
SP=SP+1
|
||||
! --------------------------
|
||||
|
||||
GENERATE(M+1)
|
||||
|
||||
! --- POP STACK ------------
|
||||
SP=SP-1
|
||||
M=STACK[SP,0] N=STACK[SP,1]
|
||||
! --------------------------
|
||||
END IF
|
||||
END FOR
|
||||
END IF
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
GENERATE(1)
|
||||
END PROGRAM
|
||||
25
Task/Combinations/EchoLisp/combinations.echolisp
Normal file
25
Task/Combinations/EchoLisp/combinations.echolisp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
;;
|
||||
;; using the native (combinations) function
|
||||
(lib 'list)
|
||||
(combinations (iota 5) 3)
|
||||
→ ((0 1 2) (0 1 3) (0 1 4) (0 2 3) (0 2 4) (0 3 4) (1 2 3) (1 2 4) (1 3 4) (2 3 4))
|
||||
;;
|
||||
;; using an iterator
|
||||
;;
|
||||
(lib 'sequences)
|
||||
(take (combinator (iota 5) 3) #:all)
|
||||
→ ((0 1 2) (0 1 3) (0 1 4) (0 2 3) (0 2 4) (0 3 4) (1 2 3) (1 2 4) (1 3 4) (2 3 4))
|
||||
;;
|
||||
;; defining a function
|
||||
;;
|
||||
(define (combine lst p) (cond
|
||||
[(null? lst) null]
|
||||
[(< (length lst) p) null]
|
||||
[(= (length lst) p) (list lst)]
|
||||
[(= p 1) (map list lst)]
|
||||
[else (append
|
||||
(map cons (circular-list (first lst)) (combine (rest lst) (1- p)))
|
||||
(combine (rest lst) p))]))
|
||||
|
||||
(combine (iota 5) 3)
|
||||
→ ((0 1 2) (0 1 3) (0 1 4) (0 2 3) (0 2 4) (0 3 4) (1 2 3) (1 2 4) (1 3 4) (2 3 4))
|
||||
6
Task/Combinations/Egison/combinations.egison
Normal file
6
Task/Combinations/Egison/combinations.egison
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(define $comb
|
||||
(lambda [$n $xs]
|
||||
(match-all xs (list integer)
|
||||
[(loop $i [1 ,n] <join _ <cons $a_i ...>> _) a])))
|
||||
|
||||
(test (comb 3 (between 0 4)))
|
||||
21
Task/Combinations/Nim/combinations.nim
Normal file
21
Task/Combinations/Nim/combinations.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
iterator comb(m, n): seq[int] =
|
||||
var c = newSeq[int](n)
|
||||
for i in 0 .. <n: c[i] = i
|
||||
|
||||
block outer:
|
||||
while true:
|
||||
yield c
|
||||
|
||||
var i = n-1
|
||||
inc c[i]
|
||||
if c[i] <= m - 1: continue
|
||||
|
||||
while c[i] >= m - n + i:
|
||||
dec i
|
||||
if i < 0: break outer
|
||||
inc c[i]
|
||||
while i < n-1:
|
||||
c[i+1] = c[i] + 1
|
||||
inc i
|
||||
|
||||
for i in comb(5, 3): echo i
|
||||
22
Task/Combinations/Perl5i/combinations.perl5i
Normal file
22
Task/Combinations/Perl5i/combinations.perl5i
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use perl5i::2;
|
||||
|
||||
# ----------------------------------------
|
||||
# generate combinations of length $n consisting of characters
|
||||
# from the sorted set @set, using each character once in a
|
||||
# combination, with sorted strings in sorted order.
|
||||
#
|
||||
# Returns a list of array references, each containing one combination.
|
||||
#
|
||||
func combine($n, @set) {
|
||||
return unless @set;
|
||||
return map { [ $_ ] } @set if $n == 1;
|
||||
|
||||
my ($head) = shift @set;
|
||||
my @result = combine( $n-1, @set );
|
||||
for my $subarray ( @result ) {
|
||||
$subarray->unshift( $head );
|
||||
}
|
||||
return ( @result, combine( $n, @set ) );
|
||||
}
|
||||
|
||||
say @$_ for combine( 3, ('a'..'e') );
|
||||
13
Task/Combinations/Phix/combinations.phix
Normal file
13
Task/Combinations/Phix/combinations.phix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
procedure comb(integer pool, needed, done=0, sequence chosen={})
|
||||
if needed=0 then -- got a full set
|
||||
?chosen -- (or use a routine_id, result arg, or whatever)
|
||||
return
|
||||
end if
|
||||
if done+needed>pool then return end if -- cannot fulfil
|
||||
-- get all combinations with and without the next item:
|
||||
done += 1
|
||||
comb(pool,needed-1,done,append(chosen,done))
|
||||
comb(pool,needed,done,chosen)
|
||||
end procedure
|
||||
|
||||
comb(5,3)
|
||||
101
Task/Combinations/Pyret/combinations.pyret
Normal file
101
Task/Combinations/Pyret/combinations.pyret
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
fun combos<a>(lst :: List<a>, size :: Number) -> List<List<a>>:
|
||||
# return all subsets of lst of a certain size,
|
||||
# maintaining the original ordering of the list
|
||||
|
||||
# Let's handle a bunch of degenerate cases up front
|
||||
# to be defensive...
|
||||
if lst.length() < size:
|
||||
# return an empty list if size is too big
|
||||
[list:]
|
||||
else if lst.length() == size:
|
||||
# combos([list: 1,2,3,4]) == list[list: 1,2,3,4]]
|
||||
[list: lst]
|
||||
else if size == 1:
|
||||
# combos(list: 5, 9]) == list[[list: 5], [list: 9]]
|
||||
lst.map(lam(elem): [list: elem] end)
|
||||
else:
|
||||
# The main resursive step here is to consider
|
||||
# all the combinations of the list that have the
|
||||
# first element (aka head) and then those that don't
|
||||
# don't.
|
||||
cases(List) lst:
|
||||
| empty => [list:]
|
||||
| link(head, rest) =>
|
||||
# All the subsets of our list either include the
|
||||
# first element of the list (aka head) or they don't.
|
||||
with-head-combos = combos(rest, size - 1).map(
|
||||
lam(combo):
|
||||
link(head, combo) end
|
||||
)
|
||||
without-head-combos = combos(rest, size)
|
||||
with-head-combos._plus(without-head-combos)
|
||||
end
|
||||
end
|
||||
where:
|
||||
# define semantics for the degenerate cases, although
|
||||
# maybe we should just make some of these raise errors
|
||||
combos([list:], 0) is [list: [list:]]
|
||||
combos([list:], 1) is [list:]
|
||||
combos([list: "foo"], 1) is [list: [list: "foo"]]
|
||||
combos([list: "foo"], 2) is [list:]
|
||||
|
||||
# test the normal stuff
|
||||
lst = [list: 1, 2, 3]
|
||||
combos(lst, 1) is [list:
|
||||
[list: 1],
|
||||
[list: 2],
|
||||
[list: 3]
|
||||
]
|
||||
combos(lst, 2) is [list:
|
||||
[list: 1, 2],
|
||||
[list: 1, 3],
|
||||
[list: 2, 3]
|
||||
]
|
||||
combos(lst, 3) is [list:
|
||||
[list: 1, 2, 3]
|
||||
]
|
||||
|
||||
# remember the 10th row of Pascal's Triangle? :)
|
||||
lst10 = [list: 1,2,3,4,5,6,7,8,9,10]
|
||||
combos(lst10, 3).length() is 120
|
||||
combos(lst10, 4).length() is 210
|
||||
combos(lst10, 5).length() is 252
|
||||
combos(lst10, 6).length() is 210
|
||||
combos(lst10, 7).length() is 120
|
||||
|
||||
# more sanity checks...
|
||||
for each(sublst from combos(lst10, 6)):
|
||||
sublst.length() is 6
|
||||
end
|
||||
|
||||
for each(sublst from combos(lst10, 9)):
|
||||
sublst.length() is 9
|
||||
end
|
||||
end
|
||||
|
||||
fun int-combos(n :: Number, m :: Number) -> List<List<Number>>:
|
||||
doc: "return all lists of size m containing distinct, ordered nonnegative ints < n"
|
||||
lst = range(0, n)
|
||||
combos(lst, m)
|
||||
where:
|
||||
int-combos(5, 5) is [list: [list: 0,1,2,3,4]]
|
||||
int-combos(3, 2) is [list:
|
||||
[list: 0, 1],
|
||||
[list: 0, 2],
|
||||
[list: 1, 2]
|
||||
]
|
||||
end
|
||||
|
||||
fun display-3-comb-5-for-rosetta-code():
|
||||
# The very concrete nature of this function is driven
|
||||
# by the web page from Rosetta Code. We want to display
|
||||
# output similar to the top of this page:
|
||||
#
|
||||
# https://rosettacode.org/wiki/Combinations
|
||||
results = int-combos(5, 3)
|
||||
for each(lst from results):
|
||||
print(lst.join-str(" "))
|
||||
end
|
||||
end
|
||||
|
||||
display-3-comb-5-for-rosetta-code()
|
||||
5
Task/Combinations/SPAD/combinations.spad
Normal file
5
Task/Combinations/SPAD/combinations.spad
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[reverse subSet(5,3,i)$SGCF for i in 0..binomial(5,3)-1]
|
||||
|
||||
[[0,1,2], [0,1,3], [0,2,3], [1,2,3], [0,1,4], [0,2,4], [1,2,4], [0,3,4],
|
||||
[1,3,4], [2,3,4]]
|
||||
Type: List(List(Integer))
|
||||
1
Task/Combinations/Sidef/combinations-1.sidef
Normal file
1
Task/Combinations/Sidef/combinations-1.sidef
Normal file
|
|
@ -0,0 +1 @@
|
|||
(@^5).combinations(3, {|c| say c })
|
||||
17
Task/Combinations/Sidef/combinations-2.sidef
Normal file
17
Task/Combinations/Sidef/combinations-2.sidef
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
func combine(n, set) {
|
||||
|
||||
set.len || return []
|
||||
n == 1 && return set.map{[_]}
|
||||
|
||||
var (head, result)
|
||||
head = set.shift
|
||||
result = combine(n-1, [set...])
|
||||
|
||||
for subarray in result {
|
||||
subarray.prepend(head)
|
||||
}
|
||||
|
||||
result + combine(n, set)
|
||||
}
|
||||
|
||||
combine(3, @^5).each {|c| say c }
|
||||
31
Task/Combinations/Sidef/combinations-3.sidef
Normal file
31
Task/Combinations/Sidef/combinations-3.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
func forcomb(callback, n, k) {
|
||||
|
||||
if (k == 0) {
|
||||
callback([])
|
||||
return()
|
||||
}
|
||||
|
||||
if (k<0 || k>n || n==0) {
|
||||
return()
|
||||
}
|
||||
|
||||
var c = @^k
|
||||
|
||||
loop {
|
||||
callback([c...])
|
||||
c[k-1]++ < n-1 && next
|
||||
var i = k-2
|
||||
while (i>=0 && c[i]>=(n-(k-i))) {
|
||||
--i
|
||||
}
|
||||
i < 0 && break
|
||||
c[i]++
|
||||
while (++i < k) {
|
||||
c[i] = c[i-1]+1
|
||||
}
|
||||
}
|
||||
|
||||
return()
|
||||
}
|
||||
|
||||
forcomb({|c| say c }, 5, 3)
|
||||
20
Task/Combinations/Swift/combinations.swift
Normal file
20
Task/Combinations/Swift/combinations.swift
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
|
||||
|
||||
return (0..<pivotList.count)
|
||||
.map {
|
||||
_ -> ([Int], [Int]) in
|
||||
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
|
||||
}
|
||||
}
|
||||
func combosOfLength(n: Int, m: Int) -> [[Int]] {
|
||||
|
||||
return [Int](1...m)
|
||||
.reduce([([Int](), [Int](0..<n))]) {
|
||||
(accum, _) in
|
||||
accum.flatMap(addCombo)
|
||||
}.map {
|
||||
$0.0
|
||||
}
|
||||
}
|
||||
|
||||
println(combosOfLength(5, 3))
|
||||
9
Task/Combinations/jq/combinations.jq
Normal file
9
Task/Combinations/jq/combinations.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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;
|
||||
|
||||
# select r integers from the set (0 .. n-1)
|
||||
def combinations(n;r): [range(0;n)] | combination(r);
|
||||
Loading…
Add table
Add a link
Reference in a new issue