summaryrefslogtreecommitdiff
path: root/perm.py
blob: cabb68fb26fdb03bf356f9213e762bf6d8b12d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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