def replace_leaf(arg, leaves, new_leaves, op, neg):
 for (idx, x) in enumerate(leaves):
  if (x[0] == arg):
   x[1][0] ^= neg
   x[1][1] = op(arg)
   leaves.pop(idx)
   new_leaves.append(x)
   return True
 return False

def replace_leaf ( arg , leaves , new_leaves , op , neg ) : for ( idx , x ) in enumerate ( leaves ) : if ( x [ 0 ] == arg ) : x [ 1 ] [ 0 ] ^= neg x [ 1 ] [ 1 ] = op ( arg ) leaves . pop ( idx ) new_leaves . append ( x ) return True return False
attempt to replace a leaf of a multiplication tree .