summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authordef <dennis@felsin9.de>2015-02-01 18:29:01 +0100
committerdef <dennis@felsin9.de>2015-02-01 18:29:01 +0100
commit1ae4d535cd8ed6b797cf0525eaab9e0c889e0c6d (patch)
treeeaf2460ac515a4e58e87d328cb2f948a7c1f35e6 /tests
parent903ca78289dc0460e07bd449a972a4d203d9a09b (diff)
downloadNim-1ae4d535cd8ed6b797cf0525eaab9e0c889e0c6d.tar.gz
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
Diffstat (limited to 'tests')
-rw-r--r--tests/stdlib/tpermutations.nim17
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/stdlib/tpermutations.nim b/tests/stdlib/tpermutations.nim
new file mode 100644
index 000000000..99bc424cd
--- /dev/null
+++ b/tests/stdlib/tpermutations.nim
@@ -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