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
7
Task/Permutations/LFE/permutations-1.lfe
Normal file
7
Task/Permutations/LFE/permutations-1.lfe
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defun permute
|
||||
(('())
|
||||
'(()))
|
||||
((l)
|
||||
(lc ((<- x l)
|
||||
(<- y (permute (-- l `(,x)))))
|
||||
(cons x y))))
|
||||
2
Task/Permutations/LFE/permutations-2.lfe
Normal file
2
Task/Permutations/LFE/permutations-2.lfe
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> (permute '(1 2 3))
|
||||
((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1))
|
||||
28
Task/Permutations/Nim/permutations.nim
Normal file
28
Task/Permutations/Nim/permutations.nim
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# iterative Boothroyd method
|
||||
iterator permutations[T](ys: openarray[T]): seq[T] =
|
||||
var
|
||||
d = 1
|
||||
c = newSeq[int](ys.len)
|
||||
xs = newSeq[T](ys.len)
|
||||
|
||||
for i, y in ys: xs[i] = y
|
||||
yield xs
|
||||
|
||||
block outer:
|
||||
while true:
|
||||
while d > 1:
|
||||
dec d
|
||||
c[d] = 0
|
||||
while c[d] >= d:
|
||||
inc d
|
||||
if d >= ys.len: break outer
|
||||
|
||||
let i = if (d and 1) == 1: c[d] else: 0
|
||||
swap xs[i], xs[d]
|
||||
yield xs
|
||||
inc c[d]
|
||||
|
||||
var x = @[1,2,3]
|
||||
|
||||
for i in permutations(x):
|
||||
echo i
|
||||
17
Task/Permutations/Phix/permutations-1.phix
Normal file
17
Task/Permutations/Phix/permutations-1.phix
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
global function permute(integer n, sequence set)
|
||||
--
|
||||
-- return the nth permute of the given set.
|
||||
-- n should be an integer in the range 1 to factorial(length(set))
|
||||
--
|
||||
sequence res
|
||||
integer w
|
||||
n -= 1
|
||||
res = set
|
||||
for i=length(set) to 1 by -1 do
|
||||
w = remainder(n,i)+1
|
||||
res[i] = set[w]
|
||||
set[w] = set[i]
|
||||
n = floor(n/i)
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
8
Task/Permutations/Phix/permutations-2.phix
Normal file
8
Task/Permutations/Phix/permutations-2.phix
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function permutes(sequence set)
|
||||
sequence res = repeat(0,factorial(length(set)))
|
||||
for i=1 to length(res) do
|
||||
res[i] = permute(i,set)
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
?permutes("abcd")
|
||||
35
Task/Permutations/Ring/permutations.ring
Normal file
35
Task/Permutations/Ring/permutations.ring
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
list = [1, 2, 3, 4]
|
||||
for perm = 1 to 24
|
||||
for i = 1 to len(list)
|
||||
see list[i] + " "
|
||||
next
|
||||
see nl
|
||||
nextPermutation(list)
|
||||
next
|
||||
|
||||
func nextPermutation a
|
||||
elementcount = len(a)
|
||||
if elementcount < 1 then return ok
|
||||
pos = elementcount-1
|
||||
while a[pos] >= a[pos+1]
|
||||
pos -= 1
|
||||
if pos <= 0 permutationReverse(a, 1, elementcount)
|
||||
return ok
|
||||
end
|
||||
last = elementcount
|
||||
while a[last] <= a[pos]
|
||||
last -= 1
|
||||
end
|
||||
temp = a[pos]
|
||||
a[pos] = a[last]
|
||||
a[last] = temp
|
||||
permutationReverse(a, pos+1, elementcount)
|
||||
|
||||
func permutationReverse a, first, last
|
||||
while first < last
|
||||
temp = a[first]
|
||||
a[first] = a[last]
|
||||
a[last] = temp
|
||||
first += 1
|
||||
last -= 1
|
||||
end
|
||||
3
Task/Permutations/Sidef/permutations-1.sidef
Normal file
3
Task/Permutations/Sidef/permutations-1.sidef
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[0,1,2].permutations { |p|
|
||||
say p
|
||||
}
|
||||
21
Task/Permutations/Sidef/permutations-2.sidef
Normal file
21
Task/Permutations/Sidef/permutations-2.sidef
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
func forperm(callback, n) {
|
||||
var idx = @^n
|
||||
|
||||
loop {
|
||||
callback([idx...])
|
||||
|
||||
var p = n-1
|
||||
while (idx[p-1] > idx[p]) {--p}
|
||||
p == 0 && return()
|
||||
|
||||
var d = p
|
||||
idx += idx.splice(p).reverse
|
||||
|
||||
while (idx[p-1] > idx[d]) {++d}
|
||||
idx.swap(p-1, d)
|
||||
}
|
||||
|
||||
return()
|
||||
}
|
||||
|
||||
forperm({|p| say p }, 3)
|
||||
11
Task/Permutations/Sidef/permutations-3.sidef
Normal file
11
Task/Permutations/Sidef/permutations-3.sidef
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
func permutations(callback, set, perm=[]) {
|
||||
set.is_empty && callback(perm)
|
||||
for i in ^set {
|
||||
__FUNC__(callback, [
|
||||
set[(0 ..^ i)..., (i+1 ..^ set.len)...]
|
||||
], [perm..., set[i]])
|
||||
}
|
||||
return()
|
||||
}
|
||||
|
||||
permutations({|p| say p }, [0,1,2])
|
||||
15
Task/Permutations/Swift/permutations.swift
Normal file
15
Task/Permutations/Swift/permutations.swift
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
func perms<T>(var ar: [T]) -> [[T]] {
|
||||
return heaps(&ar, ar.count)
|
||||
}
|
||||
|
||||
func heaps<T>(inout ar: [T], n: Int) -> [[T]] {
|
||||
return n == 1 ? [ar] :
|
||||
Swift.reduce(0..<n, [[T]]()) {
|
||||
(var shuffles, i) in
|
||||
shuffles.extend(heaps(&ar, n - 1))
|
||||
swap(&ar[n % 2 == 0 ? i : 0], &ar[n - 1])
|
||||
return shuffles
|
||||
}
|
||||
}
|
||||
|
||||
perms([1, 2, 3]) // [[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]]
|
||||
6
Task/Permutations/jq/permutations.jq
Normal file
6
Task/Permutations/jq/permutations.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def permutations:
|
||||
if length == 0 then []
|
||||
else
|
||||
range(0;length) as $i
|
||||
| [.[$i]] + (del(.[$i])|permutations)
|
||||
end ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue