Skip to content

Commit a81773f

Browse files
committed
Added generic input for combinations
1 parent 341af75 commit a81773f

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

combinations.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import "math/bits"
55

66
// All returns all combinations for a given string array.
77
// 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) {
99
length := uint(len(set))
1010

1111
// Go through all possible combinations of objects
1212
// from 1 (only first object in subset) to 2^length (all objects in subset)
1313
for subsetBits := 1; subsetBits < (1 << length); subsetBits++ {
14-
var subset []string
14+
var subset []T
1515

1616
for object := uint(0); object < length; object++ {
1717
// checks if object is contained in subset
@@ -29,7 +29,7 @@ func All(set []string) (subsets [][]string) {
2929

3030
// Combinations returns combinations of n elements for a given string array.
3131
// 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) {
3333
length := uint(len(set))
3434

3535
if n > len(set) {
@@ -43,7 +43,7 @@ func Combinations(set []string, n int) (subsets [][]string) {
4343
continue
4444
}
4545

46-
var subset []string
46+
var subset []T
4747

4848
for object := uint(0); object < length; object++ {
4949
// checks if object is contained in subset

combinations_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,78 @@ func TestStringCombinations(t *testing.T) {
7878
}
7979
}
8080

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: \nreturn:\t%v\nwant:\t%v", out, tc.out)
148+
}
149+
})
150+
}
151+
}
152+
81153
func ExampleAll() {
82154
combinations := All([]string{"A", "B", "C"})
83155
fmt.Println(combinations)

0 commit comments

Comments
 (0)