calculate the inverse of a permutation
173764
private static int[] inverse(int[] perm) {
        int[] inv = new int[perm.length];
        for (int i = 0, len = perm.length; i < len; i++)
            inv[perm[i]] = i;
        return inv;
    }
609181
def BB(n):
    """"""
    if (n<=1):return Context('0\n1')
    else:
        BB1=BB(n-1)
        AA1=AA(n-1)
        r1 = C1((n-1)*2**(n-2),2**(n-1)) - AA1 - BB1
        r2 = BB1 - C1(2**(n-1),2**(n-1)) - BB1;
        return r1 + r2
382519
function reverseInplace(array) {
  for (var i = 0; i < array.length / 2; ++i) {
    var h = array[i];
    var ii = array.length - i - 1;
    array[i] = array[ii];
    array[ii] = h;
  }
}
222854
def reverse(d):
    """"""
    r={}
    for k in d:
        r[d[k]]=k
    return r
1111719
def _inverse_i(self, y, i):
        """"""
        lb = self._lb[self._index(i)]
        ub = self._ub[self._index(i)]
        al = self._al[self._index(i)]
        au = self._au[self._index(i)]
        if 1 < 3:
            if not lb <= y <= ub:
                raise ValueError('argument of inverse must be within the given bounds')
        if y < lb + al:
            return (lb - al) + 2 * (al * (y - lb))**0.5
        elif y < ub - au:
            return y
        else:
            return (ub + au) - 2 * (au * (ub - y))**0.5
1155465
function permutations(iterable, r) {
    // (ABCD, 2) -> AB AC AD BA BC BD CA CB CD DA DB DC
    // (012,  3) -> 012 021 102 120 201 210
/*
    0 1 2 3, 3
    ______
    (0, 1, 2)
    (0, 1, 3)
    (0, 2, 1)
    (0, 2, 3)
    (0, 3, 1)
    (0, 3, 2)
    (1, 0, 2)
    (1, 0, 3)
    (1, 2, 0)
    (1, 2, 3)
    (1, 3, 0)
    (1, 3, 2)
    (2, 0, 1)
    (2, 0, 3)
    (2, 1, 0)
    (2, 1, 3)
    (2, 3, 0)
    (2, 3, 1)
    (3, 0, 1)
    (3, 0, 2)
    (3, 1, 0)
    (3, 1, 2)
    (3, 2, 0)
    (3, 2, 1)
*/

    var pool = tools.toArray(tools.ensureIter(iterable));
    var n = pool.length;
    r = r || n;
    if (r > n) {
        throw new Error(
            'Parameter r cannot be greater than amount of elements in the iterable [' + n + ']'
        );
    }
    var indices = tools.newArray(r, 0, 1);

    function fromIndices() {
        state.current = indices.map(function(idx) {
            state.used[idx] = true;
            return pool[idx];
        });
    }

    function next() {
        // 0. indices: 0, 1, 2
        // 1. indices: 0, 1, 3
        // 2. indices: 0, 1, 4 -> carry = t 0, 1, -1 -> 0, 1, 0 -> 0, 1, 1 -> 0, 1, 2
        var backtrack = null;
        var i = indices.length - 1;
        for (; i >= 0; i--) {
            indices[i]++;
            if (indices[i] === n) {
                // Time to reset current index and increase previous one.
                if (i === 0) {
                    // There is no previous index, so it's the end of generation.
                    continue;
                }

                delete state.used[indices[i - 1]]; // Release previous index
                backtrack = indices[i] - 1;        // Pre-save current index to be able to release it in future.
                indices[i] = -1;                   // Reset current index
                i++;
                continue;
            }

            if (state.used[indices[i]]) {
                // Trying to increase current index until find the
                // lowest free value or reach the end of the range.
                i++;
                continue;
            }

            // First free value found. Release previous one.
            if (backtrack !== null) {
                // If we need to backtrack, doing it!
                delete state.used[backtrack];
                backtrack = null;
                continue;
            }

            // Otherwise exiting from the generation.
            break;
        }

        if (i < 0) {
            state.finished = true;
        } else {
            fromIndices();
        }
    }

    var state = {current: null, finished: false, used: {}};
    var permutIter = {
        next: function() {
            state.current
                ? next()
                : fromIndices();

            return {
                done: state.finished,
                value: state.finished
                    ? undefined
                    : state.current
            };
        }
    };
    return tools.fuseIter(permutIter, function() { return permutIter; });
}
868761
def _ppf(self, q, dist, cache):
        """"""
        return -evaluation.evaluate_inverse(dist, 1-q, cache=cache)
