-
-
Notifications
You must be signed in to change notification settings - Fork 709
Add solution for Challenge 2 by Johrespi #648
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
Conversation
WalkthroughA new Go solution file is added to the Challenge 2 submissions directory. It implements an exported Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 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-2/submissions/Johrespi/solution-template.go (1)
9-21: Consider checking for scanner errors.The scanner could encounter an error while reading input. While the current implementation may be acceptable for this challenge, it's good practice to check
scanner.Err()after the scan loop.Example:
func main() { // Read input from standard input scanner := bufio.NewScanner(os.Stdin) if scanner.Scan() { input := scanner.Text() // Call the ReverseString function output := ReverseString(input) // Print the result fmt.Println(output) } + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) + os.Exit(1) + } }
| func ReverseString(s string) string { | ||
| // TODO: Implement the function | ||
| var output string | ||
| for c := len(s) - 1; c >= 0; c-- { | ||
| output += string(s[c]) | ||
| } | ||
| return output | ||
| } |
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.
Fix critical Unicode bug: byte indexing breaks multi-byte UTF-8 characters.
The current implementation treats the string as a byte array by indexing with s[c]. This corrupts multi-byte UTF-8 characters like emojis, accented letters, or non-ASCII text. For example, reversing "café" or "Hello 👋" will produce garbled output.
Additionally, string concatenation in a loop has O(n²) time complexity because strings are immutable in Go.
Apply this diff to fix both issues:
-// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
- // TODO: Implement the function
- var output string
- for c := len(s) - 1; c >= 0; c-- {
- output += string(s[c])
- }
- return output
+ runes := []rune(s)
+ for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
+ runes[i], runes[j] = runes[j], runes[i]
+ }
+ return string(runes)
}This solution:
- Converts the string to a rune slice to properly handle Unicode characters
- Uses in-place swapping for O(n) time complexity
- Removes the obsolete TODO comment
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In challenge-2/submissions/Johrespi/solution-template.go around lines 24 to 31,
the current implementation indexes the string by bytes and concatenates in a
loop, which corrupts multi-byte UTF-8 characters and is O(n²); fix by converting
the input string to a rune slice, perform in-place swapping of runes
(two-pointer swap from ends toward center) to reverse in O(n) time, then return
a new string created from the rune slice, and remove the obsolete TODO comment.
Challenge 2 Solution
Submitted by: @Johrespi
Challenge: Challenge 2
Description
This PR contains my solution for Challenge 2.
Changes
challenge-2/submissions/Johrespi/solution-template.goTesting
Thank you for reviewing my submission! 🚀