Skip to content

Conversation

@brenoamin
Copy link
Contributor

Comment

Implements Challenge 6: Word Frequency Counter.
Adds the CountWordFrequency function 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:

input := "Hello world hello"
output := CountWordFrequency(input)
// output -> map[string]int{"hello": 2, "world": 1}

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 9, 2025

Walkthrough

Adds two new Go program solution files for coding challenges. Challenge-1 includes a Sum function that adds two integers and a main program to read input and print the result. Challenge-6 introduces a CountWordFrequency function that normalizes text, counts word occurrences (case-insensitive, alphanumeric-only), and returns a frequency map.

Changes

Cohort / File(s) Summary
Challenge-1 Sum Solution
challenge-1/submissions/brenoamin/solution-template.go
Adds Sum(a int, b int) int exported function and main program that reads two integers from standard input using formatted scan and prints their sum
Challenge-6 Word Frequency Counter
challenge-6/submissions/brenoamin/solution-template.go
Adds CountWordFrequency(text string) map[string]int exported function that converts input to lowercase, normalizes non-alphanumeric characters to spaces using regex, splits into words, and returns a map of word frequency counts

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Challenge-1 Sum function uses straightforward integer addition with basic input parsing; verify Scanf error handling is appropriate
  • Challenge-6 CountWordFrequency uses regex pattern matching and map construction; confirm regex pattern correctly identifies word boundaries and handles edge cases (empty input, multiple spaces, punctuation)

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'Feature/challenge 6' is overly vague and does not clearly convey the main change; it refers to a challenge number without describing what the feature actually does (word frequency counting). Revise the title to be more descriptive, such as 'Add CountWordFrequency function for word frequency counting' to clearly communicate the main change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly explains the Challenge 6 implementation, including the CountWordFrequency function, case-insensitive comparison, and provides a concrete example.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between daf4fa9 and 3675e99.

📒 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']+`)
Copy link
Contributor

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 returns

Then 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).

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.

1 participant