1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class permutation(object):
def __init__(self,initArray):
self.__permResult = []
if len(initArray) > 1:
self.generatePerm(0,initArray)
else :
self.__permResult = initArray
def generatePerm(self,i,array):
if i for index in range(i,len(array)):
array[i],array[index] = array[index],array[i]
self.generatePerm(i+1,array)
array[i],array[index] = array[index],array[i]
else :
print(array)
self.__permResult.append(array)
# changing the above line to:
# self.__permResult = self.__permResult + array
# would give me something like [1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1]
def printResult(self):
print(self.__permResult)
test = permutation([1,2,3])
test.printResult() |