def permute(a):
 a.sort()
 (yield list(a))
 if (len(a) <= 1):
  return
 first = 0
 last = len(a)
 while 1:
  i = (last - 1)
  while 1:
   i = (i - 1)
   if (a[i] < a[(i + 1)]):
    j = (last - 1)
    while (not (a[i] < a[j])):
     j = (j - 1)
    (a[i], a[j]) = (a[j], a[i])
    r = a[(i + 1):last]
    r.reverse()
    a[(i + 1):last] = r
    (yield list(a))
    break
   if (i == first):
    a.reverse()
    return

def permute ( a ) : a . sort ( ) ( yield list ( a ) ) if ( len ( a ) <= 1 ) : return first = 0 last = len ( a ) while 1 : i = ( last - 1 ) while 1 : i = ( i - 1 ) if ( a [ i ] < a [ ( i + 1 ) ] ) : j = ( last - 1 ) while ( not ( a [ i ] < a [ j ] ) ) : j = ( j - 1 ) ( a [ i ] , a [ j ] ) = ( a [ j ] , a [ i ] ) r = a [ ( i + 1 ) : last ] r . reverse ( ) a [ ( i + 1 ) : last ] = r ( yield list ( a ) ) break if ( i == first ) : a . reverse ( ) return
returns generator of all permutations of a the following code is an in-place permutation of a given list .