Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions challenge-1/submissions/brenoamin/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
)

func main() {
var a, b int
// Read two integers from standard input
_, err := fmt.Scanf("%d, %d", &a, &b)
if err != nil {
fmt.Println("Error reading input:", err)
return
}

// Call the Sum function and print the result
result := Sum(a, b)
fmt.Println(result)
}

// Sum returns the sum of a and b.
func Sum(a int, b int) int {
return a + b
}
37 changes: 37 additions & 0 deletions challenge-6/submissions/brenoamin/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Package challenge6 contains the solution for Challenge 6.
package challenge6

import (
"regexp"
"strings"
)

// 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.
//
// Words are defined as sequences of letters and digits.
// All words are converted to lowercase before counting.
// All punctuation, spaces, and other non-alphanumeric characters are ignored.
//
// For example:
// Input: "The quick brown fox jumps over the lazy dog."
// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1}
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).

cleaned := strings.ReplaceAll(re.ReplaceAllString(strings.ToLower(text), " "), "'", "")
fields := strings.Fields(cleaned)

for _, word := range fields {
_, exists := freq[word]
if exists {
freq[word]++
continue
}
freq[word] = 1
}

return freq
}