-
-
Notifications
You must be signed in to change notification settings - Fork 688
Add solution for Challenge 1 #646
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 3 commits
ddc5964
827008c
014571d
0b356cc
1d016e6
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,25 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| func main() { | ||
| var a, b int | ||
| // Read two integers from standard input | ||
| _, err := fmt.Scanf("%d, %d", &a, &b) | ||
| if err != nil { | ||
| fmt.Println("Error reading input:", err) | ||
| return | ||
| } | ||
|
|
||
| // Call the Sum function and print the result | ||
| result := Sum(a, b) | ||
| fmt.Println(result) | ||
| } | ||
|
|
||
| // Sum returns the sum of a and b. | ||
| func Sum(a int, b int) int { | ||
| // TODO: Implement the function | ||
| return a+b | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Read input from standard input | ||
| scanner := bufio.NewScanner(os.Stdin) | ||
| if scanner.Scan() { | ||
| input := scanner.Text() | ||
|
|
||
| // Call the ReverseString function | ||
| output := ReverseString(input) | ||
|
|
||
| // Print the result | ||
| fmt.Println(output) | ||
| } | ||
| } | ||
|
|
||
| // ReverseString returns the reversed string of s. | ||
| func ReverseString(s string) (result string) { | ||
| for _, c := range s{ | ||
| result = string(c) + result | ||
| } | ||
| return result | ||
| } |
| 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 | ||
| } | ||
|
|
||
| // 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) { | ||
| var removeValue []Employee | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| if m.Employees[i].ID == id { | ||
| continue | ||
| } else { | ||
| removeValue = append(removeValue, m.Employees[i]) | ||
| } | ||
| m.Employees = removeValue | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+32
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: assignment inside loop causes incorrect removal. Line 30 assigns Apply this diff to move the assignment outside the loop: func (m *Manager) RemoveEmployee(id int) {
var removeValue []Employee
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
continue
- } else {
- removeValue = append(removeValue, m.Employees[i])
}
- m.Employees = removeValue
+ removeValue = append(removeValue, m.Employees[i])
}
+ m.Employees = removeValue
}🤖 Prompt for AI Agents |
||
|
|
||
| // GetAverageSalary calculates the average salary of all employees. | ||
| func (m *Manager) GetAverageSalary() float64 { | ||
| if len(m.Employees) == 0 { | ||
| return 0 | ||
| } | ||
| var aveSal float64 | ||
| for i := 0; i < len(m.Employees); i++ { | ||
| aveSal += m.Employees[i].Salary | ||
| } | ||
| return aveSal / float64(len(m.Employees)) | ||
| } | ||
|
|
||
| // FindEmployeeByID finds and returns an employee by their ID. | ||
| 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 | ||
| } | ||
|
|
||
| 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 the TODO comment or the implementation.
The TODO comment indicates the function needs to be implemented, but the implementation already exists. This creates confusion about whether the function is complete.
Additionally, consider using the more idiomatic Go function signature style:
🤖 Prompt for AI Agents