Skip to content

Commit

Permalink
chore: refactor valeria; support more tables (moscow PDF)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gornak40 committed Mar 3, 2024
1 parent 8b67630 commit 87a79de
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 118 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
| ⚙️ | move json config to ini | | | 🧑‍💻 |
| 👻 | list/commit problems | | 🦍 | 🤔 |
| 👻 | set good random group scores | | 🦍 | 🤔 |
| 👻 | generate hasher solution for `.a` | | 🦍 | 🤔 |
| 👻 | algolymp config manager | | | 🤔 |
| 👻 | download/upload package | | 🦍 | 🤔 |
| 👻 | import polygon problem | 🦍 | 🦍 | 🤔 |
Expand Down
8 changes: 7 additions & 1 deletion cmd/valeria/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"fmt"
"os"

"github.com/Gornak40/algolymp/config"
"github.com/Gornak40/algolymp/polygon"
"github.com/Gornak40/algolymp/polygon/valeria"
"github.com/akamensky/argparse"
"github.com/sirupsen/logrus"
)
Expand All @@ -25,8 +27,12 @@ func main() {

cfg := config.NewConfig()
pClient := polygon.NewPolygon(&cfg.Polygon)
val := valeria.NewValeria(pClient)

if err := pClient.InformaticsValuer(*pID, *verbose); err != nil {
table := valeria.UniversalTable{}
if err := val.InformaticsValuer(*pID, &table, *verbose); err != nil {
logrus.WithError(err).Fatal("failed get scoring")
}

fmt.Println(table.String()) //nolint:forbidigo // Basic functionality.
}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/Gornak40/algolymp
go 1.20

require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/akamensky/argparse v1.4.0 // indirect
github.com/PuerkitoBio/goquery v1.8.1
github.com/akamensky/argparse v1.4.0
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
)
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -28,7 +31,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
Expand All @@ -46,4 +48,5 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
113 changes: 0 additions & 113 deletions polygon/scoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package polygon

import (
"errors"
"fmt"
"strings"

"github.com/sirupsen/logrus"
)
Expand All @@ -18,117 +16,6 @@ var (
ErrBadTestsOrder = errors.New("bad tests order, fix in polygon required")
)

type Scoring struct {
score map[string]int
count map[string]int
first map[string]int
last map[string]int
dependencies map[string][]string
groups []string
}

func NewScoring(tests []TestAnswer, groups []GroupAnswer) (*Scoring, error) {
scorer := Scoring{
score: map[string]int{},
count: map[string]int{},
first: map[string]int{},
last: map[string]int{},
dependencies: map[string][]string{},
}
for _, test := range tests {
scorer.score[test.Group] += int(test.Points) // TODO: ensure ejudge doesn't support float points
scorer.count[test.Group]++
if val, ok := scorer.first[test.Group]; !ok || val > test.Index {
scorer.first[test.Group] = test.Index
}
if val, ok := scorer.last[test.Group]; !ok || val < test.Index {
scorer.last[test.Group] = test.Index
}
}
for _, group := range groups {
if group.PointsPolicy != "COMPLETE_GROUP" {
return nil, ErrNoTestScore
}
if scorer.last[group.Name]-scorer.first[group.Name]+1 != scorer.count[group.Name] {
return nil, ErrBadTestsOrder
}
scorer.dependencies[group.Name] = group.Dependencies
scorer.groups = append(scorer.groups, group.Name)
}

return &scorer, nil
}

func (s *Scoring) buildValuer() string {
res := []string{}
for _, g := range s.groups {
cur := fmt.Sprintf("group %s {\n\ttests %d-%d;\n\tscore %d;\n",
g, s.first[g], s.last[g], s.score[g])
if len(s.dependencies[g]) != 0 {
cur += fmt.Sprintf("\trequires %s;\n", strings.Join(s.dependencies[g], ","))
}
cur += "}\n"
res = append(res, cur)
}

return strings.Join(res, "\n")
}

func (s *Scoring) buildScoring() string {
ans := []string{
"\\begin{center}",
"\\begin{tabular}{|c|c|c|c|}",
"\\hline",
"\\textbf{Подзадача} &",
"\\textbf{Баллы} &",
"\\textbf{Дополнительные ограничения} &",
"\\textbf{Необходимые подзадачи}",
"\\\\ \\hline",
}
for i, group := range s.groups {
var info string
if i == 0 {
info = "тесты из условия"
} else if i == len(s.groups)-1 {
info = "---"
}
ans = append(ans, fmt.Sprintf("%s & %d & %s & %s \\\\ \\hline",
group, s.score[group], info, strings.Join(s.dependencies[group], ", ")))
}
ans = append(ans, "\\end{tabular}", "\\end{center}")

return strings.Join(ans, "\n")
}

func (p *Polygon) InformaticsValuer(pID int, verbose bool) error {
groups, err := p.GetGroups(pID)
if err != nil {
return err
}
tests, err := p.GetTests(pID)
if err != nil {
return err
}

scorer, err := NewScoring(tests, groups)
if err != nil {
return err
}
valuer := scorer.buildValuer()
if verbose {
logrus.Info("valuer.cfg\n" + valuer)
}
fr := NewFileRequest(pID, TypeResource, "valuer.cfg", valuer)
if err := p.SaveFile(fr); err != nil {
return err
}

scoring := scorer.buildScoring()
fmt.Println(scoring) //nolint:forbidigo // Basic functionality.

return nil
}

func (p *Polygon) IncrementalScoring(pID int, samples bool) error {
if err := p.EnablePoints(pID); err != nil {
return err
Expand Down
52 changes: 52 additions & 0 deletions polygon/valeria/universal_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package valeria

import (
"fmt"
"strings"
)

// Works both in HTML and PDF render.
type UniversalTable struct {
groups []string
}

var _ TexTable = &UniversalTable{}

func (t *UniversalTable) addGroupRow(info groupInfo, comment string) {
row := fmt.Sprintf("%s & %d & %s & %s \\\\ \\hline",
info.group,
info.score,
comment,
strings.Join(info.dependencies, ", "),
)
t.groups = append(t.groups, row)
}

func (t *UniversalTable) addGroup0(info groupInfo) {
t.addGroupRow(info, "тесты из условия")
}

func (t *UniversalTable) addGroup(info groupInfo) {
t.addGroupRow(info, "")
}

func (t *UniversalTable) addLastGroup(info groupInfo) {
t.addGroupRow(info, "---")
}

func (t *UniversalTable) String() string {
table := []string{
"\\begin{center}",
"\\begin{tabular}{|c|c|c|c|}",
"\\hline",
"\\textbf{Подзадача} &",
"\\textbf{Баллы} &",
"\\textbf{Дополнительные ограничения} &",
"\\textbf{Необходимые подзадачи}",
"\\\\ \\hline",
}
table = append(table, t.groups...)
table = append(table, "\\end{tabular}", "\\end{center}")

return strings.Join(table, "\n")
}
Loading

0 comments on commit 87a79de

Please sign in to comment.