# Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 1.82 KB  |  hits: 15  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Can anyone help me check this python code? [closed]
2. def permutation(Ori, Curr, used):
3.   if len(Ori) == len(Curr):
4.       #print Curr
5.       return
6.
7.   for i in xrange(len(Ori)):
8.       if used[i]:
9.           continue
10.       used[i] = True
11.       Curr.append(Ori[i])
12.       print Curr,i," after append"
13.       permutation(Ori, Curr, used)    # further level
14.       used[i] = False
15.       print Curr,i," before delete"
16.       Curr = Curr[0:-1]               # Curr.pop() works
17.       print Curr,i," after delete"
18.   return
19.
20. if __name__ == "__main__":
21.   used = [False]*3
22.   Curr = []
23.   permutation([1,2,3], Curr, used)
24.
25. [1] 0  after append
26. [1, 2] 1  after append
27. [1, 2, 3] 2  after append
28. [1, 2, 3] 2  before delete
29. [1, 2] 2  after delete     <------
30. [1, 2, 3] 1  before delete <------
31. [1, 2] 1  after delete
32. [1, 2, 3] 2  after append
33. [1, 2, 3] 2  before delete
34. [1, 2] 2  after delete
35. [1, 2, 3] 0  before delete
36. [1, 2] 0  after delete
37. [1, 2, 2] 1  after append
38. [1, 2, 2] 1  before delete
39. [1, 2] 1  after delete
40. [1, 2, 3] 2  after append
41. [1, 2, 3] 2  before delete
42. [1, 2] 2  after delete
43.
44. def f(A):
45.      if(len(A) == 10):
46.          return
47.      A.append('a')
48.      f(A)
49.      print A
50.  #   A = A[0:-1]
51.  #   A.pop()
52.      return
53.
54.  if __name__ == "__main__":
55.     f([])
56.
57. permutation(Ori, Curr, used)    # further level
58.
59. permutation(Ori, Curr[:], used)    # further level
60.
61. Curr.append(Ori[i])
62.
63. Curr = Curr + [Ori[i]]
64.
65. def permutations(iter):
66.     """
67.         >>> print(list( permutations(range(3)) ))
68.         [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
69.     """
70.     elements = list(iter)
71.
72.     # base case
73.     if len(elements)==0:
74.         yield []
75.
76.     for i,elem in enumerate(elements):
77.         withoutElem = elements[:i]+elements[i+1:]
78.         for perm in permutations(withoutElem):
79.             yield [elem]+perm