Skip to content

Add solution for Challenge 6 by mactavishz#1792

Merged
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
mactavishz:challenge-6-mactavishz
Jun 15, 2026
Merged

Add solution for Challenge 6 by mactavishz#1792
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
mactavishz:challenge-6-mactavishz

Conversation

@mactavishz

Copy link
Copy Markdown
Contributor

Challenge 6 Solution

Submitted by: @mactavishz
Challenge: Challenge 6

Description

This PR contains my solution for Challenge 6.

Changes

  • Added solution file to challenge-6/submissions/mactavishz/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

A new Go file is added under challenge-6/submissions/mactavishz/ implementing the CountWordFrequency(text string) map[string]int function. It removes apostrophes, compiles a regex to split on non-alphanumeric sequences, lowercases each token, skips empty tokens, and returns a word-frequency map.

Changes

CountWordFrequency Solution

Layer / File(s) Summary
CountWordFrequency implementation
challenge-6/submissions/mactavishz/solution-template.go
Adds package challenge6 with the exported CountWordFrequency function: removes apostrophes, splits on non-alphanumeric delimiter runs via compiled regex, lowercases each token, skips empty tokens, and returns a map[string]int of word counts.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • RezaSi/go-interview-practice#841: Adds the same CountWordFrequency(text string) map[string]int in challenge6 with overlapping regex-based tokenization and apostrophe-removal logic.
  • RezaSi/go-interview-practice#1619: Implements CountWordFrequency with nearly identical preprocessing: apostrophe removal, non-alphanumeric splitting, lowercasing, and map-based counting.
  • RezaSi/go-interview-practice#799: Adds the same challenge6.CountWordFrequency with equivalent apostrophe removal, regex delimiter splitting, lowercasing, and frequency counting logic.

Poem

🐇 A map of words, each counted with care,
Apostrophes gone, and lowercased fair,
The regex splits tokens with non-alphanum glee,
Empty ones skipped — only real words set free!
Hop hop, the frequencies land in their place~ 🗺️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution for Challenge 6 by a specific contributor.
Description check ✅ Passed The description is clearly related to the changeset, providing context about which challenge is being solved and confirming testing completion.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
challenge-6/submissions/mactavishz/solution-template.go (2)

23-23: ⚡ Quick win

Move 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 value

Simplify 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

📥 Commits

Reviewing files that changed from the base of the PR and between 0bc19aa and a81b804.

📒 Files selected for processing (1)
  • challenge-6/submissions/mactavishz/solution-template.go

@github-actions github-actions Bot merged commit a7f0eff into RezaSi:main Jun 15, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 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.

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