Add nextPermutation and prevPermutation
Fits best into algorithm module I guess. These are the most general ways, an iterator could easily be implemented from this. Same algorithm as in Rust: http://web.mit.edu/rust-lang_v0.11/doc/src/collections/var/tmp/alexp/rust/rust-0.11.0/src/libcollections/slice.rs.html#644
This commit is contained in:
parent
903ca78289
commit
1ae4d535cd
2 changed files with 76 additions and 0 deletions
17
tests/stdlib/tpermutations.nim
Normal file
17
tests/stdlib/tpermutations.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
discard """
|
||||
output: '''@[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
|
||||
@[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
|
||||
@[0, 1, 2, 3, 4, 5, 6, 8, 9, 7]
|
||||
@[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
|
||||
@[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
|
||||
@[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'''
|
||||
"""
|
||||
import algorithm
|
||||
|
||||
var v = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
for i in 1..3:
|
||||
v.nextPermutation()
|
||||
echo v
|
||||
for i in 1..3:
|
||||
v.prevPermutation()
|
||||
echo v
|
||||
Loading…
Add table
Add a link
Reference in a new issue