Parity of a Permutation
MATL, 17 16 bytes
1 byte removed thanks to a suggestion by Dennis
2$St!<Rz2\'oe'w)
This works in current version (15.0.0) of the language.
Try it online!
Explanation
This uses the definition of parity in terms of inversions. An inversion is a pair of elements in the second array that are in the "wrong" order compared with the first array. Since the first array need not be sorted, we first sort it and the same rearrangement needed for that sorting is applied to the second array. Then an inversion corresponds to a pair of elements that is not increasing in the second array.
Note also that the two input arrays can be swapped and the result is the same. So it's not important which array is considered to be the "original" and which the "permuted".
2$S % implicitly take two row vectors. Sort second and apply the indices
% of that sorting to the first
t! % duplicate. Transpose into column vector
< % true for elements of the column vector that exceed those of the
% row vector. Gives a 2D array with all pairs of comparisons
R % keep only upper triangular part of that array
z % number of nonzero elements. This is the number of inversions
2\ % parity of that number: gives 0 or 1
'oe'w % push string 'eo' below the top of the stack
) % apply index to produce 'e' or 'o'. An index 1 refers to the first
% element, whereas 0 refers to the last. Implicitly display
Haskell, 58 bytes
k%l|m<-zip k l=cycle"eo"!!sum[1|(a,b)<-m,(c,d)<-m,a<c,b>d]
Usage:
*Main> [8,3,5]%[5,3,8]
'o'
Same method as my Python answer. proud haskeller saved a byte with cycle
.
Octave, 56 52 bytes
It seems nobody is using this approach so far: Basically I'm just using the determinants of the corresponding permutation matrices. The expression det(eye(nnz(a))(a,:))
returns the determinant of the permutation matrix defined by the vector a
. Then it is just a matter of extracting the right character from the string, depending on the result.
p=@(v)eye(nnz(v))(v,:);@(a,b)'ole'(det(p(a)*p(b))+2)