Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions challenge-2/submissions/kiramux/solution-template.go
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)
}
Comment on lines +11 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Check for scanner errors after reading input.

The scanner may encounter errors while reading from stdin. After scanner.Scan() returns false, you should check scanner.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

‼️ 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.

Suggested change
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()
// Call the ReverseString function
output := ReverseString(input)
// Print the result
fmt.Println(output)
}
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)
}
🤖 Prompt for AI Agents
In challenge-2/submissions/kiramux/solution-template.go around lines 11 to 20,
the code calls scanner.Scan() but does not check scanner.Err() afterwards;
update the code so that after the Scan attempt you call scanner.Err() and if it
returns a non-nil error print a descriptive message to stderr (or log) and exit
with a non-zero status to signal failure, otherwise proceed normally (nil error
implies EOF/no input). Ensure the error handling runs when Scan() returns false
so real I/O errors are distinguished from EOF.

}

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Use []rune instead of []byte to correctly handle Unicode characters.

The current implementation converts the string to []byte, which reverses raw bytes rather than Unicode characters. This will corrupt multi-byte UTF-8 characters like emojis, Chinese characters, accented letters, etc.

For example:

  • Input: "Hello, 世界"
  • Expected: "界世 ,olleH"
  • Actual with []byte: Mangled output with broken UTF-8 sequences

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

‼️ 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.

Suggested change
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)
}
func ReverseString(s string) string {
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(r)
}
🤖 Prompt for AI Agents
In challenge-2/submissions/kiramux/solution-template.go around lines 24 to 37,
the function reverses raw bytes using []byte which corrupts multi-byte UTF-8
characters; change the implementation to convert the string to []rune, perform
the in-place two-pointer swap on runes, then return string(runes) so Unicode
characters (emojis, CJK, accented letters) are handled correctly.

Loading