Skip to content

Instantly share code, notes, and snippets.

@ursuad
Forked from zyxar/exercise.tour.go
Created May 24, 2016 12:45
Show Gist options
  • Save ursuad/2182e0c3e0c82c47aed8beee86a6d9fd to your computer and use it in GitHub Desktop.
Save ursuad/2182e0c3e0c82c47aed8beee86a6d9fd to your computer and use it in GitHub Desktop.

Revisions

  1. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 105 additions and 6 deletions.
    111 changes: 105 additions & 6 deletions exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -80,13 +80,11 @@ import "fmt"
    // fibonacci is a function that returns
    // a function that returns an int.
    func fibonacci() func() int {
    sum := 1
    prev := 0
    x := 0
    y := 1
    return func() int {
    old := sum
    sum += prev
    prev = old
    return sum
    x,y = y,x+y
    return x
    }
    }

    @@ -270,3 +268,104 @@ func main() {
    }

    /******************************************************************************************************/

    /* Exercise: Web Crawler #69 */
    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)
    }

    var store map[string]bool

    func Krawl(url string, fetcher Fetcher, Urls chan []string) {
    body, urls, err := fetcher.Fetch(url)
    if err != nil {
    fmt.Println(err)
    } else {
    fmt.Printf("found: %s %q\n", url, body)
    }
    Urls <- urls
    }

    func Crawl(url string, depth int, fetcher Fetcher) {
    Urls := make(chan []string)
    go Krawl(url, fetcher, Urls)
    band := 1
    store[url] = true // init for level 0 done
    for i := 0; i < depth; i++ {
    for band > 0 {
    band--
    next := <- Urls
    for _, url := range next {
    if _, done := store[url] ; !done {
    store[url] = true
    band++
    go Krawl(url, fetcher, Urls)
    }
    }
    }
    }
    return
    }

    func main() {
    store = make(map[string]bool)
    Crawl("http://golang.org/", 4, fetcher)
    }


    // fakeFetcher is Fetcher that returns canned results.
    type fakeFetcher map[string]*fakeResult

    type fakeResult struct {
    body string
    urls []string
    }

    func (f *fakeFetcher) Fetch(url string) (string, []string, error) {
    if res, ok := (*f)[url]; ok {
    return res.body, res.urls, nil
    }
    return "", nil, fmt.Errorf("not found: %s", url)
    }

    // fetcher is a populated fakeFetcher.
    var fetcher = &fakeFetcher{
    "http://golang.org/": &fakeResult{
    "The Go Programming Language",
    []string{
    "http://golang.org/pkg/",
    "http://golang.org/cmd/",
    },
    },
    "http://golang.org/pkg/": &fakeResult{
    "Packages",
    []string{
    "http://golang.org/",
    "http://golang.org/cmd/",
    "http://golang.org/pkg/fmt/",
    "http://golang.org/pkg/os/",
    },
    },
    "http://golang.org/pkg/fmt/": &fakeResult{
    "Package fmt",
    []string{
    "http://golang.org/",
    "http://golang.org/pkg/",
    },
    },
    "http://golang.org/pkg/os/": &fakeResult{
    "Package os",
    []string{
    "http://golang.org/",
    "http://golang.org/pkg/",
    },
    },
    }
  2. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 53 additions and 1 deletion.
    54 changes: 53 additions & 1 deletion exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -217,4 +217,56 @@ func main() {
    io.Copy(os.Stdout, &r)
    }

    /******************************************************************************************************/
    /******************************************************************************************************/

    /* Exercise: Equivalent Binary Trees #67 */
    package main

    import (
    "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) {
    _walk(t, ch)
    close(ch)

    }

    func _walk(t *tree.Tree, ch chan int) {
    if t != nil {
    _walk(t.Left, ch)
    ch <- t.Value
    _walk(t.Right, ch)
    }
    }

    // Same determines whether the trees
    // t1 and t2 contain the same values.
    func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)
    for i := range ch1 {
    if i != <- ch2 {
    return false
    }
    }
    return true
    }

    func main() {
    //tree.New(2)
    ch := make(chan int)
    go Walk(tree.New(1), ch)
    for v := range ch {
    fmt.Print(v)
    }
    fmt.Println(Same(tree.New(1), tree.New(1)))
    fmt.Println(Same(tree.New(1), tree.New(2)))
    }

    /******************************************************************************************************/
  3. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -185,7 +185,7 @@ func main() {
    }

    /******************************************************************************************************/
    /* Exercise: Rot13 Reader #59: 'You-cracked-the-code.' */
    /* Exercise: Rot13 Reader #59: 'You cracked the code!' */
    package main

    import (
    @@ -201,9 +201,9 @@ type rot13Reader struct {
    func (rot *rot13Reader) Read(p []byte) (n int, err error) {
    n,err = rot.r.Read(p)
    for i := 0; i < len(p); i++ {
    if p[i] < 'N' || (p[i] >='a' && p[i] < 'n') {
    if (p[i] >= 'A' && p[i] < 'N') || (p[i] >='a' && p[i] < 'n') {
    p[i] += 13
    } else {
    } else if (p[i] > 'M' && p[i] <= 'Z') || (p[i] > 'm' && p[i] <= 'z'){
    p[i] -= 13
    }
    }
    @@ -217,4 +217,4 @@ func main() {
    io.Copy(os.Stdout, &r)
    }

    /******************************************************************************************************/
    /******************************************************************************************************/
  4. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 34 additions and 1 deletion.
    35 changes: 34 additions & 1 deletion exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -184,4 +184,37 @@ func main() {
    pic.ShowImage(&m)
    }

    /******************************************************************************************************/
    /******************************************************************************************************/
    /* Exercise: Rot13 Reader #59: 'You-cracked-the-code.' */
    package main

    import (
    "io"
    "os"
    "strings"
    )

    type rot13Reader struct {
    r io.Reader
    }

    func (rot *rot13Reader) Read(p []byte) (n int, err error) {
    n,err = rot.r.Read(p)
    for i := 0; i < len(p); i++ {
    if p[i] < 'N' || (p[i] >='a' && p[i] < 'n') {
    p[i] += 13
    } else {
    p[i] -= 13
    }
    }
    return
    }

    func main() {
    s := strings.NewReader(
    "Lbh penpxrq gur pbqr!")
    r := rot13Reader{s}
    io.Copy(os.Stdout, &r)
    }

    /******************************************************************************************************/
  5. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -176,12 +176,12 @@ func (r *Image) ColorModel() color.Model {
    }

    func (r *Image) At(x, y int) color.Color {
    return color.RGBA{r.colr, r.colr, 255, 255}
    return color.RGBA{r.colr+uint8(x), r.colr+uint8(y), 255, 255}
    }

    func main() {
    m := Image{100, 100, 128}
    pic.ShowImage(&m)
    }

    /******************************************************************************************************/
    /******************************************************************************************************/
  6. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -152,3 +152,36 @@ func main() {
    }

    /******************************************************************************************************/

    /* Exercise: Images #58 */
    package main

    import (
    "image"
    "tour/pic"
    "image/color"
    )

    type Image struct{
    Width, Height int
    colr uint8
    }

    func (r *Image) Bounds() image.Rectangle {
    return image.Rect(0, 0, r.Width, r.Height)
    }

    func (r *Image) ColorModel() color.Model {
    return color.RGBAModel
    }

    func (r *Image) At(x, y int) color.Color {
    return color.RGBA{r.colr, r.colr, 255, 255}
    }

    func main() {
    m := Image{100, 100, 128}
    pic.ShowImage(&m)
    }

    /******************************************************************************************************/
  7. @zyxar zyxar revised this gist Apr 6, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /* Exercise: Loops and Functions #43 ×/
    /* Exercise: Loops and Functions #43 */
    package main

    import (
  8. @zyxar zyxar created this gist Apr 6, 2012.
    154 changes: 154 additions & 0 deletions exercise.tour.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,154 @@
    /* Exercise: Loops and Functions #43 ×/
    package main
    import (
    "fmt"
    "math"
    )
    func Sqrt(x float64) float64 {
    z := float64(2.)
    s := float64(0)
    for {
    z = z - (z*z - x)/(2*z)
    if math.Abs(s-z) < 1e-15 {
    break
    }
    s = z
    }
    return s
    }
    func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(math.Sqrt(2))
    }
    /******************************************************************************************************/

    /* Exercise: Maps #44 */
    package main

    import (
    "tour/wc"
    "strings"
    )

    func WordCount(s string) map[string]int {
    ss := strings.Fields(s)
    num := len(ss)
    ret := make(map[string]int)
    for i := 0; i < num; i++ {
    (ret[ss[i]])++
    }
    return ret
    }

    func main() {
    wc.Test(WordCount)
    }

    /******************************************************************************************************/

    /* Exercise: Slices #45 */
    package main

    import "tour/pic"

    func Pic(dx, dy int) [][]uint8 {
    ret := make([][]uint8, dy)
    for i := 0; i < dy; i++ {
    ret[i] = make([]uint8, dx)
    for j := 0; j < dx; j++ {
    ret[i][j] = uint8(i^j+(i+j)/2)
    }
    }
    return ret
    }

    func main() {
    pic.Show(Pic)
    }

    /******************************************************************************************************/

    /* Exercise: Fibonacci closure #46 */
    package main

    import "fmt"

    // fibonacci is a function that returns
    // a function that returns an int.
    func fibonacci() func() int {
    sum := 1
    prev := 0
    return func() int {
    old := sum
    sum += prev
    prev = old
    return sum
    }
    }

    func main() {
    f := fibonacci()
    for i := 0; i < 10; i++ {
    fmt.Println(f())
    }
    }

    /******************************************************************************************************/

    /* Advanced Exercise: Complex cube roots #47 */
    package main

    import (
    "fmt"
    "math/cmplx"
    )

    func Cbrt(x complex128) complex128 {
    z := complex128(2)
    s := complex128(0)
    for {
    z = z - (cmplx.Pow(z,3) - x)/(3 * (z * z))
    if cmplx.Abs(s-z) < 1e-17 {
    break
    }
    s = z
    }
    return z
    }

    func main() {
    fmt.Println(Cbrt(2))
    }

    /******************************************************************************************************/

    /* Exercise: Errors #57 */
    package main

    import (
    "fmt"
    )

    type ErrNegativeSqrt float64

    func (e ErrNegativeSqrt) Error() string {
    return fmt.Sprintf("cannot Sqrt negativ number: %g", float64(e))
    }

    func Sqrt(f float64) (float64, error) {
    if f < 0 {
    return 0, ErrNegativeSqrt(f)
    }
    return 0, nil
    }

    func main() {
    fmt.Println(Sqrt(2))
    fmt.Println(Sqrt(-2))
    }

    /******************************************************************************************************/