-
-
Notifications
You must be signed in to change notification settings - Fork 712
Add solution for Challenge 2 by kiramux #729
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
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,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||
| "bufio" | ||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // ReverseString returns the reversed string of s. | ||||||||||||||||||||||||||||||||||||||||||||
| func ReverseString(s string) string { | ||||||||||||||||||||||||||||||||||||||||||||
| if len(s) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| str := []byte(s) | ||||||||||||||||||||||||||||||||||||||||||||
| left := 0 | ||||||||||||||||||||||||||||||||||||||||||||
| right := len(str) - 1 | ||||||||||||||||||||||||||||||||||||||||||||
| for left < right { | ||||||||||||||||||||||||||||||||||||||||||||
| str[left], str[right] = str[right], str[left] | ||||||||||||||||||||||||||||||||||||||||||||
| left++ | ||||||||||||||||||||||||||||||||||||||||||||
| right-- | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return string(str) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+37
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. Use The current implementation converts the string to For example:
Apply this diff to fix the Unicode handling: func ReverseString(s string) string {
- if len(s) == 0 {
- return ""
- }
- str := []byte(s)
- left := 0
- right := len(str) - 1
- for left < right {
- str[left], str[right] = str[right], str[left]
- left++
- right--
+ r := []rune(s)
+ for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
+ r[i], r[j] = r[j], r[i]
}
- return string(str)
+ return string(r)
}📝 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.
Check for scanner errors after reading input.
The scanner may encounter errors while reading from stdin. After
scanner.Scan()returns false, you should checkscanner.Err()to distinguish between EOF and actual errors.Apply this diff to add error handling:
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.Fprintln(os.Stderr, "Error reading input:", err) + os.Exit(1) + }📝 Committable suggestion
🤖 Prompt for AI Agents