187307
def inverse(x, p, errorOnFail=False):
    
    # Check types
    assertType(x, BigInt)

    # There are a number of ways in RELIC to compute this inverse, but
    # for simplicity, we'll use the extended GCD algorithm because it 
    # involves less type conversions. On the initial development platform
    # Lehmer's algorithm was the most performant: we call it directly.
    gcd = BigInt()
    inv = BigInt()

    # bn_gcd_ext(c, d, e, a, b) computes: c = a*d + b*e
    # We take x=a. b=p, and expect: c = 1 = gcd(x,p), d = 1/x, and e is unused.
    librelic.bn_gcd_ext_lehme(byref(gcd), byref(inv), None, byref(x), byref(p))

    # Check that GCD == 1 
    if gcd != 1:
        if errorOnFail:
            raise Exception("Cannot find an inverse. gcd(x,p) == {}, but we need gcd(x,p) == 1 to find an inverse.".
                format(long(gcd)))
        else:
            return None

    return inv
703338
function(xs, ys, matrix){
  var lcs = [];
  for(var i = xs.size, j = ys.size; i !== 0 && j !== 0;){
    if (matrix[i][j] === matrix[i-1][j]){ i--; }
    else if (matrix[i][j] === matrix[i][j-1]){ j--; }
    else{
      if(Immutable.is(xs.get(i-1), ys.get(j-1))){
        lcs.push(xs.get(i-1));
        i--;
        j--;
      }
    }
  }
  return lcs.reverse();
}
613459
private int permutationParity(int[] vs) {
        int n = 0;
        for (int i = 0; i < vs.length; i++)
            for (int j = i + 1; j < vs.length; j++)
                if (vs[i] > vs[j]) n++;
        return (n & 0x1) == 1 ? -1 : 1;
    }
646934
def permute(elements):
    
    if len(elements) <= 1:
        return [elements]
    else:
        tmp = []
        for perm in permute(elements[1:]):
            for i in range(len(elements)):
                tmp.append(perm[:i] + elements[0:1] + perm[i:])
        return tmp
1318513
private static final void next_perm(final int[] permutation, int position)
    {
        int perm0 = permutation[0];

        for (int i = 0; i < position; ++i)
            permutation[i] = permutation[i + 1];
        permutation[position] = perm0;
    }
222659
private int modInverse(int x) {
		// Cube gives inverse mod 2^4, as x^4 == 1 (mod 2^4) for all odd x.
		int inverse = x * x * x;
		// Newton iteration doubles correct bits at each step.
		inverse *= 2 - x * inverse;
		inverse *= 2 - x * inverse;
		inverse *= 2 - x * inverse;
		return inverse;
	}
454136
function inverse$10(p) {
        var u, deltav, s, d, eps, ro, fi1;
        var ok;

        /* Transformation */
        /* revert y, x*/
        var tmp = p.x;
        p.x = p.y;
        p.y = tmp;
        if (!this.czech) {
            p.y *= -1;
            p.x *= -1;
        }
        ro = Math.sqrt(p.x * p.x + p.y * p.y);
        eps = Math.atan2(p.y, p.x);
        d = eps / Math.sin(this.s0);
        s = 2 * (Math.atan(Math.pow(this.ro0 / ro, 1 / this.n) * Math.tan(this.s0 / 2 + this.s45)) - this.s45);
        u = Math.asin(Math.cos(this.ad) * Math.sin(s) - Math.sin(this.ad) * Math.cos(s) * Math.cos(d));
        deltav = Math.asin(Math.cos(s) * Math.sin(d) / Math.cos(u));
        p.x = this.long0 - deltav / this.alfa;
        fi1 = u;
        ok = 0;
        var iter = 0;
        do {
            p.y = 2 * (Math.atan(Math.pow(this.k, - 1 / this.alfa) * Math.pow(Math.tan(u / 2 + this.s45), 1 / this.alfa) * Math.pow((1 + this.e * Math.sin(fi1)) / (1 - this.e * Math.sin(fi1)), this.e / 2)) - this.s45);
            if (Math.abs(fi1 - p.y) < 0.0000000001) {
                ok = 1;
            }
            fi1 = p.y;
            iter += 1;
        } while (ok === 0 && iter < 15);
        if (iter >= 15) {
            return null;
        }

        return (p);
    }
629934
function nth(list, indexNum) {
    if (arguments.length == 1) {
      // Manually currying
      indexNum = list;
      return (list) => nth(list, indexNum);
    }

    if(indexNum >= 0) {
      return list[+indexNum]
    };
    return [...list].reverse()[list.length+indexNum];
  }
93348
def next_permutation(tab):
    
    n = len(tab)
    pivot = None                         # find pivot
    for i in range(n - 1):
        if tab[i] < tab[i + 1]:
            pivot = i
    if pivot is None:                    # tab is already the last perm.
        return False
    for i in range(pivot + 1, n):        # find the element to swap
        if tab[i] > tab[pivot]:
            swap = i
    tab[swap], tab[pivot] = tab[pivot], tab[swap]
    i = pivot + 1
    j = n - 1                            # invert suffix
    while i < j:
        tab[i], tab[j] = tab[j], tab[i]
        i += 1
        j -= 1
    return True
41260
def invert(d):
    
    out = {}
    for k, v in iteritems(d):
        try:
            out[v].add(k)
        except KeyError:
            out[v] = {k}
    return out