-
-
Notifications
You must be signed in to change notification settings - Fork 705
Add solution for Challenge 2 by macborowy #572
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 macborowy #572
Conversation
WalkthroughAdds a new Go program for Challenge 2 that reads a line from stdin, reverses the string via a new exported function ReverseString using rune slicing, and prints the result. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant Main as Go Program (main)
participant IO as bufio.Scanner / os.Stdin
participant Fn as ReverseString
participant Out as fmt.Println / os.Stdout
User->>Main: Run program
Main->>IO: Read single line from stdin
IO-->>Main: Input string s
Main->>Fn: ReverseString(s)
Fn-->>Main: reversed string r
Main->>Out: Print r
Out-->>User: Output reversed string
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 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/macborowy/solution-template.go (1)
23-30: LGTM! Correct Unicode handling.The implementation correctly handles Unicode characters by converting to runes before reversing. The two-pointer swap technique is efficient and idiomatic.
Optional: The spacing in
len(r) - 1is not standard Go formatting. Runninggofmtwould change it tolen(r)-1, though this is purely cosmetic.
| 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) | ||
| } | ||
| } |
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.
Add error handling for scanner errors.
The code checks scanner.Scan() but does not verify scanner.Err() afterward. If Scan() returns false due to an error (not just EOF), the error is silently ignored, which could mask real I/O problems.
Apply this diff to add proper error handling:
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)
+ }
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 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) | |
| } | |
| } | |
| 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) | |
| } | |
| } |
🤖 Prompt for AI Agents
In challenge-2/submissions/macborowy/solution-template.go around lines 9 to 21,
the main function uses scanner.Scan() but never checks scanner.Err(), so I/O
errors are silently ignored; after the Scan loop (or after the Scan call)
capture scanner.Err(), and if non-nil print an error message to stderr (or log
and exit with non-zero status) to surface the I/O failure; ensure you only print
the reversed output when Scan succeeded and handle/return on scanner.Err() to
avoid masking read errors.
Challenge 2 Solution
Submitted by: @macborowy
Challenge: Challenge 2
Description
This PR contains my solution for Challenge 2.
Changes
challenge-2/submissions/macborowy/solution-template.goTesting
Thank you for reviewing my submission! 🚀