14

# 背景

sgn（σ）=（-1）^ N（σ）

sgn（σ）=（-1）^ m

# 挑战

• 接受两个输入数组（或字符串）作为参数，分别表示排列前后的集合。
• 给定排列，返回或打印`e`偶数或`o`奇数字符。
• 应该假设数组或字符串中的所有索引都具有唯一值。

### 测试用例

``````f([10], [10]) == "e"
f([10, 30, 20], [30, 20, 10]) == "e"
f([10, 30, 20, 40], [30, 20, 40, 10]) == "o"
``````

4

CalculatorFeline

@CatsAreFluffy更好吗？

CalculatorFeline

5

# 果冻，13 12字节

``````żṗ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.
``````

1

Patrick Roberts

6

# MATL，17 16字节

``````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
``````

1

Alex A.

@AlexA。谢谢！我已经编辑了答案以阐明预排序部分的作用：我们对一个数组进行排序，然后将排序所需的相同重排应用于另一个数组。
Luis Mendo

1

@Dennis是的，我经常想到这一点...但是目前，它使用负值含义不同的格式。我选择这样做是为了具有形式`x(1:end-2)`等的索引，而没有明确指出的大小`x`。不确定这是否是一个不错的选择，但我想现在更改为时已晚：-)也许我会找到一种兼容的方式来添加模块化索引
Luis Mendo

...并且超过当前长度的索引用于分配新值。但是索引`0`确实具有“最后一个入口”的含义，因此我可以保存一个字节（删除增量）。谢谢你的主意！
Luis Mendo

5

# 八度，56 52字节

``````p=@(v)eye(nnz(v))(v,:);@(a,b)'ole'(det(p(a)*p(b))+2)
``````

2

Luis Mendo

5

``k%l|m<-zip k l=cycle"eo"!!sum[1|(a,b)<-m,(c,d)<-m,a<c,b>d]``

``````*Main> [8,3,5]%[5,3,8]
'o'``````

1

4

## Python 2，68个字节

``lambda*M:"eo"[sum(a<b<M>A>B for a,A in zip(*M)for b,B in zip(*M))%2]``

``````>>> f=lambda*M:"eo"[sum(a<b<M>A>B for a,A in zip(*M)for b,B in zip(*M))%2]
>>> f([8,3,5],[5,3,8])
'o'``````

3

## JavaScript（ES6），73个字节

``````(a,b)=>"eo"[r=0,g=a=>a.map((e,i)=>a.slice(i).map(d=>r^=d<e)),g(a),g(b),r]
``````

1

Patrick Roberts

2

# Mathematica，77个字节

``````If[Mod[Plus@@Length/@(Join[{0},#]&)/@PermutationCycles[#][[1]],2]==0,"e","o"]&
``````

Patrick Roberts

CalculatorFeline

2

# Mathematica，31个字节

``````If[Tr[Signature/@{##}]==0,o,e]&
``````

Signature [list]给出将列表元素按规范顺序放置所需的排列签名