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
35 changes: 35 additions & 0 deletions challenge-2/submissions/4m4x/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"bufio"
"fmt"
"os"
"strings"
)

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 +10 to +22
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

Add error handling for scanner.

The code doesn't check for scanner errors after the scan operation. While scanner.Scan() returns false for both EOF and errors, you should check scanner.Err() to handle actual I/O errors properly.

Apply this diff to add 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.

Suggested change
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/4m4x/solution-template.go around lines 10 to 22, the
code calls scanner.Scan() but does not check scanner.Err() afterward; add error
handling by calling err := scanner.Err() after the scan (or after the scanning
loop), and if err != nil write a descriptive message to stderr (e.g.,
fmt.Fprintln(os.Stderr, "input error:", err)) and exit with a non-zero status
(os.Exit(1)) so actual I/O errors are surfaced instead of being ignored.


// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
runes := []rune(s)
output := []string{}

for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(s[i]))
Comment on lines +29 to +30
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 | 🔴 Critical

Critical bug: incorrect indexing breaks Unicode support.

Line 30 uses s[i] (byte index) instead of runes[i] (rune index), which defeats the purpose of converting to runes on line 26. This will corrupt multi-byte UTF-8 characters like emoji or non-Latin scripts.

For example, reversing "Hello世界" would produce corrupted output because s[i] slices the original string byte-by-byte, splitting multi-byte UTF-8 sequences.

Apply this diff to fix the bug:

 	for i := len(runes) - 1; i > -1; i-- {
-		output = append(output, string(s[i]))
+		output = append(output, string(runes[i]))
 	}
📝 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
for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(s[i]))
for i := len(runes) - 1; i > -1; i-- {
output = append(output, string(runes[i]))
}
🤖 Prompt for AI Agents
In challenge-2/submissions/4m4x/solution-template.go around lines 29 to 30, the
loop currently appends string(s[i]) which indexes the original byte string and
corrupts multi-byte UTF-8 characters; change the append to use the rune slice
instead (append output with string(runes[i])) so each Unicode code point is
preserved when reversing, and remove any reliance on s[i] in this loop.

}

s = strings.Join(output, "")
return s
}
Loading