exercises

package
v0.0.0-...-0878979 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Sqrt should return a non-nil error value when given a negative number, as it doesn't support complex numbers.

Create a new type type ErrNegativeSqrt float64 and make it an error by giving it a func (e ErrNegativeSqrt) Error() string method such that ErrNegativeSqrt(-2).Error() returns "cannot Sqrt negative number: -2".

Note: A call to fmt.Sprint(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first: fmt.Sprint(float64(e)). Why?

Change your Sqrt function to return an ErrNegativeSqrt value when given a negative number.

NOTE: Exercise: Fibonacci closure Let's have some fun with functions. Implement a fibonacci function that returns a function (a closure) that returns successive fibonacci numbers (0, 1, 1, 2, 3, 5, ...).package main

TODO: Exercise: Images (https://go.dev/tour/methods/25) Instructions: > Remember the picture generator you wrote earlier? Let's write another one,

but this time it will return an implementation of image.Image instead of a slice of data.

> Define your own Image type, implement the necessary methods, and call pic.ShowImage.

See: https://go.dev/pkg/image/#Image for "necessary methods"

> `Bounds` should return a `image.Rectangle`, like `image.Rect(0, 0, w, h)`. > `ColorModel` should return `color.RGBAModel`. > `At` should return a color; the value `v` in the last picture generator corresponds to

`color.RGBA{v, v, 255, 255}` in this one.

Exercise: Readers

Implement an `io.Reader` type that emits an ASCII character 'A'

Exercise: rot13Reader

A common pattern is an io.Reader that wraps another io.Reader, modifying the stream in some way. For example, the gzip.NewReader function takes an io.Reader (a stream of compressed data) and returns a *gzip.Reader that also implements io.Reader (a stream of the decompressed data).

TODO: Implement a rot13Reader that implements io.Reader and reads from an io.Reader, modifying the stream by applying the rot13 substitution cipher to all alphabetical characters. The rot13Reader type is provided for you. Make it an io.Reader by implementing its Read method. rot13 cipher: https://en.wikipedia.org/wiki/ROT13

NOTE: Exercise Instructions Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice of image is up to you. Interesting functions include (x+y)/2, x*y, and x^y. (You need to use a loop to allocate each []uint8 inside the [][]uint8.) (Use uint8(intValue) to convert between types.)

Exercise: Stringers

Make the type IPAddr implement fmt.Stringer to print the address as a dotted quad

For instance, IPAddr{1, 2, 3, 4} should print as "1.2.3.4"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorImage

func ColorImage()

func Fibonacci

func Fibonacci()

func IPAddrStringerTest

func IPAddrStringerTest()

func MathPic

func MathPic()

func Pic

func Pic(dx, dy int) [][]uint8

func ReaderValidate

func ReaderValidate()

func Rot13ReaderTest

func Rot13ReaderTest()

func Same

func Same(t1, t2 *tree.Tree) bool

Same determines whether the trees t1 and t2 contain the same values.

func Sqrt

func Sqrt(x float64) (float64, error)

func SqrtErr

func SqrtErr()

func TreeMain

func TreeMain()

func Walk

func Walk(t *tree.Tree, ch chan int)

Walk walks the tree t sending all values from the tree to the channel ch.

func WordCount

func WordCount(s string) map[string]int

func WordCountTest

func WordCountTest()

Types

type ErrNegativeSqrt

type ErrNegativeSqrt float64

func (ErrNegativeSqrt) Error

func (e ErrNegativeSqrt) Error() string

type IPAddr

type IPAddr [4]byte

func (IPAddr) String

func (ip IPAddr) String() string

TODO: Add a "String() string" method to IPAddr.

type Image

type Image struct{}

func (Image) At

func (img Image) At(x, y int) color.Color

func (Image) Bounds

func (img Image) Bounds() image.Rectangle

func (Image) ColorModel

func (img Image) ColorModel() color.Model

type MyReader

type MyReader struct{}

func (MyReader) Read

func (reader MyReader) Read(dst []byte) (int, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL