Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f([10], [10]) == "e"
- f([10, 30, 20], [30, 20, 10]) == "e"
- f([10, 30, 20, 40], [30, 20, 40, 10]) == "o"
- żṗ2</€⁺Sị“oe
- żṗ2</€⁺Sị“oe Main link. Arguments: A, B (lists)
- ż Zip A with B. Yields an array of pairs [x, σ(x)].
- ṗ2 Generate all pairs [[x, σ(x)], [y, σ(y)]].
- </€ Reduce each pair by </€.
- This maps [[x, σ(x)], [y, σ(y)]] to [x < y, σ(x) < σ(y)].
- ⁺ Repeat the previous link, i.e., execute </€ once more.
- This maps [x < y, σ(x) < σ(y)] to ((x < y) < (σ(x) < σ(y))), which is
- true if and only if x > y and σ(x) < σ(y).
- S Sum. This counts the number of inversions.
- ị“oe Retrieve the letter at the corresponding index.
- Indexing is 1-based and modular, so an odd sum retrieves the first
- letter, an even sum the second.
- 2$St!<Rz2'oe'w)
- 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
- p=@(v)eye(nnz(v))(v,:);@(a,b)'ole'(det(p(a)*p(b))+2)
- (a,b)=>"eo"[r=0,g=a=>a.map((e,i)=>a.slice(i).map(d=>r^=d<e)),g(a),g(b),r]
- If[Mod[Plus@@Length/@(Join[{0},#]&)/@PermutationCycles[#][[1]],2]==0,"e","o"]&
- If[Tr[Signature/@{##}]==0,o,e]&
Add Comment
Please, Sign In to add comment