4 ms·
Luckily, this is a solved problem for go. func helper(a float64, c chan float64){ time.Sleep(time.Duration(a) * time.Second) c <- a } func
by donquichotte 6y ago
Luckily, this is a solved problem for go.
func helper(a float64, c chan float64){
time.Sleep(time.Duration(a) * time.Second)
c <- a
}
func clamp(a float64, min float64, max float64) float64 {
c := make(chan float64, 3)
go helper(a, c)
go helper(min, c)
go helper(max, c)
_, out, _ := <-c, <-c, <-c
return out
}
- ccmcarey 6y agoThat will give you the median value. What OP wants is the value `a` clamped within `min` and `max`.
- donquichotte 6y agoCan you give an example where median([a, min, max]) is not equal to clamp(a, min, max), given max >= min?
- naikrovek 6y agoIf a < min or a > max, if I'm reading it right.
- mc10 6y agoYou can prove that this algorithm works for both of those cases. Assuming min <= max: If a < min, then the sorted array is [a, min, max]. The median is min, which is a clamped to min. If a > max, then the sorted array is [min, max, a]. The median is max, which is a clamped to max.
- apta 6y agoaka sleep sort
- tl 6y agoPassing math.NaN() crashes in a playground.
- resist_futility 6y agoIsn't this a race condition? https://play.golang.org/p/3Im8FuhyR_S https://play.golang.org/p/3Im8FuhyR_S