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
72 changes: 72 additions & 0 deletions challenge-3/submissions/iamsurajmandal/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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,Employee{
ID: e.ID,
Name: e.Name,
Age: e.Age,
Salary: e.Salary,
})
}
Comment on lines +17 to +25
Copy link
Contributor

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.

Suggested change
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.


// RemoveEmployee removes an employee by ID from the manager's list.
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:]...)
}
}
}
Comment on lines +28 to +35
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

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.

Suggested change
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.


// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
sum := 0.000000
if (len(m.Employees) < 1) {
return sum
}
for _, item := range m.Employees {
sum += item.Salary
}
return sum / 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