-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for Challenge 2 by nasseredine #761
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "bufio" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "golang.org/x/example/hello/reverse" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+11
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for scanner failures. The code doesn't check for scanner errors after 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)
}
+ // Check for scanner errors
+ if err := scanner.Err(); err != nil {
+ fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
+ os.Exit(1)
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ReverseString returns the reversed string of s. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func ReverseString(s string) string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Don't reinvent the wheel! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return reverse.String(s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Implement string reversal locally instead of using example dependency. Since the external dependency should be avoided (see previous comment), implement the reversal directly. Here's a Unicode-aware implementation: // ReverseString returns the reversed string of s.
func ReverseString(s string) string {
- // Don't reinvent the wheel!
- return reverse.String(s)
+ // Convert to runes for proper Unicode handling
+ 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 handles Unicode code points correctly (e.g., "Hello, 世界" → "界世 ,olleH"). 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🧩 Analysis chain
Avoid using example repositories as production dependencies.
The
golang.org/x/example/hello/reversepackage is from an example/tutorial repository, not intended for production use. Example code may change without notice and lacks API stability guarantees.Consider implementing the string reversal directly or using a well-established utility library. For a simple reversal, a local implementation would be more appropriate:
- "golang.org/x/example/hello/reverse"Then update the
ReverseStringfunction with a proper implementation (see separate comment).First, verify the package availability and check its documentation:
🌐 Web query:
💡 Result:
Summary — what it is and how to use it
The package golang.org/x/example/hello/reverse is a tiny example library whose purpose is to reverse values (primarily strings) rune-wise. It exposes a single exported function: String(s string) string, which returns the input string reversed (by runes). [1]
Intended use: it’s part of the golang.org/x/example collection and exists as a teaching/example package (used by the hello example program and tutorials) to demonstrate how to write a simple library, how to convert between string and []rune, and how to write examples/tests. You can import it in your code (import "golang.org/x/example/hello/reverse") and call reverse.String("hello") to get "olleh". [2][3]
References
Remove the example package dependency and implement string reversal locally.
The
golang.org/x/example/hello/reversepackage is part of the golang.org/x/example collection and exists as a teaching/example package, not intended for production use. Using educational example code as a production dependency introduces maintenance risks and violates best practices.Remove the import:
- "golang.org/x/example/hello/reverse"Implement
ReverseStringlocally using a simple rune-based reversal instead of relying on the example package.🤖 Prompt for AI Agents