Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Permutations/Nim/permutations-1.nim
Normal file
5
Task/Permutations/Nim/permutations-1.nim
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import algorithm
|
||||
var v = [1, 2, 3] # List has to start sorted
|
||||
echo v
|
||||
while v.nextPermutation():
|
||||
echo v
|
||||
23
Task/Permutations/Nim/permutations-2.nim
Normal file
23
Task/Permutations/Nim/permutations-2.nim
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
iterator inplacePermutations[T](xs: var seq[T]): var seq[T] =
|
||||
assert xs.len <= 24, "permutation of array longer than 24 is not supported"
|
||||
|
||||
let n = xs.len - 1
|
||||
var
|
||||
c: array[24, int8]
|
||||
i: int = 0
|
||||
|
||||
for i in 0 .. n: c[i] = int8(i+1)
|
||||
|
||||
while true:
|
||||
yield xs
|
||||
if i >= n: break
|
||||
|
||||
c[i] -= 1
|
||||
let j = if (i and 1) == 1: 0 else: int(c[i])
|
||||
swap(xs[i+1], xs[j])
|
||||
|
||||
i = 0
|
||||
while c[i] == 0:
|
||||
let t = i+1
|
||||
c[i] = int8(t)
|
||||
i = t
|
||||
31
Task/Permutations/Nim/permutations-3.nim
Normal file
31
Task/Permutations/Nim/permutations-3.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import intsets
|
||||
from math import fac
|
||||
block:
|
||||
# test all permutations of length from 0 to 9
|
||||
for l in 0..9:
|
||||
|
||||
# prepare data
|
||||
var xs = newSeq[int](l)
|
||||
for i in 0..<l: xs[i] = i
|
||||
var s = initIntSet()
|
||||
|
||||
for cs in inplacePermutations(xs):
|
||||
|
||||
# each permutation must be of length l
|
||||
assert len(cs) == l
|
||||
|
||||
# each permutation must contain digits from 0 to l-1 exactly once
|
||||
var ds = newSeq[bool](l)
|
||||
for c in cs:
|
||||
assert not ds[c]
|
||||
ds[c] = true
|
||||
|
||||
# generate a unique number for each permutation
|
||||
var h = 0
|
||||
for e in cs:
|
||||
h = l * h + e
|
||||
assert not s.contains(h)
|
||||
s.incl(h)
|
||||
|
||||
# check exactly l! unique number of permutations
|
||||
assert len(s) == fac(l)
|
||||
27
Task/Permutations/Nim/permutations-4.nim
Normal file
27
Task/Permutations/Nim/permutations-4.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# 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
|
||||
33
Task/Permutations/Nim/permutations-5.nim
Normal file
33
Task/Permutations/Nim/permutations-5.nim
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# Nim implementation of the (very fast) Go example.
|
||||
# http://rosettacode.org/wiki/Permutations#Go
|
||||
# implementing a recursive https://en.wikipedia.org/wiki/Steinhaus–Johnson–Trotter_algorithm
|
||||
|
||||
import algorithm
|
||||
|
||||
proc perm(s: openArray[int]; emit: proc(emit: openArray[int])) =
|
||||
var s = @s
|
||||
if s.len == 0:
|
||||
emit(s)
|
||||
return
|
||||
|
||||
proc rc(np: int) =
|
||||
if np == 1:
|
||||
emit(s)
|
||||
return
|
||||
var
|
||||
np1 = np - 1
|
||||
pp = s.len - np1
|
||||
|
||||
rc(np1) # Recurse prior swaps.
|
||||
|
||||
for i in countDown(pp, 1):
|
||||
swap s[i], s[i-1]
|
||||
rc(np1) # Recurse swap.
|
||||
|
||||
s.rotateLeft(0..pp, 1)
|
||||
|
||||
rc(s.len)
|
||||
|
||||
var se = @[0, 1, 2, 3] #, 4, 5, 6, 7, 8, 9, 10]
|
||||
|
||||
perm(se, proc(s: openArray[int])= echo s)
|
||||
Loading…
Add table
Add a link
Reference in a new issue