Permute.py

# File permute.py def permute1(seq): if not seq: # Shuffle any sequence: list return [seq] # Empty sequence else: res = [] for i in range(len(seq)): rest = seq[:i] + seq[i+1:] # Delete current node for x in permute1(rest): #Permute the others res.append(seq[i:i+1] + x) #Add node at front return res def permute2(seq): if not seq: # Shuffle any : generator yield seq # Empty sequence else: for i in range(len(seq)): rest = seq[:i] + seq[i+1:] #Delete current node for x in permute2(rest): #Permute the others yield seq[i:i+1] + x #Add node at front
Permutations: All possible combinations These techniques have many other real-world applications—consider generating at- tachments in an email message or points to be plotted in a GUI. Moreover, other types of sequence scrambles serve central roles in other applications, from searches to math- ematics. As is, our sequence scrambler is a simple reordering, but some programs war- rant the more exhaustive set of all possible orderings we get from permutations—pro- duced using recursive functions in both list-builder and generator forms by the follow- ing module file.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.