Guest User

Untitled

a guest
Dec 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. f([10], [10]) == "e"
  2. f([10, 30, 20], [30, 20, 10]) == "e"
  3. f([10, 30, 20, 40], [30, 20, 40, 10]) == "o"
  4.  
  5. żṗ2</€⁺Sị“oe
  6.  
  7. żṗ2</€⁺Sị“oe Main link. Arguments: A, B (lists)
  8.  
  9. ż Zip A with B. Yields an array of pairs [x, σ(x)].
  10. ṗ2 Generate all pairs [[x, σ(x)], [y, σ(y)]].
  11. </€ Reduce each pair by </€.
  12. This maps [[x, σ(x)], [y, σ(y)]] to [x < y, σ(x) < σ(y)].
  13. ⁺ Repeat the previous link, i.e., execute </€ once more.
  14. This maps [x < y, σ(x) < σ(y)] to ((x < y) < (σ(x) < σ(y))), which is
  15. true if and only if x > y and σ(x) < σ(y).
  16. S Sum. This counts the number of inversions.
  17. ị“oe Retrieve the letter at the corresponding index.
  18. Indexing is 1-based and modular, so an odd sum retrieves the first
  19. letter, an even sum the second.
  20.  
  21. 2$St!<Rz2'oe'w)
  22.  
  23. 2$S % implicitly take two row vectors. Sort second and apply the indices
  24. % of that sorting to the first
  25. t! % duplicate. Transpose into column vector
  26. < % true for elements of the column vector that exceed those of the
  27. % row vector. Gives a 2D array with all pairs of comparisons
  28. R % keep only upper triangular part of that array
  29. z % number of nonzero elements. This is the number of inversions
  30. 2 % parity of that number: gives 0 or 1
  31. 'oe'w % push string 'eo' below the top of the stack
  32. ) % apply index to produce 'e' or 'o'. An index 1 refers to the first
  33. % element, whereas 0 refers to the last. Implicitly display
  34.  
  35. p=@(v)eye(nnz(v))(v,:);@(a,b)'ole'(det(p(a)*p(b))+2)
  36.  
  37. (a,b)=>"eo"[r=0,g=a=>a.map((e,i)=>a.slice(i).map(d=>r^=d<e)),g(a),g(b),r]
  38.  
  39. If[Mod[Plus@@Length/@(Join[{0},#]&)/@PermutationCycles[#][[1]],2]==0,"e","o"]&
  40.  
  41. If[Tr[Signature/@{##}]==0,o,e]&
Add Comment
Please, Sign In to add comment