47 lines
923 B
Go
47 lines
923 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
func average(nums []float64) float64 {
|
|
if len(nums) == 0 {
|
|
return 0
|
|
}
|
|
var sum float64
|
|
for _, i := range nums {
|
|
sum += i
|
|
}
|
|
return sum / float64(len(nums))
|
|
}
|
|
|
|
func filter(nums []float64, comp func(float64, float64) bool) []float64 {
|
|
if len(nums) == 0 {
|
|
return make([]float64, 0)
|
|
}
|
|
val, idxVal := nums[0], 0
|
|
for idx, num := range nums {
|
|
if comp(num, val) {
|
|
val, idxVal = num, idx
|
|
}
|
|
}
|
|
nums = append(nums[:idxVal], nums[idxVal+1:]...)
|
|
return nums
|
|
}
|
|
|
|
func centeredAverage(nums ...float64) float64 {
|
|
nums = filter(nums, func(a float64, b float64) bool {
|
|
return a > b
|
|
})
|
|
nums = filter(nums, func(a float64, b float64) bool {
|
|
return a < b
|
|
})
|
|
return average(nums)
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(centeredAverage())
|
|
fmt.Println(centeredAverage(1, 2, 3, 4, 100))
|
|
fmt.Println(centeredAverage(1))
|
|
fmt.Println(centeredAverage(1, 1))
|
|
fmt.Println(centeredAverage(1, 1, 1))
|
|
}
|