- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPermutation.py
More file actions
Latest commit
61 lines (55 loc) · 1.42 KB
/
Copy pathPermutation.py
File metadata and controls
61 lines (55 loc) · 1.42 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
given a string, return all the permutations
based on the characters of the string
'''
defPermutationStr(string, chars_stack=None, used_chars=None):
ifchars_stackisNone:
chars_stack= []
ifused_charsisNone:
used_chars= [Falsefor_inrange(len(string))]
ifall(used_chars):
yield"".join(chars_stack)
else:
foriinrange(len(string)):
ifnotused_chars[i]:
used_chars[i] =True
chars_stack.append(string[i])
forstring1inPermutationStr(string,
chars_stack=chars_stack,
used_chars=used_chars):
yieldstring1
used_chars[i] =False
chars_stack.pop()
forperminPermutationStr("abcd"):
printperm
defPermutation1(string):
out= []
used= [Falseforxinstring]
Permute(string, out, used)
defPermute(string,out,used):
iflen(out) ==len(string) :
print"> %s"%"".join(out)
return
foriinrange(len(string)):
ifused[i]:
continue
out.append(string[i])
used[i] =True
Permute(string, out, used)
used[i] =False
out.pop()
Permutation1('erty')
forain [1,2,3]:
forbin ['a','b','c']:
forcin [10,11,12]:
fordin ['z', 'y', 'x']:
forein ['q', 'r', 't']:
printa, b, c, d, e
arrays= [
[1,2,3],
['a','b','c'],
[10,11,12],
['z', 'y', 'x'],
['q', 'r', 't'],
[3, 4, 5]
]