Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 10cfda1

Browse files
committed
fix: GCDSlice returns 1 when slice is empty
Signed-off-by: lowzj <[email protected]>
1 parent a8ade02 commit 10cfda1

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

dfget/config/supernode_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func ParseNodesSlice(value []string) ([]*NodeWeight, error) {
152152
for _, v := range nodeWeightSlice {
153153
result = append(result, &NodeWeight{
154154
Node: v.Node,
155-
Weight: (v.Weight / gcdNumber),
155+
Weight: v.Weight / gcdNumber,
156156
})
157157
}
158158

pkg/algorithm/algorithm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
package algorithm
1818

1919
// GCDSlice returns the greatest common divisor of a slice.
20+
// It returns 1 when s is empty because that any number divided by 1 is still
21+
// itself.
2022
func GCDSlice(s []int) int {
2123
length := len(s)
24+
if length == 0 {
25+
return 1
26+
}
2227
if length == 1 {
2328
return s[0]
2429
}

pkg/algorithm/algorithm_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func (suit *AlgorithmSuite) TestGCDSlice() {
5353
slice []int
5454
result int
5555
}{
56+
{nil, 1},
57+
{[]int{2}, 2},
5658
{[]int{2, 2, 4}, 2},
5759
{[]int{5, 10, 25}, 5},
5860
{[]int{1, 3, 5}, 1},

0 commit comments

Comments
 (0)