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

Appearance settings
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 0ba788d

Browse filesBrowse files
committed
fix: GCDSlice returns 1 when slice is empty
Signed-off-by: lowzj <zj3142063@gmail.com>
1 parent 734cde5 commit 0ba788d
Copy full SHA for 0ba788d

File tree

Expand file treeCollapse file tree

3 files changed

+8
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+8
-1
lines changed
Open diff view settings
Collapse file

‎dfget/config/supernode_value.go‎

Copy file name to clipboardExpand all lines: dfget/config/supernode_value.go
+1-1Lines 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

Collapse file

‎pkg/algorithm/algorithm.go‎

Copy file name to clipboardExpand all lines: pkg/algorithm/algorithm.go
+5Lines 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
}
Collapse file

‎pkg/algorithm/algorithm_test.go‎

Copy file name to clipboardExpand all lines: pkg/algorithm/algorithm_test.go
+2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.