-
-
Notifications
You must be signed in to change notification settings - Fork 704
Add solution for Challenge 3 by iamsurajmandal #719
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 3 by iamsurajmandal #719
Conversation
WalkthroughA Go solution file is added implementing an Employee and Manager system. The code defines two public types (Employee and Manager) with four Manager methods for employee lifecycle management (add, remove, find by ID) and salary calculations, plus a main function demonstrating basic usage. Changes
Sequence DiagramsequenceDiagram
participant Main
participant Manager
participant Employees
Main->>Manager: NewManager()
Main->>Manager: AddEmployee(Alice, salary: 50000)
Manager->>Employees: append
Main->>Manager: AddEmployee(Bob, salary: 60000)
Manager->>Employees: append
Main->>Manager: RemoveEmployee(1)
Manager->>Employees: iterate & remove
Main->>Manager: GetAverageSalary()
Manager->>Employees: iterate all
Manager-->>Main: 60000.0
Main->>Manager: FindEmployeeByID(2)
Manager->>Employees: search
Manager-->>Main: *Employee (Bob)
Main->>Main: Print results
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 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: 2
🧹 Nitpick comments (1)
challenge-3/submissions/iamsurajmandal/solution-template.go (1)
38-47: Consider minor style improvements.The logic is correct, but consider these idiomatic Go refinements:
- Line 39:
0.000000can be simplified to0.0- Line 40: Remove unnecessary parentheses around the condition
Apply this diff:
func (m *Manager) GetAverageSalary() float64 { - sum := 0.000000 - if (len(m.Employees) < 1) { + sum := 0.0 + if len(m.Employees) == 0 { return sum } for _, item := range m.Employees { sum += item.Salary } return sum / float64(len(m.Employees)) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-3/submissions/iamsurajmandal/solution-template.go(1 hunks)
🔇 Additional comments (3)
challenge-3/submissions/iamsurajmandal/solution-template.go (3)
5-14: LGTM!The struct definitions are clean and follow Go conventions with properly exported fields.
50-57: LGTM!The implementation correctly returns a pointer to the employee in the slice and properly handles the not-found case by returning nil.
60-72: LGTM!The main function effectively demonstrates all the Manager methods with clear output.
| func (m *Manager) AddEmployee(e Employee) { | ||
| // TODO: Implement this method | ||
| m.Employees = append(m.Employees,Employee{ | ||
| ID: e.ID, | ||
| Name: e.Name, | ||
| Age: e.Age, | ||
| Salary: e.Salary, | ||
| }) | ||
| } |
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.
🛠️ Refactor suggestion | 🟠 Major
Simplify: append the parameter directly.
The explicit field-by-field copy is unnecessary. Since e is already passed by value in Go, you can append it directly.
Apply this diff:
func (m *Manager) AddEmployee(e Employee) {
- // TODO: Implement this method
- m.Employees = append(m.Employees,Employee{
- ID: e.ID,
- Name: e.Name,
- Age: e.Age,
- Salary: e.Salary,
- })
+ m.Employees = append(m.Employees, e)
}📝 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 (m *Manager) AddEmployee(e Employee) { | |
| // TODO: Implement this method | |
| m.Employees = append(m.Employees,Employee{ | |
| ID: e.ID, | |
| Name: e.Name, | |
| Age: e.Age, | |
| Salary: e.Salary, | |
| }) | |
| } | |
| func (m *Manager) AddEmployee(e Employee) { | |
| m.Employees = append(m.Employees, e) | |
| } |
🤖 Prompt for AI Agents
In challenge-3/submissions/iamsurajmandal/solution-template.go around lines 17
to 25, the AddEmployee method unnecessarily constructs a new Employee literal
copying fields from the parameter; simply append the passed-in parameter
directly to m.Employees (i.e., replace the explicit struct literal with
append(m.Employees, e)) and remove the field-by-field copy.
| func (m *Manager) RemoveEmployee(id int) { | ||
| // TODO: Implement this method | ||
| for index, item := range m.Employees { | ||
| if (item.ID == id) { | ||
| m.Employees = append( m.Employees[:index],m.Employees[index + 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.
Add break after removal to prevent iteration over modified slice.
After removing an element at line 32, the loop continues iterating over the modified slice. This can lead to skipped elements or unexpected behavior. The standard practice is to break immediately after removal.
Apply this diff:
func (m *Manager) RemoveEmployee(id int) {
- // TODO: Implement this method
for index, item := range m.Employees {
- if (item.ID == id) {
+ if item.ID == id {
m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...)
+ break
}
}
}Note: Also removed unnecessary parentheses around the condition for Go idiomaticity.
📝 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 (m *Manager) RemoveEmployee(id int) { | |
| // TODO: Implement this method | |
| for index, item := range m.Employees { | |
| if (item.ID == id) { | |
| m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...) | |
| } | |
| } | |
| } | |
| func (m *Manager) RemoveEmployee(id int) { | |
| for index, item := range m.Employees { | |
| if item.ID == id { | |
| m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...) | |
| break | |
| } | |
| } | |
| } |
🤖 Prompt for AI Agents
In challenge-3/submissions/iamsurajmandal/solution-template.go around lines 28
to 35 the loop removes an employee from the slice but continues iterating over
the now-modified slice which can skip elements or cause unexpected behavior;
after performing the slice append to remove the element, immediately break out
of the loop to stop further iteration, and also remove the unnecessary
parentheses around the if condition to match Go idioms.
Challenge 3 Solution
Submitted by: @iamsurajmandal
Challenge: Challenge 3
Description
This PR contains my solution for Challenge 3.
Changes
challenge-3/submissions/iamsurajmandal/solution-template.goTesting
Thank you for reviewing my submission! 🚀