Skip to content

Commit d25d7d6

Browse files
authored
Challenge solution by skx (#57)
* Added 2 * Added 1 * Added 17
1 parent 65e8c56 commit d25d7d6

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-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+
24+
return a+b
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"regexp"
7+
)
8+
9+
func main() {
10+
// Get input from the user
11+
var input string
12+
fmt.Print("Enter a string to check if it's a palindrome: ")
13+
fmt.Scanln(&input)
14+
15+
// Call the IsPalindrome function and print the result
16+
result := IsPalindrome(input)
17+
if result {
18+
fmt.Println("The string is a palindrome.")
19+
} else {
20+
fmt.Println("The string is not a palindrome.")
21+
}
22+
}
23+
24+
// IsPalindrome checks if a string is a palindrome.
25+
// A palindrome reads the same backward as forward, ignoring case, spaces, and punctuation.
26+
func IsPalindrome(s string) bool {
27+
28+
reg, _ := regexp.Compile("[^a-zA-Z0-9]+")
29+
s = reg.ReplaceAllString(s, "")
30+
s = strings.ToLower(s)
31+
32+
for i := 0; i < len(s) / 2; i++ {
33+
if s[i] != s[len(s) - i - 1] {
34+
return false
35+
}
36+
}
37+
38+
return true
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
runes := []rune(s)
27+
builder := strings.Builder{}
28+
29+
for i := len(runes) - 1; i >= 0; i-- {
30+
builder.WriteRune(runes[i])
31+
}
32+
33+
return builder.String()
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Package challenge6 contains the solution for Challenge 6.
2+
package challenge6
3+
4+
import (
5+
// Add any necessary imports here
6+
"strings"
7+
8+
)
9+
10+
// CountWordFrequency takes a string containing multiple words and returns
11+
// a map where each key is a word and the value is the number of times that
12+
// word appears in the string. The comparison is case-insensitive.
13+
//
14+
// Words are defined as sequences of letters and digits.
15+
// All words are converted to lowercase before counting.
16+
// All punctuation, spaces, and other non-alphanumeric characters are ignored.
17+
//
18+
// For example:
19+
// Input: "The quick brown fox jumps over the lazy dog."
20+
// Output: map[string]int{"the": 2, "quick": 1, "brown": 1, "fox": 1, "jumps": 1, "over": 1, "lazy": 1, "dog": 1}
21+
func CountWordFrequency(text string) map[string]int {
22+
// Your implementation here
23+
m := make(map[string]int)
24+
25+
text = strings.ToLower(text)
26+
27+
text = strings.ReplaceAll(text, "\n", " ")
28+
text = strings.ReplaceAll(text, ".", " ")
29+
text = strings.ReplaceAll(text, ",", " ")
30+
text = strings.ReplaceAll(text, "'", "")
31+
text = strings.ReplaceAll(text, "!", " ")
32+
text = strings.ReplaceAll(text, "-", " ")
33+
text = strings.ReplaceAll(text, "?", " ")
34+
35+
for _, x := range(strings.Fields(text)) {
36+
if len(x) > 0{
37+
m[x] ++
38+
}
39+
}
40+
return m
41+
}

0 commit comments

Comments
 (0)