Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue