-
-
Save wesmota/73f26df9fd86f08d63c59cd9025579fa to your computer and use it in GitHub Desktop.
Revisions
-
tomgro revised this gist
May 10, 2016 . 1 changed file with 22 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ func Solution(A []int) int { // write your code in Go 1.4 var pom = make([]int, len(A)+1) pom[0] = 0; for _, v := range A { if v > 0 && v <= len(A) { pom[v-1] = 1 } } //fmt.Println(pom) for i, v := range pom { if v == 0 { return i+1 } } return 1 } -
tomgro revised this gist
May 10, 2016 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ func Solution(A []int) int { // write your code in Go 1.4 var pom = make([]int, len(A)+1) var sum = 0 for _, v := range A { if v < len(pom) && pom[v] == 0 { pom[v] = 1 sum += 1 } // fmt.Println(v, sum, X) } if(sum == len(A)) { return 1 } return 0 } -
tomgro renamed this gist
May 10, 2016 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,19 @@ func Solution(X int, A []int) int { // write your code in Go 1.4 var pom = make([]int, X+1) pom[0] = 1 for i, v := range A { if pom[v] == 0 { pom[v] = 1 X-- } // fmt.Println(v, sum, X) if(X == 0) { return i } } -
tomgro revised this gist
May 10, 2016 . 1 changed file with 22 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ func Solution(X int, A []int) int { // write your code in Go 1.4 var pom = make([]int, X) pom[0] = 1 var sum = 1 for i, v := range A { if v < X { if pom[v] == 0 { pom[v] = 1 sum += 1 } } // fmt.Println(v, sum, X) if(v == X && sum == X) { return i } } return -1 } -
tomgro revised this gist
May 10, 2016 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ func Solution(A []int) int { // write your code in Go 1.4 var B []int = make([]int, len(A)+1) for _, v := range A { B[v-1] = 1 } // fmt.Println(B) for i, v := range B { if(v == 0) { return i + 1 } } return 0 } -
tomgro revised this gist
May 10, 2016 . 1 changed file with 31 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ func Solution(A []int) int { // write your code in Go 1.4 var sum int = 0; for _, v := range A { sum += v } // fmt.Println(sum) res := make([]int, len(A)-1) var prevSum int = 0 var diff float64 = 0 for i, v := range A { if(i < len(A)-1) { prevSum = v + prevSum diff = math.Abs(float64(prevSum - (sum - prevSum))) res[i] = int(diff) } } var min float64 = math.Inf(1); for _, v := range res { if float64(v) < min { min = float64(v) } } return int(min) } -
tomgro revised this gist
May 10, 2016 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ func Solution(X int, Y int, D int) int { // write your code in Go 1.4 var j float64 = float64(Y - X) / float64(D) var r float64 = math.Ceil(j) // fmt.Println(r) return int(r) } -
tomgro revised this gist
May 10, 2016 . 11 changed files with 314 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ package solution // you can also use imports, for example: //import "fmt" import "sort" // you can write to stdout for debugging purposes, e.g. // fmt.Println("this is a debug message") func Solution(A []int) int { // write your code in Go 1.4 //var B []int = make([]int, 100) sort.Ints(A) //fmt.Println(A) lth := len(A) i := 0 for i < lth { if i >= lth-1 { return A[lth-1] } if A[i] != A[i+1] { //fmt.Println(A[i]) return A[i] } i += 2 } return 0 } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ package solution // you can also use imports, for example: //import "fmt" // import "os" // you can write to stdout for debugging purposes, e.g. // fmt.Println("this is a debug message") func Solution(A []int, K int) []int { // write your code in Go 1.4 lth := len(A) // idx := lth + K var B []int = make([]int, lth) for i, v := range A { B[i] = v } for i, v := range B { x := (i + K) % lth //fmt.Println(x, v) A[x] = v } // fmt.Println(A) return A } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ func Solution(N int) int { // write your code in Go 1.4 gap := 0 max_gap := 0 //start_gap := false for ;N > 0 && N % 2 == 0; { N = N / 2 } for N > 0 { rest := N % 2 if rest == 0 { gap += 1 } else { if gap != 0 { if gap > max_gap { max_gap = gap } gap = 0 } } N = N / 2 } return max_gap } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ package main import ( "golang.org/x/tour/wc" "strings" //"fmt" ) func WordCount(s string) map[string]int { res := make(map[string]int) for _, v := range strings.Fields(s) { _, ok := res[v]; if ok == true {res[v] = res[v] + 1 } else {res[v] = 1} } return res } func main() { wc.Test(WordCount) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ package main import "fmt" // fibonacci is a function that returns // a function that returns an int. func fibonacci() func() int { a, b := 1, 0 return func() int { a, b = b, a + b return a } } func main() { f := fibonacci() for i := 0; i < 10; i++ { fmt.Println(f()) } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ package main import "fmt" type IPAddr [4]byte // TODO: Add a "String() string" method to IPAddr. func (ip IPAddr) String() string { return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3]) } func main() { hosts := map[string]IPAddr{ "loopback": {127, 0, 0, 1}, "googleDNS": {8, 8, 8, 8}, } for name, ip := range hosts { fmt.Printf("%v: %v\n", name, ip) } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ package main import ( "fmt" "math" ) var p = 0.0 type ErrNegativeSqrt float64 func Sqrt(x float64) (float64, error) { if x < 0 { return x, ErrNegativeSqrt(x) } z := 1.0 for math.Abs(p - z) > 0.001 { z = newton(z, x) p = newton(z, x) } return z, nil } func (e ErrNegativeSqrt) Error() string { return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) } func newton(z, x float64) float64 { return z - ( ((z*z) - x) / (2*z) ) } func main() { fmt.Println(Sqrt(2)) fmt.Println(Sqrt(-2)) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ package main import ("golang.org/x/tour/reader" ) type MyReader struct{} // TODO: Add a Read([]byte) (int, error) method to MyReader. func (m MyReader) Read(b []byte) (i int, e error) { for x := range b { b[x] = 'A' } return len(b), nil } func main() { reader.Validate(MyReader{}) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ package main import ( "io" "os" "strings" ) type rot13Reader struct { r io.Reader } func (r *rot13Reader) Read(p []byte) (n int, err error) { n, err = r.r.Read(p) for i, b := range p { switch { case 'A' <= b && b <= 'M', 'a' <= b && b <= 'm': p[i] += 13 case 'N' <= b && b <= 'Z', 'n' <= b && b <= 'z': p[i] -= 13 } } return } func main() { s := strings.NewReader("Lbh penpxrq gur pbqr!") r := rot13Reader{s} io.Copy(os.Stdout, &r) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ package main import ("golang.org/x/tour/pic" "image" "image/color" ) type Image struct{ Width, Height int color uint8 } func (img Image) Bounds() image.Rectangle { return image.Rect(0, 0, img.Height, img.Width) } func (img Image) ColorModel() color.Model { return color.RGBAModel } func (img Image) At(x, y int) color.Color { return color.RGBA{img.color+uint8(x), img.color+uint8(y), 255, 255} } func main() { m := Image{100, 100, 148} pic.ShowImage(m) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ package main import ("golang.org/x/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() { ch := make(chan int) go Walk(tree.New(1), ch) for v := range ch { fmt.Print(v) } fmt.Println() fmt.Println(Same(tree.New(1), tree.New(1))) fmt.Println(Same(tree.New(1), tree.New(2))) } -
tomgro created this gist
May 10, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ package main import ( "fmt" "math" ) var p = 0.0 func Sqrt(x float64) float64 { z := 1.0 for math.Abs(p - z) > 0.000000001 { z = newton(z, x) p = newton(z, x) } return z } func newton(z, x float64) float64 { return z - ( ((z*z) - x) / (2*z) ) } func main() { fmt.Println(Sqrt(2)) }