Skip to content

Add solution for Challenge 2 by PeterIregi#1794

Open
PeterIregi wants to merge 1 commit into
RezaSi:mainfrom
PeterIregi:challenge-2-PeterIregi
Open

Add solution for Challenge 2 by PeterIregi#1794
PeterIregi wants to merge 1 commit into
RezaSi:mainfrom
PeterIregi:challenge-2-PeterIregi

Conversation

@PeterIregi

Copy link
Copy Markdown
Contributor

Challenge 2 Solution

Submitted by: @PeterIregi
Challenge: Challenge 2

Description

This PR contains my solution for Challenge 2.

Changes

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

Testing

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

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

A new Go file is added under challenge-2/submissions/PeterIregi/ implementing a main function that reads one line from stdin and prints its reverse. An exported ReverseString function performs the reversal by iterating from the last index to the first and concatenating bytes into a new string.

Changes

Challenge 2 String Reversal Submission

Layer / File(s) Summary
main entrypoint and ReverseString implementation
challenge-2/submissions/PeterIregi/solution-template.go
main scans one line from stdin and passes it to ReverseString, which builds the reversed string via a backwards index loop and concatenation, then prints the result.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Poem

🐇 Hop, hop, flip the string,
From the end back to the start I spring!
Each byte swapped with a loop so neat,
A reversed word — my favorite treat.
ReverseString runs with glee,
Challenge-2 solved by PeterIregi! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a Challenge 2 solution submission by PeterIregi.
Description check ✅ Passed The description is related to the changeset, providing context about the Challenge 2 solution submission and confirming testing status.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ed5efc33-8ac2-4740-9c5f-4e1b0c305853

📥 Commits

Reviewing files that changed from the base of the PR and between 0bc19aa and 794e4f6.

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

Comment on lines +24 to +29
func ReverseString(s string) string {
newString:= ""
for i:=len(s)-1;i>=0;i--{
newString+=string(s[i])
}
return newString

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Unicode reversal is incorrect because this loop reverses bytes, not characters.

At Line 26-27, indexing s[i] reverses raw bytes and breaks UTF-8 input (e.g., the bilingual test case), so output can be corrupted for non-ASCII text. Switching to rune-based reversal also avoids the O(n²) string concatenation pattern.

Proposed fix
 func ReverseString(s string) string {
-	newString:= ""
-	for i:=len(s)-1;i>=0;i--{
-	    newString+=string(s[i])
-	}
-	return newString
+	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)
 }
📝 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 {
newString:= ""
for i:=len(s)-1;i>=0;i--{
newString+=string(s[i])
}
return newString
func ReverseString(s string) string {
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)
}

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.

1 participant