Add solution for Challenge 6 by mactavishz#1792
Conversation
WalkthroughA new Go file is added under ChangesCountWordFrequency Solution
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
challenge-6/submissions/mactavishz/solution-template.go (2)
23-23: ⚡ Quick winMove regex compilation to package level to avoid recompilation on every call.
Compiling the regex pattern inside the function means it will be recompiled on every invocation of
CountWordFrequency. Since the pattern is constant, declare it as a package-level variable to compile it once.⚡ Proposed refactor
+var wordSplitRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`) + // CountWordFrequency takes a string containing multiple words and returns // a map where each key is a word and the value is the number of times that // word appears in the string. The comparison is case-insensitive.func CountWordFrequency(text string) map[string]int { freqMap := make(map[string]int, 100) - re := regexp.MustCompile(`[^a-zA-Z0-9]+`) text = strings.ReplaceAll(text, "'", "") text = strings.TrimSpace(text) - words := re.Split(text, -1) + words := wordSplitRegex.Split(text, -1)
32-37: 💤 Low valueSimplify map increment using Go's zero-value behavior.
In Go, accessing a non-existent map key returns the zero value (0 for int), so the explicit exists check is unnecessary. You can simplify the increment to
freqMap[w]++.♻️ Proposed simplification
w = strings.ToLower(w) - count, exists := freqMap[w] - if exists { - freqMap[w] = count + 1 - } else { - freqMap[w] = 1 - } + freqMap[w]++
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 675598bf-095f-4c0a-830a-a586b3c8ed25
📒 Files selected for processing (1)
challenge-6/submissions/mactavishz/solution-template.go
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @mactavishz! 📊 Scoreboards and badges will be updated shortly. |
Challenge 6 Solution
Submitted by: @mactavishz
Challenge: Challenge 6
Description
This PR contains my solution for Challenge 6.
Changes
challenge-6/submissions/mactavishz/solution-template.goTesting
Thank you for reviewing my submission! 🚀