-
-
Notifications
You must be signed in to change notification settings - Fork 768
Feature/challenge 6 #861
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
base: main
Are you sure you want to change the base?
Feature/challenge 6 #861
Conversation
WalkthroughAdds two new Go program solution files for coding challenges. Challenge-1 includes a Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
challenge-6/submissions/brenoamin/solution-template.go (1)
27-34: Simplify map increment logic.The existence check is unnecessary. Go maps return the zero value (0 for int) when accessing non-existent keys, so you can directly increment.
Apply this diff to simplify the loop:
for _, word := range fields { - _, exists := freq[word] - if exists { - freq[word]++ - continue - } - freq[word] = 1 + freq[word]++ }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
challenge-1/submissions/brenoamin/solution-template.go(1 hunks)challenge-6/submissions/brenoamin/solution-template.go(1 hunks)
🔇 Additional comments (3)
challenge-1/submissions/brenoamin/solution-template.go (2)
7-19: LGTM!The main function correctly reads two integers from standard input, handles potential read errors gracefully, and prints the result of the Sum function.
21-24: LGTM!The Sum function implementation is correct and straightforward for a basic challenge solution.
challenge-6/submissions/brenoamin/solution-template.go (1)
20-37: Function logic is correct and handles the requirements well.The implementation correctly converts to lowercase, handles word extraction, and counts frequencies. The apostrophe handling (preserving during split, then removing) is a reasonable approach for contractions.
| func CountWordFrequency(text string) map[string]int { | ||
| freq := make(map[string]int) | ||
|
|
||
| re := regexp.MustCompile(`[^a-zA-Z0-9']+`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Move regex compilation to package level for better performance.
Compiling the regex on every function call is inefficient. Declare it as a package-level variable to compile once at initialization.
Apply this refactor at the top of the file, after the imports:
import (
"regexp"
"strings"
)
+var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z0-9']+`)
+
// CountWordFrequency takes a string containing multiple words and returnsThen update line 23-24:
- re := regexp.MustCompile(`[^a-zA-Z0-9']+`)
- cleaned := strings.ReplaceAll(re.ReplaceAllString(strings.ToLower(text), " "), "'", "")
+ cleaned := strings.ReplaceAll(nonAlphanumericRegex.ReplaceAllString(strings.ToLower(text), " "), "'", "")Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In challenge-6/submissions/brenoamin/solution-template.go around line 23, the
regexp is being compiled inside the function which is inefficient; move the
compilation to a package-level variable declared at the top of the file
immediately after the import block (e.g., declare a var like reWord =
regexp.MustCompile(`[^a-zA-Z0-9']+`) ) so it is compiled once at init, then
change the code at lines 23-24 to use that package-level variable (replace the
local regexp.MustCompile call with the reused variable).
Comment
Implements Challenge 6: Word Frequency Counter.
Adds the
CountWordFrequencyfunction that takes a string with multiple words and returns a map where each key is a word and the value is the number of times that word appears.The comparison is case-insensitive, so
"Hello"and"hello"are treated as the same word.Example: