-
-
Notifications
You must be signed in to change notification settings - Fork 721
Add solution for Challenge 2 by inok94 #773
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 submission file for Challenge 2 that implements a string reversal solution. The program reads a single line from standard input, reverses it using a ReverseString function with strings.Builder, and outputs the result. 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 (2)
challenge-2/submissions/inok94/solution-template.go (2)
10-22: Consider adding error handling for scanner errors.While the logic correctly handles the single-line input case, consider checking
scanner.Err()after the scan to catch potential I/O errors. This would make the solution more robust.You could add after line 21:
if err := scanner.Err(); err != nil { fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err) os.Exit(1) }
29-29: Inconsistent indentation - rungofmt.This line appears to use spaces for indentation instead of tabs. Run
gofmt -w solution-template.goto automatically fix formatting according to Go conventions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-2/submissions/inok94/solution-template.go(1 hunks)
🔇 Additional comments (1)
challenge-2/submissions/inok94/solution-template.go (1)
1-8: LGTM!Package declaration and imports are appropriate for this string reversal program.
| func ReverseString(s string) string { | ||
| builder := strings.Builder{} | ||
| builder.Grow(len(s)) | ||
| for i := len(s) - 1; i >= 0; i-- { | ||
| builder.WriteByte(s[i]) | ||
| } | ||
|
|
||
| return builder.String() | ||
| } |
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.
Critical: Byte-level reversal breaks UTF-8 multi-byte characters.
The current implementation uses s[i] which indexes individual bytes, not Unicode code points (runes). This will corrupt multi-byte UTF-8 characters like emoji, Chinese characters, etc.
For example:
- Input:
"Hello世界"(where 世 and 界 are 3 bytes each) - Expected:
"界世olleH" - Actual: Corrupted UTF-8 byte sequence
Apply this diff to correctly handle UTF-8:
func ReverseString(s string) string {
- builder := strings.Builder{}
- builder.Grow(len(s))
- for i := len(s) - 1; i >= 0; i-- {
- builder.WriteByte(s[i])
- }
-
- return builder.String()
+ 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 approach converts the string to a rune slice, reverses in-place by swapping, and converts back to a string.
🤖 Prompt for AI Agents
In challenge-2/submissions/inok94/solution-template.go around lines 25 to 33,
the function reverses bytes (using s[i]) which corrupts multi-byte UTF-8
characters; fix by converting the string to a []rune, reverse the rune slice
in-place by swapping runes from ends toward the center, then convert the rune
slice back to a string and return it so UTF-8 characters remain intact.
Challenge 2 Solution
Submitted by: @inok94
Challenge: Challenge 2
Description
This PR contains my solution for Challenge 2.
Changes
challenge-2/submissions/inok94/solution-template.goTesting
Thank you for reviewing my submission! 🚀