Add solution for Challenge 2 by PeterIregi#1794
Conversation
WalkthroughA new Go file is added under ChangesChallenge 2 String Reversal Submission
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ed5efc33-8ac2-4740-9c5f-4e1b0c305853
📒 Files selected for processing (1)
challenge-2/submissions/PeterIregi/solution-template.go
| func ReverseString(s string) string { | ||
| newString:= "" | ||
| for i:=len(s)-1;i>=0;i--{ | ||
| newString+=string(s[i]) | ||
| } | ||
| return newString |
There was a problem hiding this comment.
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.
| 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) | |
| } |
Challenge 2 Solution
Submitted by: @PeterIregi
Challenge: Challenge 2
Description
This PR contains my solution for Challenge 2.
Changes
challenge-2/submissions/PeterIregi/solution-template.goTesting
Thank you for reviewing my submission! 🚀