Skip to content

Commit baf9fc7

Browse files
Add solution for Challenges by ashwinipatankar (#52)
* Add solution for Challenge 1 by ashwinipatankar * Add solution for Challenge 2 by ashwinipatankar * Add solution for Challenge 3 by ashwinipatankar * Add solution for Challenge 6 by ashwinipatankar
1 parent 508834d commit baf9fc7

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
var a, b int
9+
// Read two integers from standard input
10+
_, err := fmt.Scanf("%d, %d", &a, &b)
11+
if err != nil {
12+
fmt.Println("Error reading input:", err)
13+
return
14+
}
15+
16+
// Call the Sum function and print the result
17+
result := Sum(a, b)
18+
fmt.Println(result)
19+
}
20+
21+
// Sum returns the sum of a and b.
22+
func Sum(a int, b int) int {
23+
// TODO: Implement the function
24+
return a + b
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
func main() {
11+
// Read input from standard input
12+
scanner := bufio.NewScanner(os.Stdin)
13+
if scanner.Scan() {
14+
input := scanner.Text()
15+
16+
// Call the ReverseString function
17+
output := ReverseString(input)
18+
19+
// Print the result
20+
fmt.Println(output)
21+
}
22+
}
23+
24+
// ReverseString returns the reversed string of s.
25+
func ReverseString(s string) string {
26+
r := ""
27+
splittedString := strings.Split(s, "")
28+
29+
le := len(splittedString) - 1
30+
for _ = range splittedString {
31+
r = r + splittedString[le]
32+
le--
33+
}
34+
35+
return r
36+
37+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Employee struct {
8+
ID int
9+
Name string
10+
Age int
11+
Salary float64
12+
}
13+
14+
type Manager struct {
15+
Employees []Employee
16+
}
17+
18+
// AddEmployee adds a new employee to the manager's list.
19+
func (m *Manager) AddEmployee(e Employee) {
20+
m.Employees = append(m.Employees, e)
21+
}
22+
23+
// RemoveEmployee removes an employee by ID from the manager's list.
24+
func (m *Manager) RemoveEmployee(id int) {
25+
if id < 1 {
26+
return
27+
}
28+
var indexToDelete int
29+
var found bool
30+
for i := range m.Employees {
31+
if m.Employees[i].ID == id {
32+
indexToDelete = i
33+
found = true
34+
break
35+
}
36+
}
37+
38+
if !found {
39+
return
40+
}
41+
42+
m.Employees = append(m.Employees[:indexToDelete], m.Employees[indexToDelete+1:]...)
43+
}
44+
45+
// GetAverageSalary calculates the average salary of all employees.
46+
func (m *Manager) GetAverageSalary() float64 {
47+
var avgSalary float64
48+
49+
if len(m.Employees) < 1 {
50+
return avgSalary
51+
}
52+
53+
for i := range m.Employees {
54+
avgSalary += m.Employees[i].Salary
55+
}
56+
57+
58+
return avgSalary/float64(len(m.Employees))
59+
}
60+
61+
// FindEmployeeByID finds and returns an employee by their ID.
62+
func (m *Manager) FindEmployeeByID(id int) *Employee {
63+
for i := range m.Employees {
64+
if m.Employees[i].ID == id {
65+
return &m.Employees[i]
66+
}
67+
}
68+
69+
return nil
70+
}
71+
72+
func main() {
73+
manager := Manager{}
74+
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})
75+
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
76+
manager.RemoveEmployee(1)
77+
averageSalary := manager.GetAverageSalary()
78+
employee := manager.FindEmployeeByID(2)
79+
80+
fmt.Printf("Average Salary: %f\n", averageSalary)
81+
if employee != nil {
82+
fmt.Printf("Employee found: %+v\n", *employee)
83+
}
84+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Package challenge6 contains the solution for Challenge 6.
2+
package challenge6
3+
4+
import (
5+
"strings"
6+
)
7+
8+
// CountWordFrequency takes a string containing multiple words and returns
9+
// a map where each key is a word and the value is the number of times that
10+
// word appears in the string. The comparison is case-insensitive.
11+
//
12+
// Words are defined as sequences of letters and digits.
13+
// All words are converted to lowercase before counting.
14+
// All punctuation, spaces, and other non-alphanumeric characters are ignored.
15+
//
16+
// For example:
17+
// Input: "The quick brown fox jumps over the lazy dog."
18+
// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1}
19+
func CountWordFrequency(text string) map[string]int {
20+
text = strings.ReplaceAll(text, "'", "")
21+
text = strings.ReplaceAll(text, "-", " ")
22+
cleaned := strings.ToLower(strings.Trim(text, ".,!?;:'"))
23+
24+
words := strings.Fields(cleaned)
25+
26+
wordCount := make(map[string]int)
27+
28+
for _, word := range words {
29+
word = strings.Trim(word, ".,!?;:'")
30+
if word != "" {
31+
wordCount[word]++
32+
}
33+
}
34+
35+
return wordCount
36+
}

0 commit comments

Comments
 (0)