-
-
Notifications
You must be signed in to change notification settings - Fork 704
Add solution for Challenge 2 by redstrike #596
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
Add solution for Challenge 2 by redstrike #596
Conversation
WalkthroughA new Go program is added in challenge-2/submissions/redstrike/ that implements a ReverseString function to reverse input strings using a rune-based two-pointer swap, with a main function that reads a line from standard input and prints the reversed result. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 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: 0
🧹 Nitpick comments (2)
challenge-2/submissions/redstrike/solution-template.go (2)
9-21: Consider checking for scanner errors.After the scan loop, it's a Go best practice to check
scanner.Err()to distinguish between EOF and actual read errors, though for this challenge context the current implementation is acceptable.Optional improvement:
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) + } }
23-38: Implementation is correct and handles Unicode properly.The rune-based two-pointer reversal correctly handles multi-byte characters. As an optional improvement, Go's tuple assignment can simplify the swap.
Optional simplification:
func ReverseString(s string) string { // Swap runes or two-pointer technique runes := []rune(s) // Convert string to slice of runes - runesLen := len(runes) - halfRunesLen := runesLen / 2 + n := len(runes) - for i := 0; i < halfRunesLen; i++ { - r1 := runes[i] - r2 := runes[runesLen-1-i] - runes[i] = r2 - runes[runesLen-1-i] = r1 + for i := 0; i < n/2; i++ { + runes[i], runes[n-1-i] = runes[n-1-i], runes[i] } return string(runes) }
Challenge 2 Solution
Submitted by: @redstrike
Challenge: Challenge 2
Description
This PR contains my solution for Challenge 2.
Changes
challenge-2/submissions/redstrike/solution-template.goTesting
Thank you for reviewing my submission! 🚀