add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent ca24d0a56a
commit 0c73608ce5
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
36 changed files with 791 additions and 0 deletions

View file

@ -0,0 +1,58 @@
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