GithubHelp home page GithubHelp logo

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

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

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
}

`

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?

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
}

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.

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.

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.

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
}

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

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?

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

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.

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}

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.