human
Generate permutations with only adjacent swaps allowed | Python3 program to generate permutations with only one swap allowed ..
don 't swap the current position.
Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character ..
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to generate permutations with only adjacent swaps allowed and the other steps required.
 def findPermutations ( string , index , n ) :
	if index >= n or ( index + 1 ) >= n :
		print ( ' ' . join ( string ) )
		return
	findPermutations ( string , index + 1 , n )
	string [ index ] , string [ index + 1 ] = string [ index + 1 ] , string [ index ]
	findPermutations ( string , index + 2 , n )
	string [ index ] , string [ index + 1 ] = string [ index + 1 ] , string [ index ]
if __name__ == " _ _ main _ _ " :
	string = list ( "12345" )
	n = len ( string )
	findPermutations ( string , 0 , n )