-
-
Notifications
You must be signed in to change notification settings - Fork 711
Add solution for Challenge 3 by baindraraj #613
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
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,73 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Employee struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ID int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Age int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Salary float64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type Manager struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Employees []Employee | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // AddEmployee adds a new employee to the manager's list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) AddEmployee(e Employee) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| m.Employees = append(m.Employees, e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // RemoveEmployee removes an employee by ID from the manager's list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) RemoveEmployee(id int) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for indx, e := range m.Employees { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if e.ID == id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| m.Employees = append(m.Employees[:indx], m.Employees[indx+1:]...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+31
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. Remove TODO comment. The method is implemented correctly, but the TODO comment on Line 24 should be removed. - // TODO: Implement this method
for indx, e := range m.Employees {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // GetAverageSalary calculates the average salary of all employees. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) GetAverageSalary() float64 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(m.Employees) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var total float64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cnt := 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, e := range m.Employees { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total += e.Salary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cnt++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return total/ float64(cnt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+48
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. Remove TODO comment and simplify logic. The method works correctly but has a few minor issues:
- // TODO: Implement this method
if len(m.Employees) == 0 {
return 0
}
var total float64
- cnt := 0
for _, e := range m.Employees {
total += e.Salary
- cnt++
}
- return total/ float64(cnt)
+ return total / float64(len(m.Employees))📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // FindEmployeeByID finds and returns an employee by their ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (m *Manager) FindEmployeeByID(id int) *Employee { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Implement this method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, e := range m.Employees { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if e.ID == id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+59
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. Critical bug: returning address of loop variable. Line 55 returns - // TODO: Implement this method
- for _, e := range m.Employees {
+ for i := range m.Employees {
+ e := &m.Employees[i]
if e.ID == id {
- return &e
+ return e
}
}
return nil🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func main() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager := Manager{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager.RemoveEmployee(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| averageSalary := manager.GetAverageSalary() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| employee := manager.FindEmployeeByID(2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Printf("Average Salary: %f\n", averageSalary) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 TODO comment.
The method is implemented correctly, but the TODO comment on Line 18 should be removed.
- // TODO: Implement this method m.Employees = append(m.Employees, e)📝 Committable suggestion
🤖 Prompt for AI Agents