-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination.py
More file actions
55 lines (48 loc) · 1.25 KB
/
combination.py
File metadata and controls
55 lines (48 loc) · 1.25 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
import unittest
class Solution:
def __init__(self):
self.stack = []
def combination(self, arr, offset, length, k, result):
if k == 0:
result.append([''.join(self.stack[:])])
return
for i in range(offset, length):
self.stack.append(arr[i])
self.combination(arr, i, length, k - 1, result)
self.stack.pop()
class TestCombination(unittest.TestCase):
def test(self):
sol = Solution()
arr = ['A', 'B', 'C']
result = []
sol.combination(arr, 0, len(arr), 2, result)
self.assertEqual(
result,
[
['AA'],
['AB'],
['AC'],
['BB'],
['BC'],
['CC'],
]
)
result = []
sol.combination(arr, 0, len(arr), 3, result)
self.assertEqual(
result,
[
['AAA'],
['AAB'],
['AAC'],
['ABB'],
['ABC'],
['ACC'],
['BBB'],
['BBC'],
['BCC'],
['CCC'],
]
)
if __name__ == '__main__':
unittest.TestCase()