Last active
May 14, 2018 20:41
-
-
Save scottcrawford03/75d3d6b29ba33de9a9497ba55a5234d0 to your computer and use it in GitHub Desktop.
go by example solutions
This file contains 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 characters
// This one was tough because I didn't know that you had to tell the function that it was going to return a float and an error. | |
// Once I figured that piece out then it started to make sense. | |
package main | |
import ( | |
"fmt" | |
) | |
type ErrNegativeSqrt float64 | |
func (e ErrNegativeSqrt) Error() string { | |
return fmt.Sprintf("Sqrt: negative number %g", e) | |
} | |
func Sqrt(x float64) (float64, error) { | |
if x < 0 { | |
return 0, ErrNegativeSqrt(x) | |
} | |
if x == 0 { return 0, nil } | |
z := 1.0 | |
for i := 0; i < int(x); i++ { | |
fmt.Println(z) | |
former := z | |
z -= (z*z - x) / (2*z) | |
if former == z { i = int(x) } | |
} | |
return z, nil | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(Sqrt(-2)) | |
} |
This file contains 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 characters
package main | |
import "fmt" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
i := 0 | |
first := 0 | |
second := 1 | |
return func() int { | |
i++ | |
if i == 1 { return first } | |
if i == 2 { return second } | |
new := first + second | |
first = second | |
second = new | |
return second | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 15; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains 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 characters
// I'm still not sure what this is supposed to be doing. | |
package main | |
import ( | |
"golang.org/x/tour/reader" | |
) | |
type MyReader struct{} | |
// TODO: Add a Read([]byte) (int, error) method to MyReader. | |
func (r MyReader) Read(b []byte) (int, error) { | |
for i := range b { | |
b[i] = 'A' | |
} | |
return len(b), nil | |
} | |
func main() { | |
reader.Validate(MyReader{}) | |
} |
This file contains 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 characters
package main | |
import ( | |
"golang.org/x/tour/pic" | |
) | |
func Pic(dx, dy int) [][]uint8 { | |
pic := make([][]uint8, dy) | |
for i := range pic { | |
pic[i] = make([]uint8, dx) | |
for j := range pic[i] { | |
pic[i][j] = uint8((i^j) / 2) | |
} | |
} | |
return pic | |
} | |
func main() { | |
pic.Show(Pic) | |
} |
This file contains 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 characters
package main | |
import ( | |
"fmt" | |
) | |
type IPAddr [4]byte | |
func (ipAddr IPAddr) String() string { | |
return fmt.Sprintf("%v.%v.%v.%v", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[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 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 characters
package main | |
import ( | |
"strings" | |
"golang.org/x/tour/wc" | |
) | |
func WordCount(s string) map[string]int { | |
words := strings.Fields(s) | |
dict := map[string]int{} | |
for _, v := range words { | |
dict[v] += 1 | |
} | |
return dict | |
} | |
func main() { | |
wc.Test(WordCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment