Skip to content

Instantly share code, notes, and snippets.

@def-
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save def-/28574c7d9703eab87ec5 to your computer and use it in GitHub Desktop.

Select an option

Save def-/28574c7d9703eab87ec5 to your computer and use it in GitHub Desktop.
import algorithm
proc nextPermutation[T](x: var openarray[T]): bool {.discardable.} =
if x.len < 2:
return false
var i = x.high
while i > 0 and x[i-1] >= x[i]:
dec i
if i == 0:
return false
var j = x.high
while j >= i and x[j] <= x[i-1]:
dec j
swap x[j], x[i-1]
x.reverse(i, x.high)
result = true
var v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in 1 .. 999_999:
v.nextPermutation()
echo "The answer is ", @v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment