Fixed crashing when handling empty arrays

This commit is contained in:
raul 2024-01-31 10:57:39 +00:00
parent 10e172b29f
commit 41c2fcc5af
1 changed files with 6 additions and 0 deletions

View File

@ -5,6 +5,9 @@ package minmax
// TODO: Handle empty arrays without panicking
func Min(arr []float64) (min float64) {
if len(arr) == 0 {
return 0
}
min = arr[0]
for _, v := range arr {
if v < min {
@ -15,6 +18,9 @@ func Min(arr []float64) (min float64) {
}
func Max(arr []float64) (max float64) {
if len(arr) == 0 {
return 0
}
max = arr[0]
for _, v := range arr {
if v > max {