-
-
Notifications
You must be signed in to change notification settings - Fork 687
solution for challenge 3 #553
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| type Employee struct { | ||
| ID int | ||
| Name string | ||
| Age int | ||
| Salary float64 | ||
| } | ||
| type Manager struct { | ||
| Employees []Employee | ||
| } | ||
|
|
||
| func (m *Manager) GetEmployee() { | ||
|
|
||
| } | ||
|
|
||
| func (m *Manager) AddEmployee(em Employee) { | ||
| m.Employees = append(m.Employees, em) | ||
| } | ||
|
|
||
| func (m *Manager) RemoveEmployee(id int) { | ||
| var tempValue []Employee | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| if m.Employees[i].ID == id { | ||
| continue | ||
| } else { | ||
| tempValue = append(tempValue, m.Employees[i]) | ||
| } | ||
| } | ||
| m.Employees = tempValue | ||
| } | ||
|
|
||
| func (m *Manager) GetAverageSalary() float64 { | ||
| if len(m.Employees) == 0 { | ||
| return 0 | ||
| } | ||
| var averagesalary float64 | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| averagesalary += m.Employees[i].Salary | ||
| } | ||
|
|
||
| averagesalary = averagesalary / float64(len(m.Employees)) | ||
| return averagesalary | ||
| } | ||
|
|
||
| func (m *Manager) FindEmployeeByID(id int) *Employee { | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| if m.Employees[i].ID == id { | ||
| return &m.Employees[i] | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
Comment on lines
+48
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential dangling pointer after slice modifications. Returning a pointer to a slice element ( Option 1: Return by value instead of pointer -func (m *Manager) FindEmployeeByID(id int) *Employee {
- for i := 0; i < len(m.Employees); i++ {
- if m.Employees[i].ID == id {
- return &m.Employees[i]
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
+ for _, employee := range m.Employees {
+ if employee.ID == id {
+ return employee, true
}
}
- return nil
+ return Employee{}, false
}Then update the caller in main: - employee := manager.FindEmployeeByID(2)
- if employee != nil {
- fmt.Printf("Employee found: %+v\n", *employee)
+ employee, found := manager.FindEmployeeByID(2)
+ if found {
+ fmt.Printf("Employee found: %+v\n", employee)
}Option 2: Use a map instead of slice for O(1) lookups This would also eliminate the pointer safety issue and improve performance. 🤖 Prompt for AI Agents |
||
|
|
||
| func main() { | ||
| manager := Manager{} | ||
| manager.AddEmployee(Employee{ID: 1, Name: "Kushal", Age: 23, Salary: 4500}) | ||
| manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000}) | ||
| manager.RemoveEmployee(1) | ||
| averagesalary := manager.GetAverageSalary() | ||
| fmt.Println("The average salary is ", averagesalary) | ||
| employee := manager.FindEmployeeByID(2) | ||
| if employee != nil { | ||
| fmt.Printf("Employee found: %+v\n", *employee) | ||
| } | ||
| } | ||
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 or implement the GetEmployee stub.
This method is empty and has no return value. If it's not needed, remove it. Otherwise, implement the intended functionality.