diff options
-rw-r--r-- | Makefile | 2 | ||||
-rwxr-xr-x | perm.py | 43 |
2 files changed, 44 insertions, 1 deletions
@@ -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" @@ -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 |