-
-
Notifications
You must be signed in to change notification settings - Fork 911
Add solution for Challenge 3 by yudha-Dlesmana #889
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
base: main
Are you sure you want to change the base?
Add solution for Challenge 3 by yudha-Dlesmana #889
Conversation
WalkthroughA new Go solution file adds Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 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: 1
🧹 Nitpick comments (1)
challenge-3/submissions/yudha-Dlesmana/solution-template.go (1)
30-41: Redundant type conversion on line 40.Since
totalis already declared asfloat64(line 35), the conversionfloat64(total)is unnecessary.Apply this diff:
- return float64(total) / float64(len(m.Employees)) + return total / float64(len(m.Employees))
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-3/submissions/yudha-Dlesmana/solution-template.go(1 hunks)
🔇 Additional comments (6)
challenge-3/submissions/yudha-Dlesmana/solution-template.go (6)
1-3: LGTM!The package declaration and import are correct for a standalone Go executable.
5-10: LGTM!The
Employeestruct is well-defined with appropriate field types.
12-14: LGTM!The
Managerstruct appropriately uses a slice to manage the employee collection.
16-19: LGTM!The
AddEmployeemethod correctly uses a pointer receiver and appends the employee to the slice.
43-51: Verify pointer stability after slice modifications.The method returns a pointer to an element in the slice. If
RemoveEmployeeis called afterward, the underlying slice may be reallocated, potentially invalidating the pointer or causing it to point to different data. For this challenge, the implementation is acceptable, but in production code, consider returning a copy or documenting this behavior.
53-65: LGTM!The
mainfunction effectively demonstrates all theManagermethods with clear, working examples.
| func (m *Manager) RemoveEmployee(id int) { | ||
| for i, emp := range m.Employees { | ||
| if emp.ID == id { | ||
| m.Employees = append(m.Employees[:i], m.Employees[i+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.
Missing return after removal causes iteration bugs.
The function continues iterating after modifying the slice, which can cause the next element to be skipped. After removing an employee, you should return immediately.
Apply this diff to fix the issue:
func (m *Manager) RemoveEmployee(id int) {
for i, emp := range m.Employees {
if emp.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
+ return
}
}
}Additionally, consider returning a boolean to indicate whether removal was successful:
func (m *Manager) RemoveEmployee(id int) bool {
for i, emp := range m.Employees {
if emp.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
return true
}
}
return false
}🤖 Prompt for AI Agents
In challenge-3/submissions/yudha-Dlesmana/solution-template.go around lines 22
to 28, the RemoveEmployee method keeps iterating after modifying the slice which
can skip elements; modify the function to return immediately after removing the
matching employee (add a return right after the append), and optionally change
the signature to return a bool and return true on successful removal and false
if not found (i.e., update signature, return true after the append, and return
false at end).
Challenge 3 Solution
Submitted by: @yudha-Dlesmana
Challenge: Challenge 3
Description
This PR contains my solution for Challenge 3.
Changes
challenge-3/submissions/yudha-Dlesmana/solution-template.goTesting
Thank you for reviewing my submission! 🚀