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

Commit

Permalink
fix: GCDSlice returns 1 when slice is empty
Browse files Browse the repository at this point in the history
Signed-off-by: lowzj <[email protected]>
  • Loading branch information
lowzj committed Apr 26, 2020
1 parent 06d2368 commit 41a8604
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
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 41a8604

Please sign in to comment.