I'm trying to determine whether a permutation is odd or even by counting the transpositions from a sort. Specifically, I'd like to use the int b+c+d
in the attached code so that upon calling the SortCount(A)
function it will give an output of either True
or False
based on whether that value is odd or even.
The tuple that is returned in the included code gives the sorted array and the number of transpositions required to sort that array from it's original configuration. I'm a bit new to using tuples, and programming in general, and am not sure how to make the transposition number available to perform the stated manipulation above. Manipulating B, b+c+d
from the return line for SortCount(A)
breaks the code. The code is attached.
def SortCount(A):
l = len(A)
if l > 1:
n = l//2
C = A[:n]
D = A[n:]
C, c = SortCount(A[:n])
D, d = SortCount(A[n:])
B, b = MergeCount(C,D)
return B, b+c+d
else:
return A, 0
def MergeCount(A,B):
count = 0
M = []
while A and B:
if A[0] <= B[0]:
M.append(A.pop(0))
else:
count += len(A)
M.append(B.pop(0))
M += A + B
return M, count