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
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue