Go Function Examples: Mastering Basic Data Structures
This challenge will help you solidify your understanding of fundamental Go data structures and how to manipulate them. You will be tasked with creating functions that demonstrate common operations on slices and maps, which are essential building blocks in Go programming. Mastering these operations is crucial for efficient data handling in any Go application.
Problem Description
Your task is to create three Go functions, each demonstrating a different common operation:
SumIntSlice(numbers []int) int: This function should take a slice of integers ([]int) as input and return the sum of all elements in the slice.FindMaxInIntSlice(numbers []int) (int, error): This function should take a slice of integers ([]int) and return the largest integer in the slice. If the slice is empty, it should return0and an error indicating that the slice cannot be empty.CountWordOccurrences(text string) map[string]int: This function should take a string as input, parse it into words, and return a map where keys are the unique words and values are their respective counts. Words should be considered case-insensitive. Punctuation attached to words should be ignored.
Key Requirements:
- All functions must be implemented in Go.
- The function signatures must match the ones provided above.
- Error handling for
FindMaxInIntSlicemust be implemented correctly. - Word counting in
CountWordOccurrencesshould handle case-insensitivity and remove common punctuation (e.g., '.', ',', '!', '?').
Expected Behavior:
SumIntSliceshould correctly sum all elements, including negative numbers. An empty slice should result in a sum of 0.FindMaxInIntSliceshould return the maximum value for non-empty slices and an appropriate error for empty slices.CountWordOccurrencesshould produce a map reflecting the frequency of each word, treating "The" and "the" as the same word and excluding trailing punctuation.
Examples
Example 1: SumIntSlice
Input: numbers = []int{1, 2, 3, 4, 5}
Output: 15
Explanation: The sum of all elements in the slice is 1 + 2 + 3 + 4 + 5 = 15.
Example 2: SumIntSlice (Empty Slice)
Input: numbers = []int{}
Output: 0
Explanation: The sum of an empty slice is 0.
Example 3: FindMaxInIntSlice
Input: numbers = []int{10, 4, 8, 15, 6}
Output: 15, nil
Explanation: The largest number in the slice is 15. No error occurred.
Example 4: FindMaxInIntSlice (Empty Slice)
Input: numbers = []int{}
Output: 0, "slice cannot be empty"
Explanation: The slice is empty, so an error is returned along with a zero value for the integer.
Example 5: CountWordOccurrences
Input: text = "Hello world! This is a test. Hello again."
Output: map[string]int{"hello": 2, "world": 1, "this": 1, "is": 1, "a": 1, "test": 1, "again": 1}
Explanation: The text is processed, words are lowercased, and punctuation is removed. "Hello" appears twice, and all other words appear once.
Example 6: CountWordOccurrences (Case and Punctuation)
Input: text = "Go is fun. GO IS GREAT!"
Output: map[string]int{"go": 2, "is": 2, "fun": 1, "great": 1}
Explanation: Case differences and punctuation are handled. "Go" and "GO" are counted as the same word.
Constraints
- The input slice for
SumIntSliceandFindMaxInIntSlicecan contain any validintvalue, including negative numbers. - The input
textforCountWordOccurrenceswill be a valid UTF-8 string. - The maximum number of words in the input text is not explicitly limited, but your solution should be reasonably efficient.
- The
FindMaxInIntSlicefunction must return an error of typeerror.
Notes
- For
CountWordOccurrences, consider usingstrings.Fieldsto split the text into words andstrings.ToLowerfor case conversion. You might also need functions from thestringsandunicodepackages to handle punctuation effectively. - When checking for punctuation, focus on common sentence-ending punctuation and commas. You don't need to handle every possible Unicode punctuation character, but aim for robustness with the most frequent ones.
- Think about how you will handle multiple spaces between words or leading/trailing spaces in the input text.