/
match.go
149 lines (135 loc) · 3.54 KB
/
match.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package model
import (
"encoding/csv"
"fmt"
"os"
"text/tabwriter"
)
type Match struct {
Company *Company
Student *Student
}
func NewMatch(c *Company, s *Student) *Match {
return &Match{
Company: c,
Student: s,
}
}
func (m Match) Equals(m2 Match) bool {
return m.Company.Equals(*m2.Company) && m.Student.Equals(*m2.Student)
}
type Matches []*Match
func (m Matches) Add(student *Student, company *Company) Matches {
if m.StudentHasMatch(student) {
panic(fmt.Sprintf("attempted to match already matched student: %v -> %v", student.Name, company.Name))
}
if !m.CompanyCanSupportMatch(company) {
panic(fmt.Sprintf("attempted to match company beyond matching limit: %v (%v) <- %v", company.NumberHiring, company.NumberHiring, student.Name))
}
return append(m, &Match{
Company: company,
Student: student,
})
}
// CompanyCanSupportMatch returns true if a company has room to match
func (m Matches) CompanyCanSupportMatch(company *Company) bool {
nMatches := 0
for _, match := range m {
if match.Company.Name == company.Name {
nMatches++
}
}
return nMatches < company.NumberHiring
}
// Equals returns true of two sets of matches are exactly equal to one-another.
func (m Matches) Equals(m2 Matches) bool {
if len(m) != len(m2) {
return false
}
for _, match := range m {
match2 := m2.FindByStudent(match.Student)
if match2 == nil {
return false
}
if !match.Equals(*match2) {
return false
}
}
return true
}
// FindByStudent finds the student's match in the list of matches, or nil if they haven't been
// matched yet
func (m Matches) FindByStudent(student *Student) *Match {
for _, match := range m {
if match.Student.Name == student.Name {
return match
}
}
return nil
}
// StudentHasMatch returns true if the given student has a match already assigned
func (m Matches) StudentHasMatch(student *Student) bool {
for _, match := range m {
if match.Student.Name == student.Name {
return true
}
}
return false
}
func (m Matches) WriteByCompany() {
fmt.Printf("by company\n")
byCompany := make(map[string][]*Match)
for _, match := range m {
if _, in := byCompany[match.Company.Name]; in {
byCompany[match.Company.Name] = append(byCompany[match.Company.Name], match)
} else {
byCompany[match.Company.Name] = []*Match{match}
}
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', tabwriter.DiscardEmptyColumns)
for companyName, matches := range byCompany {
str := fmt.Sprintf("%v \t%v ", companyName, matches[0].Company.NumberHiring)
for _, match := range matches {
str += fmt.Sprintf("\t%v ", match.Student.Name)
}
str += "\n"
fmt.Fprint(w, str)
}
w.Flush()
}
func (m Matches) WriteByStudent() {
fmt.Printf("by student\n")
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
for _, match := range m {
fmt.Fprintf(w, "%v\t%v\n", match.Student.Name, match.Company.Name)
}
w.Flush()
}
func (m Matches) WriteCSVByCompany() {
byCompany := make(map[string][]*Match)
for _, match := range m {
if _, in := byCompany[match.Company.Name]; in {
byCompany[match.Company.Name] = append(byCompany[match.Company.Name], match)
} else {
byCompany[match.Company.Name] = []*Match{match}
}
}
w := csv.NewWriter(os.Stdout)
for companyName, matches := range byCompany {
line := []string{companyName}
for _, match := range matches {
line = append(line, match.Student.Name)
}
w.Write(line)
}
w.Flush()
}
func (m Matches) WriteCSVByStudent() {
w := csv.NewWriter(os.Stdout)
for _, match := range m {
w.Write([]string{match.Student.Name, match.Company.Name})
}
w.Flush()
}