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 01 #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

EX 01 #150

wants to merge 2 commits into from

Conversation

vbhat
Copy link

@vbhat vbhat commented May 13, 2020

No description provided.

)

func main() {
filename := os.Args[1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to handle the scenario when no args or more args are provided before accessing the file name


func main() {
filename := os.Args[1]
data, err := ioutil.ReadFile(string(filename))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Args is of type []string no need of an explicit cast here


// Analyze takes in a string of words and returns a slice sorted in reverse numerical order based on the word count
func Analyze(data string) []WordCount {
words := splitWord(string(data))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You dont need the string(data) case here as well.. data is already a string

func (a ByCount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func splitWord(word string) []string {
slice := regexp.MustCompile("[^a-zA-Z0-9']+").Split(word, -1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice use of regexp and split

wordSlice := make([]WordCount, 0)

for index, word := range words {
words[index] = strings.ToLower(word)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just iterate over words and increment count in your map

for _, word := range words {
  wordMap[word]++
}

the zero value of ints is 0 in go so it should just work out fine

@vbhat vbhat changed the title Add corpus EX 01 May 16, 2020
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

Successfully merging this pull request may close these issues.

3 participants