signature.py

Created by nicolas-patrois

Created on June 09, 2018

126 Bytes

Calculates the signature of a permutation. The permutation is written this way: (3,5,2,4,1) and it’s not a 5-cycle but the permutation p whose image of 3 is 2. In a nutshell, s(1)=3, s(2)=5, s(3)=4 and s(5)=1.


def signature(p):
 s=1
 for i in range(len(p)-1):
  for j in range(i+1,len(p)):
   s^=p[i]>p[j]
 return 2*s-1