summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2009-05-10 12:42:31 +0200
committerTobias Klauser <tklauser@distanz.ch>2009-05-10 12:42:31 +0200
commit3bd9a200855a29d252672b8de6b6d2fe67873a63 (patch)
tree29b5240fd89d28625f168680859400883d4b041e
parent83bca5af259daaa1865a5c165de42b7ee5bb7dab (diff)
Add perm.py script
-rw-r--r--Makefile2
-rwxr-xr-xperm.py43
2 files changed, 44 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 4764de2..6e4ba24 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
BINDIR = $(HOME)/bin
-TARGETS = mkheader.py isprime multi-send-key
+TARGETS = mkheader.py isprime multi-send-key perm.py
all: $(TARGETS)
@echo "Nothing to be done"
diff --git a/perm.py b/perm.py
new file mode 100755
index 0000000..cabb68f
--- /dev/null
+++ b/perm.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+#
+# perm.py - print all possible permutations of a word
+#
+# Copyright 2009 Tobias Klauser <tklauser@distanz.ch>
+#
+# "THE BEER-WARE LICENSE" (Revision 42):
+# <tklauser@distanz.ch> wrote this file. As long as you retain this notice you
+# can do whatever you want with this stuff. If we meet some day, and you think
+# this stuff is worth it, you can buy me a beer in return
+# Tobias Klauser
+
+
+from __future__ import generators
+import sys, os
+
+def usage():
+ print "usage: %s WORD..." % (os.path.basename(sys.argv[0]))
+
+def xcombinations(items, n):
+ if n==0:
+ yield []
+ else:
+ for i in xrange(len(items)):
+ for cc in xcombinations(items[:i] + items[i+1:], n-1):
+ yield [items[i]] + cc
+
+def xpermutations(items):
+ return xcombinations(items, len(items))
+
+def main():
+
+ if len(sys.argv[1:]) < 1:
+ usage()
+
+ for w in sys.argv[1:]:
+ print "Permutations of %s:" % w
+ for p in xpermutations(w): print " %s" % ''.join(p)
+
+if __name__=="__main__":
+ main()
+
+# vim: expandtab shiftwidth=4