jeudi 27 mai 2010

Generate all anagrams of a string


def perm(t):
if len(t) == 0:
return []
if len(t) == 1:
return [t]
else:
L = []
for i in range (0, len(t)):
selected_c = [t[i]]
subL = []
for j in range (0,i):
subL.append(t[j])
for j in range (i+1,len(t)):
subL.append(t[j])
V = perm(subL)
for s in perm(subL):
L.append(selected_c + s)
return L

print map ( "".join , perm (list("ANNA")))


As usual other solutions to this problem exist

Aucun commentaire:

Enregistrer un commentaire