Unyeilding a permutation generator

Michele Simionato michele.simionato at gmail.com
Mon Nov 3 11:24:57 EST 2008


On Nov 2, 10:34 pm, silly... at yahoo.com wrote:
> Anyway what I want to do is experiment with code similar to this (i.e.
> same algorithm and keep the recursion) in other languages,
> particularly vbscript and wondered what it would look like if it was
> rewritten to NOT use the yield statement - or at least if it was
> amended so that it can be easily translated to other languages that
> dont have python's yield statement. I think the statement "for perm in
> all_perms(str[1:]):"  will be hardest to replicate in a recursive
> vbscript program for example.
>
> Thanks for any constructive help given.
>

Here is a solution which does not use yield, translittered
from some Scheme code I have:

def perm(lst):
    ll = len(lst)
    if ll == 0:
        return []
    elif ll == 1:
        return [lst]
    else:
        return [[el] + ls for el in lst
                for ls in perm([e for e in lst if not e==el])]

if __name__ == '__main__':
    print perm('abcd')



More information about the Python-list mailing list