-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_declaration_forms.go
77 lines (63 loc) · 1.69 KB
/
custom_declaration_forms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"sort"
"strings"
)
type customsForm struct {
anyYes []rune
allYes []rune
peopleAnswers []string
}
func (form *customsForm) String() string {
return fmt.Sprintf("form: peoples' answers: %v, anyYes: %v, allYes: %v", form.peopleAnswers, form.anyYes, form.allYes)
}
func (form *customsForm) AnyoneAnsweredYes() []rune {
runes := make([]rune, len(form.anyYes))
for i, value := range form.anyYes {
runes[i] = rune(value)
}
sort.SliceStable(runes, func(i, j int) bool {
return runes[i] < runes[j]
})
return runes
}
func (form *customsForm) EveryoneAnsweredYes() []rune {
runes := make([]rune, len(form.allYes))
for i, value := range form.allYes {
runes[i] = rune(value)
}
sort.SliceStable(runes, func(i, j int) bool {
return runes[i] < runes[j]
})
return runes
}
func makeCustomsForm(input string) customsForm {
anyFoundTracker := make(map[rune]bool)
allFoundTracker := make(map[rune]int)
var anyYes, allYes []rune
newForm := customsForm{anyYes: anyYes, allYes: allYes}
numberOfPeopleInGroup := len(strings.Fields(input))
for _, peoplesAnswers := range strings.Fields(input) {
for _, letter := range peoplesAnswers {
anyFoundTracker[letter] = true
allFoundTracker[letter]++
}
newForm.peopleAnswers = append(newForm.peopleAnswers, peoplesAnswers)
}
for key, value := range allFoundTracker {
if value == numberOfPeopleInGroup {
newForm.allYes = append(newForm.allYes, rune(key))
}
}
for key := range anyFoundTracker {
newForm.anyYes = append(newForm.anyYes, key)
}
return newForm
}
func getForms(records chan string, forms chan customsForm) {
for record := range records {
forms <- makeCustomsForm(record)
}
close(forms)
}