moved exercises beneath common package
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const min_threshold = 1e-200
|
||||
|
||||
func Sqrt(x float64, threshold float64) (z float64, iterations int) {
|
||||
z = x
|
||||
old_z := z
|
||||
for {
|
||||
z = z - (z*z-x)/(2*x)
|
||||
if math.Abs(z-old_z) < threshold {
|
||||
break
|
||||
}
|
||||
old_z = z
|
||||
iterations++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
threshold := 1.0
|
||||
right_answer := math.Sqrt(2)
|
||||
for threshold > min_threshold {
|
||||
answer, iterations := Sqrt(2, threshold)
|
||||
fmt.Printf("%0.4g %0.4g %0.20f %0.20f %0.2d\n",
|
||||
threshold, right_answer-answer, right_answer, answer,
|
||||
iterations)
|
||||
threshold = threshold / 10.0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"code.google.com/p/go-tour/wc"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func WordCount(s string) map[string]int {
|
||||
wc := make(map[string]int)
|
||||
for _, v := range strings.Fields(s) {
|
||||
wc[v] += 1
|
||||
}
|
||||
return wc
|
||||
}
|
||||
|
||||
func main() {
|
||||
wc.Test(WordCount)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "code.google.com/p/go-tour/pic"
|
||||
|
||||
func Pic(dx, dy int) [][]uint8 {
|
||||
result := make([][]uint8, dy)
|
||||
for j := 0; j < dy; j++ {
|
||||
result[j] = make([]uint8, dx)
|
||||
for i := 0; i < dx; i++ {
|
||||
result[j][i] = uint8(i) ^ uint8(j)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
pic.Show(Pic)
|
||||
}
|
||||
Reference in New Issue
Block a user