GithubHelp home page GithubHelp logo

gopl's Introduction

GOPL solutions

Solutions to every exercise in The Go Programming Language.

Many of the exercises are based on an example from the book or an earlier exercise, and I've copied files as needed so there's a separate package for each exercise. If I'd been thinking I would have committed as I went so that it's easier to use git to see what I started from. As it is you'll need to determine the predecessor example or exercise and use diff for exercises after 5.6.

Interesting exercises

Package list

  • ex1.1: ex1.1 prints command line arguments.
  • ex1.2: ex1.2 prints commandline indexes and arguments.
  • ex1.3: ex1.3 compares string concatenation techniques.
  • ex1.4: ex1.4 prints the count and text of lines that appear more than once in the input.
  • ex1.5: ex1.5 generates GIF animations of random Lissajous figures, with a green-on-black palette.
  • ex1.6: ex1.6 generates GIF animations of random Lissajous figures, with a gradient applied on the time dimension.
  • ex1.7: ex1.7 streams the content found at each specified URL to stdout.
  • ex1.8: ex1.8 streams the content found at each specified URL to stdout, and appends http:// to arguments as needed.
  • ex1.9: ex1.9 streams the content found at each specified URL to stdout and prints the HTTP status code.
  • ex1.10: ex1.10 fetches URLs in parallel and reports their times and sizes.
  • ex1.12: ex1.6 generates GIF animations of random Lissajous figures, with a gradient applied on the time dimension and the number of cycles to display can be supplied in the url query string when running as a web server.
  • ex2.1: ex2.1 performs Celsius, Fahrenheit, and Kelvin conversions.
  • ex2.2: ex2.2 prints measumrents given on the command line or stdin in various units.
  • ex2.3: ex2.3: compare popcount implementations: looping and single-expression bytewise table lookups.
  • ex2.4: ex2.4: compare popcount implementations, including looping table lookups and shift value.
  • ex2.5: ex2.4: compare popcount implementations, including clear rightmost.
  • ex3.1: ex3.1 prints an svg image, ignoring non-finite vertexes.
  • ex3.2: ex3.2 prints an SVG rendering of an eggbox or saddle.
  • ex3.3: ex3.3 prints an svg image, coloring its vertices based on their height.
  • ex3.4: ex3.4 serves SVG rendering of a 3-D surface function over http.
  • ex3.5: ex3.5 emits a full-color PNG image of the Mandelbrot fractal.
  • ex3.6: ex3.5 emits a supersampled image of the Mandelbrot fractal.
  • ex3.7: ex3.7 visualizes how many iterations it takes to find complex roots of a quartic equation using Newton's method, using different colors for different roots.
  • ex3.8: ex3.8 compares different numeric types when rendering fractals.
  • ex3.9: ex3.9 serves images of fractals over http.
  • ex3.10: ex3.10 inserts commas into integer strings given as command-line arguments, without using recursion.
  • ex3.11: ex3.11 inserts commas into floating point strings with an optional sign, given as command-line arguments.
  • ex3.12: ex3.12 determines if strings are anagrams of each other.
  • ex3.13: ex3.13 is a short definition of byte unit constants.
  • ex4.1: ex4.1 computes the number of bits different between two hashes.
  • ex4.2: ex4.2 prints the SHA hash of stdin.
  • ex4.3: ex4.3 reverses an array
  • ex4.4: ex4.4 rotates a slice of ints by one position to the left.
  • ex4.5: ex4.5 dedupes a slice of strings.
  • ex4.6: ex4.6 reverses a utf8 string.
  • ex4.7: ex4.7 reverses a utf8 string in-place.
  • ex4.8: ex4.8 computes counts of Unicode characters.
  • ex4.9: ex4.9 counts word frequency for stdin.
  • ex4.10: ex4.10 prints a table of GitHub issues matching the search terms, organized by the past day, month, and year.
  • ex4.11: Package github provides a Go API for the GitHub issue tracker.
  • ex4.12: ex4.12 gets, indexes, and searches xkcd comic metadata.
  • ex4.13: ex4.13 searches OMDB by title and downloads a movie poster.
  • ex4.14: Package github provides a Go API for the GitHub issue tracker.
  • ex5.1: ex5.1 prints the links in an HTML document read from standard input.
  • ex5.2: ex5.2 counts the frequency of different tags in an html document on stdin.
  • ex5.3: ex5.3 prints nonempty text tokens from an html document on stdin.
  • ex5.4: ex5.4 prints the links in an HTML document read from standard input, including those for images, scripts, and style sheets.
  • ex5.5: ex5.5 counts the number of words and images at a url.
  • ex5.6: ex5.6 computes an SVG rendering of a 3-D surface function, using a bare return statement.
  • ex5.7: ex5.7 pretty-prints html.
  • ex5.8: ex5.8 finds an html element node by id attribute.
  • ex5.9: ex5.9 expands shell-style variable references on stdin.
  • ex5.10: ex5.10 sorts courses topologically based on hard-coded dependencies.
  • ex5.11: ex5.11 reports on cycles in course dependencies.
  • ex5.12: ex5.12 prints the outline of an HTML document tree.
  • ex5.13: ex5.13 saves a local mirror of a website.
  • ex5.14: ex5.14 prints a random course's prerequisites, recursively.
  • ex5.15: ex5.15 explores variadic min and max functions.
  • ex5.16: ex5.16 provides a variadic string-joining function.
  • ex5.17: ex5.17 uses a variadic ElementsByTagName function to find html nodes.
  • ex5.18: ex5.18 saves the contents of a URL to a local file.
  • ex5.19: ex5.19 returns a non-zero value using panic and recover, contradicting the function signature.
  • ex6.1: ex6.1 is a bit vector integer set.
  • ex6.2: ex6.2 is a integer set with a variadic AddAll method.
  • ex6.3: ex6.3 is a bit vector integer set with binary set operations.
  • ex6.4: ex6.4 is an integer set with an Elems method.
  • ex6.5: ex6.5 is a variable-word-size bit vector intset.
  • ex7.1: ex7.1 provides line and word counters.
  • ex7.2: ex7.2 wraps a writer to count written words.
  • ex7.3: ex7.3 provides insertion sort using an unbalanced binary tree, and a String method to visualize the tree.
  • ex7.4: ex7.4 provides a simple string reader.
  • ex7.5: ex7.5 provides a LimitReader that reports EOF at a given offset.
  • ex7.6: ex7.6 prints flag arguments for different temperature scales, including Kelvin.
  • ex7.8: ex7.8 provides iterative columnar sorting for Persons.
  • ex7.9: ex7.9 serves an html table with a stable column sort.
  • ex7.10: ex7.10 uses sort.Interface to detect palindromes.
  • ex7.11: ex7.11 adds CRUD http endpoints to a simple PriceDB server.
  • ex7.12: ex7.12 convert's PriceDB list output to an html table.
  • ex7.13: ex7.13 adds pretty-printing to an arithmetic expression parser.
  • ex7.14: ex7.14 adds factorials to an arithmetic expression parser.
  • ex7.15: ex7.15 evaluates an expression using given variable bindings.
  • ex7.16: ex7.16 is a web-based calculator.
  • ex7.17: ex7.17 provides CSS-style selectors for XML.
  • ex7.18: ex7.18 parses XML into a tree of nodes, using the token-based API of encoding/xml.
  • ex8.1/clock: clock is a TCP server that periodically writes the time.
  • ex8.1/clockwall: clockwall listens to multiple clock servers concurrently.
  • ex8.2: ex8.2 is a minimal ftp server as per section 5.1 of RFC 959.
  • ex8.3: ex8.3 is a simple read/write client for TCP servers.
  • ex8.4: ex8.4 is a reverb server that uses sync.WaitGroup to choose when to close connections.
  • ex8.5: ex8.5 is a parallellized Mandelbrot fractal generator.
  • ex8.6: ex8.6 is a depth-limited web crawler.
  • ex8.7: ex8.7 mirrors a website to a given depth using multiple goroutines and rewrites local links.
  • ex8.8: ex8.8 is a reverb server that disconnects inactive clients.
  • ex8.9: ex8.9 is a concurrent du clone.
  • ex8.10: ex8.10 is a web-mirroring tool that can be gracefully interrupted using ctrl-c.
  • ex8.11: ex8.11 prints the first HTTP response received.
  • ex8.12: ex8.12 is a server that lets clients chat with each other.
  • ex8.13: ex8.13 is a chat server that disconnects inactive clients.
  • ex8.14: ex8.14 is a chat server that prompts clients for a name upon connection.
  • ex8.15: ex8.15 is a chat server that skips clients that are slow to process writes.
  • ex9.1: ex9.1 provides a concurrency-safe bank, with withdrawals.
  • ex9.2: ex9.2 lazily initializes the popcount LUT.
  • ex9.3: ex9.3 provides cancellable memoization of a function.
  • ex9.4: ex9.4 tests the performance of goroutine pipelines.
  • ex9.5: ex9.5 tests of performance of ping-ponging goroutines.
  • ex10.1: ex10.1 converts images to between jpg, png, and gif formats.
  • ex10.2: ex10.2 detects and reads zip and tar archives.
  • ex10.2/cmd/arprint: Print the names and contents of the files in a tar or zip archive.
  • ex10.4: ex10.4 lists go packages that transitively depend on the given packages.
  • ex11.1: ex11.1 computes counts of Unicode characters, and includes tests.
  • ex11.2: Package intset provides a set of integers based on a bit vector.
  • ex11.3: ex11.3 tests IsPalindrome on random non-palindromes.
  • ex11.4: ex11.4 tests IsPalindrome on strings including punctuation.
  • ex11.5: ex11.5 tests strings.Split using a table-driven test.
  • ex11.6: ex11.6 benchmarks popcount implementations.
  • ex11.7: Package intset provides a set of integers based on a bit vector.
  • ex12.1: ex12.1 uses reflection to print arbitrary values.
  • ex12.2: ex12.2 displays arbitrary values to a certain depth.
  • ex12.3: ex12.3 is a codec for s-expressions.
  • ex12.4: ex12.4 is a codec for s-expressions, with pretty-printing.
  • ex12.5: ex12.5 is a codec for json.
  • ex12.6: ex12.6 is an s-expression codec that doesn't encode zero values.
  • ex12.7: ex12.7 provides a streaming decoder for s-expressions.
  • ex12.8: ex12.8 can unmarshall s-expressions from an io.Reader.
  • ex12.9: ex12.9 is token-based API for decosing s-expressions.
  • ex12.10: ex12.10 is a s-expression codec that can decode booleans, floating point numbers, and registered interface values.
  • ex12.11: ex12.11 provides a reflection-based codec for URL query parameters.
  • ex12.12: ex12.12 provides a URL query parameter codec with validation triggered by struct tags.
  • ex12.13: ex12.13 is a s-expression codec that uses names found in struct tags.
  • ex13.1: ex13.1 provides a deep equivalence relation for arbitrary values.
  • ex13.2: ex13.2 determines if a value is cyclic.
  • ex13.3: ex13.3 bzip provides a concurrency-safe writer that uses bzip2 compression.
  • ex13.4: ex13.4 provides a bzip2 writer using the system's bzip2 binary.

