To check if swapping two indices places them in their correct position
if ( pos3 != pos1 ) : NEW_LINE INDENT indices . append ( [ pos1 , pos2 , pos3 ] ) NEW_LINE arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ] NEW_LINE arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ] NEW_LINE pos . remove ( pos2 ) NEW_LINE if pos3 in pos : NEW_LINE INDENT pos . remove ( pos3 ) NEW_LINE DEDENT if ( arr [ pos1 ] == pos1 ) : NEW_LINE INDENT pos . remove ( pos1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( pos3 == pos [ 0 ] ) : NEW_LINE INDENT it = 0 NEW_LINE if ( pos [ 0 ] != pos [ - 1 ] ) : NEW_LINE INDENT it = it + 1 NEW_LINE pos3 = pos [ it ] NEW_LINE if ( pos [ it ] != pos [ - 1 ] and pos3 == pos2 ) : NEW_LINE INDENT it = it + 1 NEW_LINE pos3 = pos [ it ] NEW_LINE DEDENT elif ( pos [ it ] == pos [ - 1 ] and pos3 == pos2 ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT indices . append ( [ pos1 , pos2 , pos3 ] ) NEW_LINE arr [ pos1 ] , arr [ pos2 ] = arr [ pos2 ] , arr [ pos1 ] NEW_LINE arr [ pos1 ] , arr [ pos3 ] = arr [ pos3 ] , arr [ pos1 ] NEW_LINE pos . remove ( pos2 ) NEW_LINE DEDENT if ( arr [ i ] != i ) : NEW_LINE i = i - 1 NEW_LINE if ( flag == True or len ( indices ) > moves ) : NEW_LINE print ( " Not ▁ Possible " ) NEW_LINE else : NEW_LINE