-
-
Notifications
You must be signed in to change notification settings - Fork 699
Add solution for Challenge 19 by kiramux #722
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
Conversation
WalkthroughAdds a new Go program Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Main as main()
participant FindMax as FindMax()
participant RemoveDup as RemoveDuplicates()
participant Reverse as ReverseSlice()
participant Filter as FilterEven()
Main->>FindMax: pass sample slice
FindMax-->>Main: max value
Main->>RemoveDup: pass sample slice
RemoveDup-->>Main: deduped slice
Main->>Reverse: pass sample slice
Reverse-->>Main: reversed slice
Main->>Filter: pass sample slice
Filter-->>Main: filtered evens
Note over Main: prints results
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 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: 0
🧹 Nitpick comments (2)
challenge-19/submissions/kiramux/solution-template.go (2)
44-56: Consider pre-allocating the result slice for better performance.While the logic is correct and the use of
map[int]struct{}is idiomatic, pre-allocating the result slice can reduce memory allocations duringappendoperations.Apply this diff to pre-allocate the result slice:
func RemoveDuplicates(numbers []int) []int { seen := make(map[int]struct{}, len(numbers)) - res := []int{} + res := make([]int, 0, len(numbers)) for _, v := range numbers { if _, ok := seen[v]; !ok { seen[v] = struct{}{} res = append(res, v) } } return res }
58-73: Consider building the reversed slice directly instead of copy-then-swap.The current implementation copies the slice and then reverses it in-place. You can achieve the same result more efficiently by building the reversed slice directly in a single pass.
Apply this diff to build the reversed slice directly:
func ReverseSlice(slice []int) []int { if len(slice) == 0 { return []int{} } result := make([]int, len(slice)) - copy(result, slice) - left := 0 - right := len(result) - 1 - for left < right { - result[left], result[right] = result[right], result[left] - left++ - right-- + for i, v := range slice { + result[len(slice)-1-i] = v } return result }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-19/submissions/kiramux/solution-template.go(1 hunks)
🔇 Additional comments (3)
challenge-19/submissions/kiramux/solution-template.go (3)
8-27: LGTM! Clear demonstration of all utility functions.The main function effectively demonstrates each utility function with appropriate test data and clear output labels.
29-42: Consider the ambiguity of returning 0 for empty slices.Returning
0for an empty slice is ambiguous since0could be a valid maximum value (e.g., in a slice of negative numbers). This makes it impossible to distinguish between "no elements" and "maximum is 0". Consider returning an error or using a tuple return(int, bool)to indicate validity.Example demonstrating the ambiguity:
FindMax([]int{}) // returns 0 - empty slice FindMax([]int{-5, 0}) // returns 0 - valid maximum
75-85: LGTM! Clean and correct even number filter.The logic is straightforward and correct. Pre-allocating the result slice here would be wasteful since the final size is unknown, and the current approach is appropriate.
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: 0
🧹 Nitpick comments (2)
challenge-19/submissions/kiramux/solution-template.go (2)
44-56: LGTM! Excellent use of empty struct for the seen map.The logic is correct and order-preserving. Consider pre-allocating the result slice capacity for a minor performance improvement:
- res := []int{} + res := make([]int, 0, len(numbers))
70-80: LGTM! Correct even number filtering.The modulo operation correctly identifies even numbers. Optionally, pre-allocate capacity for better performance:
- result := []int{} + result := make([]int, 0, len(numbers))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-19/submissions/kiramux/solution-template.go(1 hunks)
🔇 Additional comments (4)
challenge-19/submissions/kiramux/solution-template.go (4)
3-6: LGTM! Clean import usage.Both imports are necessary and properly used throughout the code.
8-27: LGTM! Well-structured demonstration.The main function provides clear demonstrations of all four utilities with labeled output.
29-42: LGTM! Correct implementation with proper edge case handling.The use of
math.MinIntas the initial value is the right approach, ensuring correct behavior even with all-negative slices.
58-68: LGTM! Clean reverse implementation.The logic correctly reverses the slice with proper pre-allocation and empty slice handling.
Challenge 19 Solution
Submitted by: @kiramux
Challenge: Challenge 19
Description
This PR contains my solution for Challenge 19.
Changes
challenge-19/submissions/kiramux/solution-template.goTesting
Thank you for reviewing my submission! 🚀