Skip to content

Instantly share code, notes, and snippets.

View amstee's full-sized avatar
🇬🇧

Jérémy Barneron amstee

🇬🇧
View GitHub Profile
@amstee
amstee / exercise-sqrt.go
Last active January 15, 2017 00:09
An answer for the exercise sqrt
func Sqrt(x float64) (z float64) {
z = 1.0
for i := 0; i < 20; i++ {
z = z - (z * z - x) / (2 * z)
}
return z
}
@amstee
amstee / exercise-stringer.go
Last active January 15, 2017 00:09
An answer for the exercise stringer
import "fmt"
type IPAddr [4]byte
func (this IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", this[0], this[1], this[2], this[3])
}
@amstee
amstee / exercise-rot-reader.go
Last active January 15, 2017 00:09
An answer for the exercise rot reader
import (
"io"
)
type rot13Reader struct {
r io.Reader
}
func (reader *rot13Reader) Read(param []byte) (int, error) {
si, err := reader.r.Read(param)
type MyReader struct{}
func (m MyReader) Read(param []byte) (int, error) {
count := 0
for i := 0; i < len(param); i++ {
param[i] = 'A'
count += 1
}
return count, nil
}
@amstee
amstee / exercise-maps.go
Last active January 15, 2017 00:09
An answer for the exercise maps
import (
"strings"
)
func WordCount(s string) (container map[string]int) {
fields := strings.Fields(s)
container = make(map[string]int)
for i := 0; i < len(fields); i++ {
if _, ok := container[fields[i]]; ok == true {
@amstee
amstee / execise-images.go
Last active January 15, 2017 00:10
An answer for the exercise images
import "image"
import "image/color"
type Image struct{
x int
y int
}
func (im Image) Bounds() (image.Rectangle) {
return image.Rect(0, 0, im.x, im.y)
@amstee
amstee / exercise-fibonacci.go
Last active January 15, 2017 00:10
An answer for the exercise fibonacci
func fibonacci() func() int {
a, b := -1, 1
return func() int {
if a == -1 {
a = 0
return a
}
a, b = b, a + b
return a
}
@amstee
amstee / exercise-errors.go
Last active January 15, 2017 00:10
An answer for the exercise errors
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e ErrNegativeSqrt) Error() string {
return fmt.Sprintf("cannot Sqrt negative number: %d", int(e))
}
@amstee
amstee / execise-slices.go
Last active January 15, 2017 00:08
An answer for the exercise slices
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i := 0; i < dy; i++ {
pic[i] = make([]uint8, dx)
for u := 0; u < dx; u++ {
pic[i][u] = uint8((u + i) / 2)
}
}
return pic