Skip to content

Commit 792a269

Browse files
author
Mari Wahl
committed
typo fixed in the comb.py 👽
1 parent 62601a8 commit 792a269

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

book/book_second_edition.pdf

283 Bytes
Binary file not shown.

src/builtin_structures/combinations.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
__author__ = "bt3"
44

55

6-
def comb_str(l1):
7-
8-
if len(l1) < 2:
9-
return l1
10-
11-
result = []
12-
for i, c in enumerate(l1):
13-
result.append(c)
14-
for comb in comb_str(l1[i+1:]):
15-
result.append(c + comb)
16-
17-
return result
6+
def combinations(s):
7+
'''
8+
>>> combinations('abc')
9+
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
10+
>>> combinations('')
11+
''
12+
'''
13+
if len(s) < 2:
14+
return s
15+
res = []
16+
for i, c in enumerate(s):
17+
res.append(c)
18+
for j in combinations(s[:i] + s[i+1:]):
19+
res.append(c + j)
20+
return res
1821

1922

2023

2124
if __name__ == '__main__':
22-
l1 = ['a', 'b', 'c']
23-
print comb_str(l1)
25+
import doctest
26+
doctest.testmod()

0 commit comments

Comments
 (0)