Skip to content

Instantly share code, notes, and snippets.

@cipto-hd
Last active October 12, 2017 15:19
Show Gist options
  • Save cipto-hd/d7adfd8c726729962f52ed7bdf559da4 to your computer and use it in GitHub Desktop.
Save cipto-hd/d7adfd8c726729962f52ed7bdf559da4 to your computer and use it in GitHub Desktop.
A Tour of Go Exercises
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)
}
}
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)
}
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)
}
@cipto-hd
Copy link
Author

@cipto-hd
Copy link
Author

cipto-hd commented Oct 12, 2017

// 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