Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3a0377e

Browse files
chore: added generic input for combinations (#12)
Co-authored-by: Max Schmitt <max@schmitt.mx>
1 parent5c498f6 commit3a0377e

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-
funcAll(set []string) (subsets [][]string) {
8+
funcAll[Tany](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
forsubsetBits:=1;subsetBits< (1<<length);subsetBits++ {
14-
varsubset []string
14+
varsubset []T
1515

1616
forobject:=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-
funcCombinations(set []string,nint) (subsets [][]string) {
32+
funcCombinations[Tany](set []T,nint) (subsets [][]T) {
3333
length:=uint(len(set))
3434

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

46-
varsubset []string
46+
varsubset []T
4747

4848
forobject:=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+
funcTestIntegerCombinations(t*testing.T) {
82+
tt:= []struct {
83+
namestring
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:=rangett {
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
funcExampleAll() {
82154
combinations:=All([]string{"A","B","C"})
83155
fmt.Println(combinations)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp