58 lines
1 KiB
Go
58 lines
1 KiB
Go
package q3606
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func isValidCode(code string) bool {
|
|
for i := range len(code) {
|
|
c := code[i]
|
|
if (c < 'a' || c > 'z') &&
|
|
(c < 'A' || c > 'Z') &&
|
|
(c < '0' || c > '9') &&
|
|
c != '_' {
|
|
return false
|
|
}
|
|
}
|
|
return len(code) > 0
|
|
}
|
|
|
|
func businessToI(b string) int8 {
|
|
switch b {
|
|
case "electronics":
|
|
return 1
|
|
case "grocery":
|
|
return 2
|
|
case "pharmacy":
|
|
return 3
|
|
case "restaurant":
|
|
return 4
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func validateCoupons(code []string, businessLine []string, isActive []bool) []string {
|
|
validCodes := []int{}
|
|
for i := range code {
|
|
if isValidCode(code[i]) && businessToI(businessLine[i]) > 0 && isActive[i] {
|
|
validCodes = append(validCodes, i)
|
|
}
|
|
}
|
|
|
|
slices.SortFunc(validCodes, func(a, b int) int {
|
|
ba, bb := businessToI(businessLine[a]), businessToI(businessLine[b])
|
|
if ba != bb {
|
|
return int(ba - bb)
|
|
}
|
|
return strings.Compare(code[a], code[b])
|
|
})
|
|
|
|
ret := make([]string, 0, len(validCodes))
|
|
for _, i := range validCodes {
|
|
ret = append(ret, code[i])
|
|
}
|
|
return ret
|
|
}
|
|
|
|
var _ = validateCoupons
|