From b0f2d371e3506120334b20a458a15f2d6bcdec32 Mon Sep 17 00:00:00 2001 From: oabrivard Date: Sun, 20 Aug 2023 00:12:44 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + .../exercise-equivalent-binary-tree.go | 63 +++++++++++++++++++ exercise-equivalent-binary-tree/go.mod | 5 ++ exercise-equivalent-binary-tree/go.sum | 2 + helloworld/go.mod | 3 + helloworld/helloworld.go | 8 +++ 6 files changed, 83 insertions(+) create mode 100644 exercise-equivalent-binary-tree/exercise-equivalent-binary-tree.go create mode 100644 exercise-equivalent-binary-tree/go.mod create mode 100644 exercise-equivalent-binary-tree/go.sum create mode 100644 helloworld/go.mod create mode 100644 helloworld/helloworld.go diff --git a/.gitignore b/.gitignore index adf8f72..e620e5a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.DS_Store + # ---> Go # If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore diff --git a/exercise-equivalent-binary-tree/exercise-equivalent-binary-tree.go b/exercise-equivalent-binary-tree/exercise-equivalent-binary-tree.go new file mode 100644 index 0000000..871b690 --- /dev/null +++ b/exercise-equivalent-binary-tree/exercise-equivalent-binary-tree.go @@ -0,0 +1,63 @@ +package main + +import "fmt" +import "golang.org/x/tour/tree" + +func recWalk(t *tree.Tree, ch chan int) { + if t.Left != nil { + recWalk(t.Left,ch) + } + ch <- t.Value + if t.Right != nil { + recWalk(t.Right,ch) + } +} + +// Walk walks the tree t sending all values +// from the tree to the channel ch. +func Walk(t *tree.Tree, ch chan int) { + recWalk(t,ch) + close(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 { + v1, ok1 := <-ch1 + v2, ok2 := <-ch2 + + if !ok1 && !ok2 { + break + } + + if ok1 != ok2 { + return false + } + + if v1 != v2 { + return false + } + } + + return true +} + +func main() { + ch := make(chan int) + + go Walk(tree.New(1), ch) + + for i := range ch { + fmt.Println(i) + } + + fmt.Println(Same(tree.New(1), tree.New(1))) + fmt.Println(Same(tree.New(1), tree.New(2))) +} diff --git a/exercise-equivalent-binary-tree/go.mod b/exercise-equivalent-binary-tree/go.mod new file mode 100644 index 0000000..5415b90 --- /dev/null +++ b/exercise-equivalent-binary-tree/go.mod @@ -0,0 +1,5 @@ +module gitea.paas.celticinfo.fr/oabrivard/tour-of-go/exercise-equivalent-binary-tree + +go 1.21.0 + +require golang.org/x/tour v0.1.0 diff --git a/exercise-equivalent-binary-tree/go.sum b/exercise-equivalent-binary-tree/go.sum new file mode 100644 index 0000000..cde48ea --- /dev/null +++ b/exercise-equivalent-binary-tree/go.sum @@ -0,0 +1,2 @@ +golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE= +golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc= diff --git a/helloworld/go.mod b/helloworld/go.mod new file mode 100644 index 0000000..5d563f3 --- /dev/null +++ b/helloworld/go.mod @@ -0,0 +1,3 @@ +module gitea.paas.celticinfo.fr/oabrivard/tour-of-go/helloworld + +go 1.20 diff --git a/helloworld/helloworld.go b/helloworld/helloworld.go new file mode 100644 index 0000000..d5011a1 --- /dev/null +++ b/helloworld/helloworld.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, world.") +} +