File tree Expand file tree Collapse file tree 2 files changed +76
-4
lines changed Expand file tree Collapse file tree 2 files changed +76
-4
lines changed Original file line number Diff line number Diff line change @@ -5,13 +5,13 @@ import "math/bits"
5
5
6
6
// All returns all combinations for a given string array.
7
7
// This is essentially a powerset of the given set except that the empty set is disregarded.
8
- func All (set []string ) (subsets [][]string ) {
8
+ func All [ T any ] (set []T ) (subsets [][]T ) {
9
9
length := uint (len (set ))
10
10
11
11
// Go through all possible combinations of objects
12
12
// from 1 (only first object in subset) to 2^length (all objects in subset)
13
13
for subsetBits := 1 ; subsetBits < (1 << length ); subsetBits ++ {
14
- var subset []string
14
+ var subset []T
15
15
16
16
for object := uint (0 ); object < length ; object ++ {
17
17
// checks if object is contained in subset
@@ -29,7 +29,7 @@ func All(set []string) (subsets [][]string) {
29
29
30
30
// Combinations returns combinations of n elements for a given string array.
31
31
// For n < 1, it equals to All and returns all combinations.
32
- func Combinations (set []string , n int ) (subsets [][]string ) {
32
+ func Combinations [ T any ] (set []T , n int ) (subsets [][]T ) {
33
33
length := uint (len (set ))
34
34
35
35
if n > len (set ) {
@@ -43,7 +43,7 @@ func Combinations(set []string, n int) (subsets [][]string) {
43
43
continue
44
44
}
45
45
46
- var subset []string
46
+ var subset []T
47
47
48
48
for object := uint (0 ); object < length ; object ++ {
49
49
// checks if object is contained in subset
Original file line number Diff line number Diff line change @@ -78,6 +78,78 @@ func TestStringCombinations(t *testing.T) {
78
78
}
79
79
}
80
80
81
+ func TestIntegerCombinations (t * testing.T ) {
82
+ tt := []struct {
83
+ name string
84
+ in []int
85
+ out [][]int
86
+ }{
87
+ {
88
+ name : "Empty slice" ,
89
+ in : []int {},
90
+ out : nil ,
91
+ },
92
+ {
93
+ name : "Single item" ,
94
+ in : []int {1 },
95
+ out : [][]int {
96
+ {1 },
97
+ },
98
+ },
99
+ {
100
+ name : "Two items" ,
101
+ in : []int {1 , 2 },
102
+ out : [][]int {
103
+ {1 },
104
+ {2 },
105
+ {1 , 2 },
106
+ },
107
+ },
108
+ {
109
+ name : "Three items" ,
110
+ in : []int {1 , 2 , 3 },
111
+ out : [][]int {
112
+ {1 },
113
+ {2 },
114
+ {1 , 2 },
115
+ {3 },
116
+ {1 , 3 },
117
+ {2 , 3 },
118
+ {1 , 2 , 3 },
119
+ },
120
+ },
121
+ {
122
+ name : "Four items" ,
123
+ in : []int {1 , 2 , 3 , 4 },
124
+ out : [][]int {
125
+ {1 },
126
+ {2 },
127
+ {1 , 2 },
128
+ {3 },
129
+ {1 , 3 },
130
+ {2 , 3 },
131
+ {1 , 2 , 3 },
132
+ {4 },
133
+ {1 , 4 },
134
+ {2 , 4 },
135
+ {1 , 2 , 4 },
136
+ {3 , 4 },
137
+ {1 , 3 , 4 },
138
+ {2 , 3 , 4 },
139
+ {1 , 2 , 3 , 4 },
140
+ },
141
+ },
142
+ }
143
+ for _ , tc := range tt {
144
+ t .Run (tc .name , func (t * testing.T ) {
145
+ out := All (tc .in )
146
+ if ! reflect .DeepEqual (out , tc .out ) {
147
+ t .Errorf ("error: \n return:\t %v\n want:\t %v" , out , tc .out )
148
+ }
149
+ })
150
+ }
151
+ }
152
+
81
153
func ExampleAll () {
82
154
combinations := All ([]string {"A" , "B" , "C" })
83
155
fmt .Println (combinations )
You can’t perform that action at this time.
0 commit comments