Skip to content

Commit

Permalink
HW10 is completed
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Pogodaev <[email protected]>
  • Loading branch information
Pavel Pogodaev committed Nov 18, 2024
1 parent 411b524 commit 508f18f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 19 deletions.
Empty file removed hw10_program_optimization/.sync
Empty file.
3 changes: 3 additions & 0 deletions hw10_program_optimization/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.22

require github.com/stretchr/testify v1.7.0

require github.com/mailru/easyjson v0.7.7

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
4 changes: 4 additions & 0 deletions hw10_program_optimization/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
33 changes: 14 additions & 19 deletions hw10_program_optimization/stats.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package hw10programoptimization

import (
"encoding/json"
"bufio"
"fmt"
"io"
"regexp"
"strings"

"github.com/mailru/easyjson"

Check failure on line 9 in hw10_program_optimization/stats.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/mailru/easyjson' is not allowed from list 'Main' (depguard)
)

type User struct {
Expand All @@ -31,35 +32,29 @@ func GetDomainStat(r io.Reader, domain string) (DomainStat, error) {
type users [100_000]User

func getUsers(r io.Reader) (result users, err error) {
content, err := io.ReadAll(r)
if err != nil {
return
}
scanner := bufio.NewScanner(r)
var i int

lines := strings.Split(string(content), "\n")
for i, line := range lines {
for scanner.Scan() {
var user User
if err = json.Unmarshal([]byte(line), &user); err != nil {
if err = easyjson.Unmarshal(scanner.Bytes(), &user); err != nil {
return
}

result[i] = user
i++
}
return

return result, scanner.Err()
}

func countDomains(u users, domain string) (DomainStat, error) {
result := make(DomainStat)
domain = strings.ToLower(domain)

for _, user := range u {
matched, err := regexp.Match("\\."+domain, []byte(user.Email))
if err != nil {
return nil, err
}

if matched {
num := result[strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])]
num++
result[strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])] = num
if strings.Contains(user.Email, "."+domain) {
result[strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])]++
}
}
return result, nil
Expand Down
127 changes: 127 additions & 0 deletions hw10_program_optimization/stats_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 508f18f

Please sign in to comment.