Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1302 from lowzj/fix-GCDSlice
Browse files Browse the repository at this point in the history
fix: GCDSlice returns 1 when slice is empty
  • Loading branch information
starnop authored Apr 28, 2020
2 parents a8ade02 + 10cfda1 commit ca96df3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dfget/config/supernode_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func ParseNodesSlice(value []string) ([]*NodeWeight, error) {
for _, v := range nodeWeightSlice {
result = append(result, &NodeWeight{
Node: v.Node,
Weight: (v.Weight / gcdNumber),
Weight: v.Weight / gcdNumber,
})
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/algorithm/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package algorithm

// GCDSlice returns the greatest common divisor of a slice.
// It returns 1 when s is empty because that any number divided by 1 is still
// itself.
func GCDSlice(s []int) int {
length := len(s)
if length == 0 {
return 1
}
if length == 1 {
return s[0]
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/algorithm/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (suit *AlgorithmSuite) TestGCDSlice() {
slice []int
result int
}{
{nil, 1},
{[]int{2}, 2},
{[]int{2, 2, 4}, 2},
{[]int{5, 10, 25}, 5},
{[]int{1, 3, 5}, 1},
Expand Down

0 comments on commit ca96df3

Please sign in to comment.