Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ex 1.4 solution #22

Open
enku opened this issue Sep 16, 2019 · 2 comments
Open

Ex 1.4 solution #22

enku opened this issue Sep 16, 2019 · 2 comments

Comments

@enku
Copy link

enku commented Sep 16, 2019

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.

@torbiak
Copy link
Owner

torbiak commented Sep 16, 2019

Like you, I also tried to do the exercises without using concepts and libraries that hadn't been introduced yet, since that seems to be the intent of the exercises in a lot of books, but I decided not to be a purist about it once I realized doing so would make many of my solutions awkward or less idiomatic. It's been a while since I went through the book but I feel like a lot of the exercises in GOPL were intended to make you research and think about different solutions instead of merely applying the material that was just learned.

Also, on page 12, a couple paragraphs before Exercise 1.4 is given, it says:

ReadFile returns a byte slice that must be converted into a string so it can be split by strings.Split. We will discuss strings and byte slices at length in Section 3.5.4.

which seems like a nudge towards reading ahead and using string and slice related functions that haven't been introduced yet. Also, in the preface they say they assume the reader has programmed before and that they won't "spell everything out as if for a total beginner", which I think gives them some latitude in specifying less guided exercises.

What was your solution? If I wanted to avoid using append I guess I could build up formatted strings in the foundIn map instead of using fmt.Sprintf. Sprintf hasn't been introduced yet, but it's used in examples a bunch of times before ever being described. Yeah...playing the game of only using previously introduced libs just wasn't fun for me.

@enku
Copy link
Author

enku commented Sep 16, 2019

When I spoke to a colleague who is also going through the book, he indicated that he thought that the intent of the author is to have the reader solve the problems with the concepts already given and not to "look ahead", so I'm not sure. Maybe it is mentioned in the "how to use this book" section.

How I ended up solving it is to only use maps because, although slices were introduced, the author indicated that it was to be covered in detail later. So I created a structure like this:

map[string]map[string]int

Where I track the number of occurrences of a line per file, then

for filename, counts := range names {
    if counts[line] > 0 {
        fmt.Printf("\t%s\n", filename)
    }
}

My thinking was "well they want us to use maps, but I don't think they want us to use slices yet", but then, as you say, I walk away from my solution feeling awkward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants