Skip to content

Instantly share code, notes, and snippets.

Created March 2, 2018 19:23
Show Gist options
  • Save anonymous/3e2fe1aa8f3f0cdccebbae2aa4ef12d7 to your computer and use it in GitHub Desktop.
Save anonymous/3e2fe1aa8f3f0cdccebbae2aa4ef12d7 to your computer and use it in GitHub Desktop.
import algorithm, sequtils
proc permutations(v: seq[int]): seq[seq[int]] =
let nbMaxPerms = toSeq(1..v.len).foldr(a * b) # nb permutations max = (len v)!
result = @[v]
var temp = v
for cpt in 1..<nbMaxPerms:
temp.nextPermutation()
result.add(temp)
var v = toSeq(1..5)
echo permutations(v)
echo v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment