59 lines
972 B
Go
59 lines
972 B
Go
package helper
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func check(e error) {
|
|
if e != nil {
|
|
panic(e)
|
|
}
|
|
}
|
|
|
|
func RemoveError[T any](value T, e error) T {
|
|
check(e)
|
|
return value
|
|
}
|
|
|
|
func ReadTextFile(filePath string) []string {
|
|
file, err := os.ReadFile(filePath)
|
|
check(err)
|
|
return strings.Split(string(file), "\r\n")
|
|
}
|
|
|
|
func FindRepeatedItems[T int | int64](itemgroup1, itemgroup2 []T) []T {
|
|
elementsCompartment1 := map[T]bool{}
|
|
repeatedElemnts := map[T]bool{}
|
|
for _, item := range itemgroup1 {
|
|
elementsCompartment1[item] = true
|
|
}
|
|
for _, item := range itemgroup2 {
|
|
if elementsCompartment1[item] {
|
|
repeatedElemnts[item] = true
|
|
}
|
|
}
|
|
v := make([]T, 0, len(repeatedElemnts))
|
|
for key, _ := range repeatedElemnts {
|
|
v = append(v, key)
|
|
}
|
|
|
|
return v
|
|
}
|
|
|
|
func FindMax[T int | int64](slice []T) (m T) {
|
|
for i, e := range slice {
|
|
if i == 0 || e > m {
|
|
m = e
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func Sum[T int | int64](slice []T) (s T) {
|
|
for _, e := range slice {
|
|
s += e
|
|
}
|
|
return
|
|
}
|