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

Word Count Exercise 1 First Draft #145

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Saigo
A series of (hopefully cool!) exercises for those eager to learn Go
A series of (hopefully cool!) exercises for those eager to learn Go.


## Setting Up Your Go Environment

As new versions of the Go suite are released you will want an
As new versions of the Go suite are released, you will want an
easy way to stay up to date. So please follow the [Setup](setup-environment.md)
guide to install Go and build your workspace.

It is best to get this right the first time around so if you have
trouble please ask for help!
It is best to get this right the first time around; so if you have
trouble, please ask for help!


## Exercises
Expand All @@ -18,7 +18,7 @@ The Saigo exercises are intended to be a tool for the instructor. Experienced de
to use them as a way to jump right in the pool. However, to get the most out of them it is recommended that
learners find an instructor.

Some of the exercises may require serveral days to complete. Learners should consider building solutions incrementally and meeting with their instructor between iterations.
Some of the exercises may require serveral days to complete. Learners should consider building solutions incrementally and meeting with their instructor between iterations.

The [first](https://github.com/enova/saigo/tree/master/exercise-000-prep) exercise asks learners to go through Caleb
Doxsey's book [An Introduction to Programming in Go](https://www.golang-book.com/books/intro). Learners should schedule regular
Expand All @@ -40,11 +40,11 @@ an instructor.
### Engineering Tasks

Engineering tasks will ask you to write some code, usually an application of some sort.
As mentioned above, learners should routinely schedule brief (ten-minute) meetings with instructors
As mentioned above, when unsure about their approach, learners may schedule brief meetings with instructors
while working on engineering-tasks. You will want to avoid situations where you write 150 lines of code
only to find your solution has issues. Even learning can be agile.

Be ready to demo your application when it is completed. Instructors want to see it in action!
Once your task is complete, create a PR so one of the saigo bucketeers can take a look. If changes are requested, address them and push up a new commit onto the same PR.

## Recommended Resources

Expand Down
8 changes: 8 additions & 0 deletions exercise-001-corpus/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gofmt -w .
golint ./...
go build word_count.go
pushd corpus
go test
go test -bench=.
popd
./word_count -file 7oldsamr.txt
79 changes: 79 additions & 0 deletions exercise-001-corpus/corpus/corpus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package corpus

import (
"errors"
"io/ioutil"
"log"
"regexp"
"sort"
"strings"
)

func WordCountFromFileSorted(file string, reverse bool) (PairedList, error) {
contents, err := StringFromFile(file)
if err != nil {
return nil, err
}
words := WordCountMapFromString(contents)
plist := SortedMapPairedList(words, reverse)
return plist, nil
}

func SortedMapPairedList(unsorted map[string]int, reverse bool) PairedList {
if len(unsorted) == 0 {
return nil
}
sortable := mapToPairedList(unsorted)
if reverse {
sort.Sort(sort.Reverse(sortable))
} else {
sort.Sort(sortable)
}
return sortable
}

func mapToPairedList(unsorted map[string]int) PairedList {
sortable := make(PairedList, len(unsorted))
i := 0
for k, v := range unsorted {
sortable[i] = Pair{k, v}
i++
}
return sortable
}

func WordCountFromFile(file string) (PairedList, error) {
contents, err := StringFromFile(file)
if err != nil {
return nil, err
}
if len(contents) > 0 {
words := WordCountMapFromString(contents)
wordsPL := mapToPairedList(words)
return wordsPL, nil
} else {
return nil, errors.New("Empty File")
}
}

func StringFromFile(file string) (string, error) {
contents, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(contents), nil
}

func WordCountMapFromString(str string) map[string]int {
words := strings.Fields(str)
reg, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
log.Fatal(err)
}
wordMap := make(map[string]int)
for _, word := range words {
prettyWord := strings.ToLower(reg.ReplaceAllString(word, ""))
wordMap[prettyWord] += 1
}
return wordMap
}
Loading