A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
19
Task/Map-range/Go/map-range-1.go
Normal file
19
Task/Map-range/Go/map-range-1.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func mapRange(x, y rangeBounds, n float64) float64 {
|
||||
return y.b1 + (n - x.b1) * (y.b2 - y.b1) / (x.b2 - x.b1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r1 := rangeBounds{0, 10}
|
||||
r2 := rangeBounds{-1, 0}
|
||||
for n := float64(0); n <= 10; n += 2 {
|
||||
fmt.Println(n, "maps to", mapRange(r1, r2, n))
|
||||
}
|
||||
}
|
||||
37
Task/Map-range/Go/map-range-2.go
Normal file
37
Task/Map-range/Go/map-range-2.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func newRangeMap(xr, yr rangeBounds) func(float64) (float64, bool) {
|
||||
// normalize direction of ranges so that out-of-range test works
|
||||
if xr.b1 > xr.b2 {
|
||||
xr.b1, xr.b2 = xr.b2, xr.b1
|
||||
yr.b1, yr.b2 = yr.b2, yr.b1
|
||||
}
|
||||
// compute slope, intercept
|
||||
m := (yr.b2 - yr.b1) / (xr.b2 - xr.b1)
|
||||
b := yr.b1 - m*xr.b1
|
||||
// return function literal
|
||||
return func(x float64) (y float64, ok bool) {
|
||||
if x < xr.b1 || x > xr.b2 {
|
||||
return 0, false // out of range
|
||||
}
|
||||
return m*x + b, true
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rm := newRangeMap(rangeBounds{0, 10}, rangeBounds{-1, 0})
|
||||
for s := float64(-2); s <= 12; s += 2 {
|
||||
t, ok := rm(s)
|
||||
if ok {
|
||||
fmt.Printf("s: %5.2f t: %5.2f\n", s, t)
|
||||
} else {
|
||||
fmt.Printf("s: %5.2f out of range\n", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue