add new solutions
This commit is contained in:
parent
ca24d0a56a
commit
0c73608ce5
36 changed files with 791 additions and 0 deletions
58
solutions/36/q3606/solution.go
Normal file
58
solutions/36/q3606/solution.go
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue