Create a python class that initializes a position and an array
Then create a method within that class that decides the next permutation of the array.
Change the current permutation into whatever the next one will be as seen in the johnson trotter algorithm
Ans :-
def permutation(lst):
if len(lst) == 0:
return []
if len(lst) == 1:
return [lst]
l = []
for j in range(len(lst)):
m = lst[j]
remLst = lst[:j] + lst[j+1:]
for p in permutation(remLst):
l.append([m] + p)
return l
data = list('423')
for x in permutation(data):
print x
Get Answers For Free
Most questions answered within 1 hours.