License

CC BY-NC-SA 4.0, the same as the example code for The Go Programming Language.

gopl's People

Contributors

catay avatar jreisinger avatar torbiak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gopl's Issues

Wrong loop func in exercise2.3

func PopCountTableLoop(x uint64) int {
sum := 0
for i := 0; i < 8; i++ {
sum += int(pc[byte(x>>uint(i))])//Why use uint(i), which evaluates differently than PopCountTable func
}
return sum
}

Ex8.4: should pass *sync.WaitGroup

func handleConn(c net.Conn) {
	wg := sync.WaitGroup{}
	input := bufio.NewScanner(c)
	for input.Scan() {
		wg.Add(1)
		go echo(c, input.Text(), 1*time.Second, wg)
	}
	wg.Wait()
	// NOTE: ignoring potential errors from input.Err()
	c.Close()
}

Should pass &wg to go go echo(...).

ex7.4: incorrect logic

The implementation of Read sets the EOF error if no data is left after the last read; it should only be set in the subsequent read. Compare with the behavior of strings.Read()

The solution for 3.6 is wrong

Instead of averaging over the subpixels, it is sampling the pixels diagonal to the one being worked on, making the end result same as the one without super sampling. Changing epsX and epsY to 1/width and 1/height would be the correct way

ex4.4: Incorrect solution

