From 3bd9a200855a29d252672b8de6b6d2fe67873a63 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 10 May 2009 12:42:31 +0200 Subject: Add perm.py script --- Makefile | 2 +- perm.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100755 perm.py 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 +# +# "THE BEER-WARE LICENSE" (Revision 42): +# 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 -- cgit v1.2.3-54-g00ecf