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

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.