The exercise requested to write a version of rotate that operates in a single pass. The answer provided rotates a slice by one position to the left, so while your code runs correctly it's not the right answer to the exercise.

Generic solution that performs in place rotation

// Rotates in place the slice s to the left by n steps
func rotateLeft(s []int, n int) {
	if len(s) == 0 || n%len(s) == 0 {
		return
	}

	n = n % len(s)

	ss := make([]int, len(s))

	copy(ss, s)
	copy(s, ss[n:])
	copy(s[len(s)-n:], ss[0:n])
}

Generic solution that doesn't perform in place rotation.

// Rotates the slice s to the left by n steps
func rotateLeft(s []int, n int) []int {
	if len(s) == 0 || n%len(s) == 0 {
		return s
	}

	n = n % len(s)
	s = append(s[n:], s[0:n]...)

	return s
}

bug(ex7.18) - panic when XML file is ended with new line or space

fetch http://www.w3.org/TR/2006/REC-xml11-20060816 | go run . leads to panic. The end of XML is new line and

case xml.CharData:
  parent := stack[len(stack)-1]
  parent.Children = append(parent.Children, CharData(tok))

stack[len(stack)-1] had runtime error: index out of range.

problem with ex9.3 cancelable function memo

Hi, I believe the specific switch and loop order in the code to deal with the problem of incorrectly canceling of other successive/concurrent requests on the same key you mentioned in comments maybe helps, but not sufficient.

