Last active
October 12, 2017 15:19
-
-
Save cipto-hd/d7adfd8c726729962f52ed7bdf559da4 to your computer and use it in GitHub Desktop.
A Tour of Go Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
wordsMap := make(map[string]int) | |
words := strings.Fields(s) | |
for _, v := range words { | |
wordsMap[v] ++ | |
} | |
return wordsMap | |
} | |
func main() { | |
wc.Test(WordCount)package main | |
import ( | |
"golang.org/x/tour/wc" | |
"strings" | |
) | |
func WordCount(s string) map[string]int { | |
wordsMap := make(map[string]int) | |
words := strings.Fields(s) | |
for _, v := range words { | |
wordsMap[v]++ | |
} | |
return wordsMap | |
} | |
func main() { | |
wc.Test(WordCount) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) (z float64) { | |
const DELTA = 4e-16 | |
const INITIAL_Z = 1.0 | |
z = INITIAL_Z | |
step := func() float64 { | |
return z - (z*z-x)/(2*z) | |
} | |
for zz := step(); math.Abs(zz-z) > DELTA; { | |
z = zz | |
zz = step() | |
} | |
return | |
} | |
func main() { | |
fmt.Println(Sqrt(100)) | |
fmt.Println(math.Sqrt(100)) | |
fmt.Println(DELTA) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "golang.org/x/tour/pic" | |
func Pic(dx, dy int) [][]uint8 { | |
table := make([][]uint8, dy) | |
for y := 0; y < dy; y++ { | |
row := make([]uint8, dx) | |
for x := 0; x < dx; x++ { | |
row[x] = uint8(x^y) | |
} | |
table[y] = row | |
} | |
return table | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
// Just for comparison - fibonacci in javascript
fib = function(numMax){
for(var fibArray = [0,1], k=0; k<numMax; k++ ){
x=fibArray[k] + fibArray[k+1]
fibArray.push(x);
}
console.log(fibArray);
}
fib(10)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
newt_sqrt from https://gist.github.com/abesto/3476594