-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_1128.py
28 lines (24 loc) · 871 Bytes
/
problem_1128.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Given a number in the form of a list of digits, return all possible permutations.
For example, given [1,2,3],
return [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]].
"""
def permutate(source):
"""
Given an array of integers, find all the possible permutations.
"""
permutations = []
def helper(index, source):
if index == len(source)-1:
permutations.append(source[:])
else:
for pos in range(index, len(source)):
source[index], source[pos] = source[pos], source[index]
helper(index+1, source)
source[index], source[pos] = source[pos], source[index]
helper(0, source)
return permutations
if __name__ == "__main__":
source = [1 , 2 , 3]
permutations = permutate(source)
print(f"[main] the permutations calculated are :: {permutations}")