Skip to content

Commit 19581b2

Browse files
authored
Add solution for Challenges by MiladJlz (#236)
* Add solution for Challenge 2 by MiladJlz * Add solution for Challenge 3 by MiladJlz
1 parent ad05589 commit 19581b2

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"slices"
8+
9+
)
10+
11+
func main() {
12+
// Read input from standard input
13+
scanner := bufio.NewScanner(os.Stdin)
14+
if scanner.Scan() {
15+
input := scanner.Text()
16+
17+
// Call the ReverseString function
18+
output := ReverseString(input)
19+
20+
// Print the result
21+
fmt.Println(output)
22+
}
23+
}
24+
25+
// ReverseString returns the reversed string of s.
26+
func ReverseString(s string) string {
27+
stringToBytes := []byte(s)
28+
slices.Reverse(stringToBytes)
29+
return string(stringToBytes)
30+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
)
7+
8+
type Employee struct {
9+
ID int
10+
Name string
11+
Age int
12+
Salary float64
13+
}
14+
15+
type Manager struct {
16+
Employees []Employee
17+
}
18+
19+
// AddEmployee adds a new employee to the manager's list.
20+
func (m *Manager) AddEmployee(e Employee) {
21+
m.Employees = append(m.Employees, e)
22+
}
23+
24+
// RemoveEmployee removes an employee by ID from the manager's list.
25+
func (m *Manager) RemoveEmployee(id int) {
26+
27+
for i, v := range m.Employees {
28+
if v.ID == id {
29+
m.Employees = slices.Delete(m.Employees, i, i+1)
30+
31+
}
32+
}
33+
}
34+
35+
// GetAverageSalary calculates the average salary of all employees.
36+
func (m *Manager) GetAverageSalary() float64 {
37+
if len(m.Employees) == 0 {
38+
return 0
39+
}
40+
sum := 0.0
41+
for _, v := range m.Employees {
42+
sum += v.Salary
43+
}
44+
return sum / float64((len(m.Employees)))
45+
}
46+
47+
// FindEmployeeByID finds and returns an employee by their ID.
48+
func (m *Manager) FindEmployeeByID(id int) *Employee {
49+
for _, v := range m.Employees {
50+
if v.ID == id {
51+
return &v
52+
}
53+
}
54+
return nil
55+
}
56+
57+
func main() {
58+
manager := Manager{}
59+
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})
60+
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
61+
manager.RemoveEmployee(1)
62+
averageSalary := manager.GetAverageSalary()
63+
employee := manager.FindEmployeeByID(2)
64+
65+
fmt.Printf("Average Salary: %f\n", averageSalary)
66+
if employee != nil {
67+
fmt.Printf("Employee found: %+v\n", *employee)
68+
}
69+
}

0 commit comments

Comments
 (0)