Skip to content

Commit 1b83a77

Browse files
committed
more details on permutation example.
1 parent 944ef53 commit 1b83a77

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

algorithms/permutations.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ def permutations(l):
22
"""
33
Generator for list permutations
44
5-
Example: [1,2,3] = [1,2,3], [1,3,2], [2,1,3] ...
6-
75
@param l list to generate permutations for
86
@result yield each permutation
7+
8+
Example:
9+
l = [1,2,3]
10+
a = [1]
11+
permutations([2,3]) = [[2,3], [3,2]]
12+
[2,3]
13+
yield [1,2,3]
14+
yield [2,1,3]
15+
yield [2,3,1]
16+
[3,2]
17+
yield [1,3,2]
18+
yield [3,1,2]
19+
yield [3,2,1]
920
"""
10-
print 'permutations: ',l
1121
if len(l) <= 1:
1222
yield l
1323
else:
@@ -16,3 +26,6 @@ def permutations(l):
1626
for i in range(len(p)+1):
1727
yield p[:i] + a + p[i:]
1828

29+
for p in permutations([1,2,3]):
30+
print p
31+

0 commit comments

Comments
 (0)