Skip to content

Instantly share code, notes, and snippets.

View royling's full-sized avatar
🔬
Keep practicing and learning everyday!

Roy Ling royling

🔬
Keep practicing and learning everyday!
View GitHub Profile
@royling
royling / go_tour_ex1_sqrt.go
Last active December 16, 2015 00:59
go-tour exercise1: implement the square root function using Newton's method
package main
import (
"fmt"
"math"
)
const Delta = float64(1e-15)
func Sqrt(x float64) float64 {
@royling
royling / go_tour_ex2_slice.go
Created April 11, 2013 06:21
Ex2: Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice of image is up to you. Interesting functions include x^y, (x+y)/2, and x*y.
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
p := make([][]uint8, dy)
for y := range p {
p[y] = make([]uint8, dx)
for x := range p[y] {
p[y][x] = uint8(x^y)
@royling
royling / go_tour_ex3_map.go
Created April 11, 2013 09:57
Implement WordCount. It should return a map of the counts of each “word” in the string s.
package main
import (
"code.google.com/p/go-tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
m := make(map[string]int)
words := strings.Fields(s)
@royling
royling / go_tour_ex4_closure_fib.go
Last active December 16, 2015 03:29
Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
f1, f2 := 0, 1
return func() int {
f1, f2 = f2, f1 + f2
@royling
royling / go_tour_ex5_cbrt.go
Last active December 16, 2015 03:39
Implement cube root with Newton's method.
package main
import (
"fmt"
"math/cmplx"
)
const delta = 1e-15
func Cbrt(x complex128) complex128 {
@royling
royling / go_tour_ex_web_server.go
Last active December 16, 2015 04:29
Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server.
package main
import (
"fmt"
"net/http"
)
type MyString string
// implements http.Handler
func (s MyString) ServeHTTP(
@royling
royling / go_tour_ex_image.go
Created April 13, 2013 13:19
Define your own Image type, implement the necessary methods, and call pic.ShowImage. 1) Bounds should return a image.Rectangle, like image.Rect(0, 0, w, h). 2) ColorModel should return color.RGBAModel. 3) At should return a color; the value v in the last picture generator corresponds to color.RGBA{v, v, 255, 255} in this one.
package main
import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)
type Image struct{
dx, dy int
@royling
royling / go_tour_ex_rot13.go
Created April 14, 2013 07:43
Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@royling
royling / go_tour_ex_eq_btree.go
Last active December 16, 2015 09:08
A function to check whether two binary trees store the same sequence is quite complex in most languages. We'll use Go's concurrency and channels to write a simple solution.
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
@royling
royling / go_tour_ex_web_crawler.go
Last active December 16, 2015 10:09
Modify the Crawl function to fetch URLs in parallel without fetching the same URL twice.
package main
import (
"fmt"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
Fetch(url string) (body string, urls []string, err error)