Source

codeeval_challenges / 038_StringList / string_perm.py

Full commit
#! /usr/bin/env python
""" Examples:
        >>> generate_permutations(['a'])
        ['a']
        >>> generate_permutations('hat')
        ['aht', 'ath', 'hat', 'hta', 'tah', 'tha']
"""

import sys
from optparse import OptionParser

def generate_permutations(alphabet, base=''):

    if len(alphabet) == 0:
        return [base]

    for idx in range(len(alphabet)):
        char = alphabet[idx]

        remaining = alphabet[:idx] + alphabet[idx:]
        for base in 
            new_bases.append(
                char+generate_permutations(alphabet, bases)
            )
    #new_bases = []
    #for char in alphabet:
    #    for base in bases:
    #        new_bases.append(base + char)

def main():

    parser = OptionParser('%s testcase.txt' % __file__)
    options, args = parser.parse_args()

    if len(args) < 1:
        parser.error("No input file provided.")

    test_cases = open(sys.argv[1], 'r')

    for test in test_cases:
        test = test.strip()

        if test != '':
            length, chars = test.split(',')
            length = int(length)
            chars = set(chars)

            permutations = generate_permutations(chars, length)
            print ",".join(permutations)

    test_cases.close()


if __name__ == "__main__":
    main()