Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions challenge-3/submissions/clgp-aint-cool/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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) {
m.Employees = append(m.Employees,e)
}

// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
for i, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
}
}
}
Comment on lines +22 to +28
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Modifying slice while iterating can cause unexpected behavior.

After removing an element with append(m.Employees[:i], m.Employees[i+1:]...), the slice is shortened but the range loop continues. This can skip elements or cause index issues. Since employee IDs should be unique, add a return after removal to exit immediately.

🐛 Proposed fix
 func (m *Manager) RemoveEmployee(id int) {
 	for i, e := range m.Employees {
 		if e.ID == id {
 			m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
+			return
 		}
 	}
 }
📝 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.

Suggested change
func (m *Manager) RemoveEmployee(id int) {
for i, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
}
}
}
func (m *Manager) RemoveEmployee(id int) {
for i, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
return
}
}
}


// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}
var totalSalary float64 = 0

for i:= range m.Employees{
totalSalary += m.Employees[i].Salary
}

return totalSalary / float64(len(m.Employees))
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i:=range m.Employees{
if m.Employees[i].ID == id {
return &m.Employees[i]
}
}
return nil
}

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)
}
}
Loading