For instance If goroutine A is executing its function then B and C follow up waiting for the ready event on the same key, after a while A cancels its request while B and C can only be left with the canceled result caused by A and have no chance to fix it. This is a problem because the done channel of B or C does not fire a cancel request. The memo server routine does no help in this case.

This may not pose a problem if each same key share the same cancel channel in the request but this is not necessarily true. One way I can think of to deal with it is to check and restart the request again if the result is incorrectly canceled by some previous request on the same key other than its own done channel.

Something like this:

func (memo *Memo) Get(key string, done <-chan struct{}) (interface{}, error) {
        ...
	select {
	case <-done:
		fmt.Println("get: queueing cancellation request")
		memo.cancels <- req
	default:
		// incorrectly canceled result set by others: restart request.
		if res.canceled {
			return Get(key, done)
		}
	}
	...

though it seems possible to cause some live lock problem in theory.

Missing Ex1.11

Ex1.11: Try fetchall with longer argument lists, such as samplesfrom the top million web sites available at alexa.com. Howdoes the program behave if a web site just doesn’t respond?

Ex 1.4 solution

So I just started reading the book and got to Exercise 1.4. I thought I knew a few "obvious" solutions to the problem, but all of them involved logic or data structures that were not yet prior to the exercise, so I thought that those solutions were "cheating". Finally I came up with a solution that I think only uses things that were taught so far in the chapter, but it was kinda hacky. Finally I decided to Google the solution and found this repo. The solution in this repo, however, used concepts that are not yet introduced (e.g. in, append, etc.) so I wonder whether or not that's valid.

This isn't really a bug report, just something to discuss.

Ex6.3 IntersectWith and DifferenceWith

IntersectWith and DifferenceWith is wrong, you should not append tword where i > len(s.words), test code below

var m, n IntSet
m.AddAll(1,3,5)
n.AddAll(1,2,5, 100, 1000)
fmt.Println("m:", &m)
fmt.Println("n:", &n)
//m.IntersectWith(&n)
//fmt.Println("After m.IntersectWith(&n) m:", &m)
m.DifferenceWith(&n)
fmt.Println("After m.DifferenceWith(&n) m:", &m)

//m: {1 3 5}
//n: {1 2 5 100 1000}
//After m.DifferenceWith(&n) m: {3 100 1000}
//m: {1 3 5}
//n: {1 2 5 100 1000}
//After m.IntersectWith(&n) m: {1 5 100 1000}

Wrong byte shift in "popcount" exercises.

Hey! Thanks for sharing your solutions. It's very helpful when it comes to thinking out my own solution and there is an option to compare it with someone else work.

Though I've found few mistakes made in the code. You missed multiplier for byte shift at the line 12 of your popcount solution:

	for i := 0; i < 8; i++ {
		sum += int(pc[byte(x>>uint(i))]) // Should be uint(i*8)
	}

Just look at this:

func PopCountTable(x uint64) int {
	return int(pc[byte(x>>(0*8))] +
		pc[byte(x>>(1*8))] +
		pc[byte(x>>(2*8))] +
		pc[byte(x>>(3*8))] +
		pc[byte(x>>(4*8))] +
		pc[byte(x>>(5*8))] +
		pc[byte(x>>(6*8))] +
		pc[byte(x>>(7*8))])
}

It also happens here and here.

Thank you.

overflow

const (

It seems that if you continue this pattern all the way up to YB, there will be overflow. But it does not make any sense, since numeric constant can be of arbitrary precision in GO.

Missing ex1.11

EX 1.11: Try fetchall with longer argument lists, such as samples from the top million web sites available at alexa.com. How does the program behave if a web site just doesn’t respond?

ex3.3: probably wrong solution

the entire histogram turned red after little change in the function. It seems like provided solution is similar to z <=> 0

see #13

a
becomes
b

Incorrect 8.1 -> clockwall.go

Hello,
If you execute the program then it gives
./clockwall NewYork=localhost:8010 London=localhost:8020 NewYork done 2019/09/09 11:01:08 cannot read from NewYork : read tcp 127.0.0.1:60572->127.0.0.1:8010: use of closed network connection London done 2019/09/09 11:01:08 cannot read from London : read tcp 127.0.0.1:60573->127.0.0.1:8020: use of closed network connection 2019/09/09 11:01:09 write tcp 127.0.0.1:8010->127.0.0.1:60572: write: broken pipe 2019/09/09 11:01:09 write tcp 127.0.0.1:8020->127.0.0.1:60573: write: broken pipe [1] - 49198 exit 1 ./clock -port 8010 [2] + 49216 exit 1 ./clock -port 8020
You also have a sleep in the main loop to allow sufficient time for all goroutines to finish when actually they never finish as the time is printed every second.

I have attached changed code which will work.
`
package main

import (
"fmt"
"io"
"log"
"net"
"os"
"strings"
"sync"
)

type clock struct {
name, host string
}

func main() {
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, "usage: clockwall NAME=HOST ...")
os.Exit(1)
}
clocks := createClocks(os.Args[1:])
var wg sync.WaitGroup
wg.Add(len(clocks))
for _, c := range clocks {
go startWatching(c, wg)
}
wg.Wait()
}

func (c *clock) watch(w io.Writer, r io.Reader) {
if _, err := io.Copy(w, r); err != nil {
log.Fatal(err)
}
}

func startWatching(c *clock, wg sync.WaitGroup) {
conn, err := net.Dial("tcp", c.host)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
defer wg.Done()
c.watch(os.Stdout, conn)
}

func createClocks(args []string) (clocks []*clock) {
clocks = make([]*clock, 0)
for _, pair := range args {
fields := strings.Split(pair, "=")
if len(fields) != 2 {
fmt.Fprintf(os.Stderr, "bad arg: %s\n", pair)
os.Exit(1)
}
clocks = append(clocks, &clock{fields[0], fields[1]})
}
return
}

`

Formatting not gofmt compliant

Hi,

I also noticed the examples are not gofmt compliant.
Maybe this was intentionally, if not, let me know and I can submit a PR.

$ gofmt -l . |head -10
ex1.12/main.go
ex1.3/concat_test.go
ex1.4/main.go
ex1.5/main.go
ex1.6/main.go
ex1.7/main.go
ex1.8/main.go
ex1.9/main.go
ex10.1/imgconv.go
ex10.2/arprint.go
...

Cheers,

Steven

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.