diff --git a/book/book_second_edition.pdf b/book/book_second_edition.pdf index 4a2a050..490f54d 100644 Binary files a/book/book_second_edition.pdf and b/book/book_second_edition.pdf differ diff --git a/src/builtin_structures/combinations.py b/src/builtin_structures/combinations.py index a7cd99f..c25a7a6 100755 --- a/src/builtin_structures/combinations.py +++ b/src/builtin_structures/combinations.py @@ -3,21 +3,24 @@ __author__ = "bt3" -def comb_str(l1): - - if len(l1) < 2: - return l1 - - result = [] - for i, c in enumerate(l1): - result.append(c) - for comb in comb_str(l1[i+1:]): - result.append(c + comb) - - return result +def combinations(s): + ''' + >>> combinations('abc') + ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] + >>> combinations('') + '' + ''' + if len(s) < 2: + return s + res = [] + for i, c in enumerate(s): + res.append(c) + for j in combinations(s[:i] + s[i+1:]): + res.append(c + j) + return res if __name__ == '__main__': - l1 = ['a', 'b', 'c'] - print comb_str(l1) + import doctest + doctest.testmod()