Thursday, August 10, 2017

Permutation Function with Repetition

def perm(l=None, n=None, str_a=None, perm_a=None):
    if len(str_a) == n:
        return [str_a] + perm_a
    else:
        new_perm_a = perm_a
        for c in l:
            new_perm_a = perm(l=l, n=n, str_a=str_a + c, perm_a=new_perm_a)
        return new_perm_a

def permutations(l=None, n=None):
    str_a, perm_a = '', []
    result = perm(l=l, n=n, str_a=str_a, perm_a=perm_a)
    return result

lst = permutations(l=['a', 'b', 'c', 'd'], n=3)
print(lst)

No comments:

Post a Comment