In: Computer Science
You must write functions permute(...) and permuteAux(...) by analogy with the the functions enumerate(....) and enumAux(....). Basically, the changes are to implement the "without replacement" condition: you must keep track of which letters have been used using a Boolean list in previous stages of the recursion (e.g., B[0] will be true if at an earlier stage of the recursion, 'A' has already been inserted into X; simply don't do the recursive call if that letter has been used).
Print out the sequences for permute(4,3)
CODE FOR enumerate and enumAux
letter = [chr(i) for i in range(ord('A'),ord('Z')+1)] =
def enumerate(N,L):
X = [0] * L
enumAux(N,X,0)
def enumAux(N,X,I):
if(I >= len(X)):
print(X)
else:
for j in range(N):
X[I] = letter[j]
enumAux(N,X,I+1)
enumerate(3,2)
OUTPUT:
['A', 'A'] ['A', 'B'] ['A', 'C'] ['B', 'A'] ['B', 'B'] ['B', 'C'] ['C', 'A'] ['C', 'B'] ['C', 'C']
Program screenshot:
Output:
Code to copy:
letter = [chr(i) for i in range(ord('A'),ord('Z')+1)]
#function enumerate()
def enumerate(N,L):
X = [0] * L
B = [0] * 26
enumAux(N,X,B,0)
#function enumAux()
def enumAux(N,X,B,I):
if(I >= len(X)):
print(X)
else:
for j in range(N):
if not B[j]:
X[I] = letter[j]
B[j] = 1
enumAux(N,X,B,I+1)
B[j] = 0