##### Permutations # A permutation of the number 0 to n-1 can be represented by an Array containing # only the number 0 to n-1, each number occurring only once in the array. P1=[0,2,1] # is permutation P2=[4,2,1,3] #is not P3=[0,1,2,3] # identity permutation P3[i]==i P4 = [5,2,2,0] # is not def counts(x,A): # returns number of times that x is found in A cx = 0 for y in A: if (x==y): cx += 1 return cx # def isperm1(P): # return True/False depending on if P is a permutation n = len(P) # check each number from 0 to n-1 occurs once in P answer = True i = 0 while answer and i<=n-1: if count(i,P)!= 1: answer = False i += 1 # return answer #isperm1 - O(n**2), not very efficient def isperm2(P): n = len(P) B = [] i = 0 answer = True while answer and i < n: if P[i] < 0 or P[i] >=n: answer = False elif P[i] in B: answer = False else: B.append(P[i]) i += 1 #while return answer #isperm2, better than isperm1 but still O(n**2) def isperm(P): n = len(P) C = [0]*n answer = True i = 0 while answer and i=n: answer=False else: C[ P[i] ] += 1 i += 1 #while i = 0 while answer and i