-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for Challenge 19 by iamsurajmandal #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add solution for Challenge 19 by iamsurajmandal #716
Conversation
WalkthroughAdds a new Go solution file with four slice-manipulation utility functions (FindMax, RemoveDuplicates, ReverseSlice, FilterEven) and a main function demonstrating their usage on a test slice. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Pre-merge checks✅ Passed checks (3 passed)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
challenge-19/submissions/iamsurajmandal/solution-template.go (1)
62-67: Consider pre-allocating the result slice.Pre-allocating with the known length improves performance by avoiding multiple allocations during append operations.
- result := []int{} + result := make([]int, 0, len(slice)) for i := len(slice) - 1; i >= 0 ; i-- { result = append(result, slice[i]) }Alternatively, for even better performance, allocate with full length and assign by index:
func ReverseSlice(slice []int) []int { result := make([]int, len(slice)) for i := 0; i < len(slice); i++ { result[len(slice)-1-i] = slice[i] } return result }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-19/submissions/iamsurajmandal/solution-template.go(1 hunks)
🔇 Additional comments (3)
challenge-19/submissions/iamsurajmandal/solution-template.go (3)
8-27: LGTM!The main function provides clear demonstrations of all utility functions with well-labeled output.
46-58: LGTM!The deduplication logic correctly preserves order using a map to track seen values.
70-80: LGTM with minor style note.The filtering logic correctly identifies even numbers. Minor: the parentheses around the condition on line 75 are unnecessary in Go (idiomatic style omits them for simple conditions).
Optional style refinement:
- if (value % 2 == 0 ) { + if value % 2 == 0 { result = append(result, value) }
| if (len(numbers) < 1) { | ||
| return 0 | ||
| } | ||
| max := int(math.Inf(-1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix: Avoid converting math.Inf to int.
Converting math.Inf(-1) (negative infinity) to int is problematic—the behavior is implementation-defined and can produce unexpected results. Initialize max with the first element after confirming the slice is non-empty, or use math.MinInt.
Apply this diff to use a clearer initialization:
func FindMax(numbers []int) int {
- if (len(numbers) < 1) {
+ if len(numbers) == 0 {
return 0
}
- max := int(math.Inf(-1))
- for i := 0; i < len(numbers); i++ {
- if (numbers[i] > max) {
- max = numbers[i]
- }
- }
+ max := numbers[0]
+ for _, num := range numbers {
+ if num > max {
+ max = num
+ }
+ }
return max
}Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 35,
the code sets max := int(math.Inf(-1)) which converts negative infinity to int
(implementation-defined) — replace this by first checking the slice is non-empty
and initializing max from the first element (e.g., max := arr[0]) before
looping, or alternatively initialize max to math.MinInt (math.MinInt64 or
math.MinInt depending on types) to avoid converting from math.Inf.
| // RemoveDuplicates returns a new slice with duplicate values removed, | ||
| // preserving the original order of elements. | ||
| func RemoveDuplicates(numbers []int) []int { | ||
| // TODO: Implement this function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove TODO comment.
The function is fully implemented—remove the placeholder TODO comment.
- // TODO: Implement this function
hs := map[int]bool{}📝 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.
| // TODO: Implement this function | |
| hs := map[int]bool{} |
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 47,
remove the leftover "// TODO: Implement this function" comment because the
function is already implemented; simply delete that comment line so the source
no longer contains a misleading TODO placeholder.
| // ReverseSlice returns a new slice with elements in reverse order. | ||
| func ReverseSlice(slice []int) []int { | ||
| result := []int{} | ||
| // TODO: Implement this function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove TODO comment.
The function is fully implemented—remove the placeholder TODO comment.
- // TODO: Implement this function
for i := len(slice) - 1; i >= 0 ; 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.
| // TODO: Implement this function | |
| for i := len(slice) - 1; i >= 0 ; i-- { |
🤖 Prompt for AI Agents
In challenge-19/submissions/iamsurajmandal/solution-template.go around line 63,
remove the leftover "// TODO: Implement this function" comment because the
function has been implemented; delete that single-line placeholder and ensure
the file is saved (run gofmt / go vet as needed) so no stray TODO remains.
Challenge 19 Solution
Submitted by: @iamsurajmandal
Challenge: Challenge 19
Description
This PR contains my solution for Challenge 19.
Changes
challenge-19/submissions/iamsurajmandal/solution-template.goTesting
Thank you for reviewing my submission! 🚀