Skip to content

Conversation

@DaniilYuz
Copy link
Contributor

Challenge 2 Solution

Submitted by: @DaniilYuz
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

  • Added solution file to challenge-2/submissions/DaniilYuz/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 6, 2025

Walkthrough

Introduces a new Go solution file in challenge-2/submissions/DaniilYuz/ that implements string reversal. The ReverseString function converts the string to a byte slice and reverses it in-place using a two-pointer approach. The main function reads input from stdin and prints the reversed output.

Changes

Cohort / File(s) Summary
New Go String Reversal Solution
challenge-2/submissions/DaniilYuz/solution-template.go
Added ReverseString function that reverses input strings by converting to a byte slice and swapping characters from both ends; added main function that reads a line from stdin using a scanner and prints the reversed result

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

  • #790: Implements the same ReverseString function at challenge-2/submissions/*/solution-template.go for Challenge 2 string reversal
  • #729: Adds ReverseString using the same two-pointer byte-swap in-place reversal implementation in a challenge-2 submission file
  • #593: Adds identical ReverseString(s string) string function with main that reads and prints reversed input

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add solution for Challenge 2 by DaniilYuz' directly reflects the main change: adding a solution file for Challenge 2.
Description check ✅ Passed The description is related to the changeset, providing details about the Challenge 2 submission and noting that tests pass and code follows best practices.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7b8b26a and 82f4a61.

📒 Files selected for processing (1)
  • challenge-2/submissions/DaniilYuz/solution-template.go (1 hunks)

Comment on lines +9 to +21
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)
}
}
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

Add error handling for scanner.

The code doesn't check scanner.Err() after scanning. If an I/O error occurs while reading from stdin, it will be silently ignored, which could lead to unexpected behavior or silent failures.

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.

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/DaniilYuz/solution-template.go around lines 9 to 21,
the scanner error is not checked after scanning which can silently ignore I/O
problems; after the Scan() block call scanner.Err(), and if it returns a non-nil
error write a descriptive message to stderr (using fmt.Fprintln(os.Stderr, ...))
and exit with a non-zero status (os.Exit(1)) or return, otherwise continue to
call ReverseString and print the output; ensure the error check runs after the
Scan() completes so genuine read errors are handled.

Comment on lines +24 to +30
func ReverseString(s string) string {
res := []byte(s)
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return string(res)
}
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

Fix Unicode handling in ReverseString.

The function uses []byte(s) which reverses individual bytes. This breaks multi-byte UTF-8 characters like emojis, Chinese/Arabic/Cyrillic text, etc. For example, reversing "Hello 世界" would corrupt the Chinese characters since each character spans multiple bytes.

Apply this diff to handle Unicode correctly:

 // ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-	res := []byte(s)
+	res := []rune(s)
 	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
 	    res[i], res[j] = res[j], res[i] 
 	}
 	return string(res)
 }
🤖 Prompt for AI Agents
In challenge-2/submissions/DaniilYuz/solution-template.go around lines 24 to 30,
the function reverses bytes using []byte(s) which corrupts multi-byte UTF-8
characters; convert the string to a []rune, reverse the rune slice in-place
(swap runes from ends moving inward), and return string(runes) so Unicode
characters are preserved while keeping the same function signature and behavior.

@RezaSi RezaSi merged commit 4177e00 into RezaSi:main Dec 6, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants