-
Notifications
You must be signed in to change notification settings - Fork 0
HW3 is completed #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,13 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw= | ||
| github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= | ||
| github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
| github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
| gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
| gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
| gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
| gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= | ||
| gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,58 @@ | ||
| package hw03frequencyanalysis | ||
|
|
||
| func Top10(_ string) []string { | ||
| // Place your code here. | ||
| return nil | ||
| import ( | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| type Word struct { | ||
| Value string | ||
| Count int | ||
| } | ||
|
|
||
| func Top10(str string) []string { | ||
| var words = strings.Fields(str) | ||
| var groupedWords = GroupWords(words) | ||
| sort.Slice(groupedWords, func(i, j int) bool { | ||
| if groupedWords[i].Count == groupedWords[j].Count { | ||
| return groupedWords[i].Value < groupedWords[j].Value | ||
| } | ||
|
|
||
| return groupedWords[i].Count > groupedWords[j].Count | ||
| }) | ||
|
|
||
| var result = make([]string, 0) | ||
| for k, v := range groupedWords { | ||
| if k > 9 { | ||
| return result | ||
| } | ||
| result = append(result, v.Value) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func GroupWords(words []string) []Word { | ||
| var result = make([]Word, 0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 Так как вы в момент определения не знаете финальный размер слайса, то лучше его не инициализировать, а просто определить: var result []WordДалее с помощью функции |
||
| for _, word := range words { | ||
| _, pos := ValueIsExists(word, result) | ||
| if pos == -1 { | ||
| result = append(result, Word{word, 0}) | ||
| pos = len(result) - 1 | ||
| } | ||
|
|
||
| result[pos].Count = result[pos].Count + 1 | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func ValueIsExists(word string, Words []Word) (Word, int) { | ||
| for i, value := range Words { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 Очень дорогая операция поиска в слайсе. Она по времени будет нам стоить O(N). Можно сделать эффективнее этот поиск введя дополнительный хешмап, для которого поиск будет осуществляться в среднем за O(1) |
||
| if value.Value == word { | ||
| return value, i | ||
| } | ||
| } | ||
|
|
||
| return Word{"", 0}, -1 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,11 +43,17 @@ var text = `Как видите, он спускается по лестни | |
| посидеть у огня и послушать какую-нибудь интересную сказку. | ||
| В этот вечер...` | ||
|
|
||
| var additionalTest = "a6 a5 a4 a3 a2 a1" | ||
|
|
||
| func TestTop10(t *testing.T) { | ||
| t.Run("no words in empty string", func(t *testing.T) { | ||
| require.Len(t, Top10(""), 0) | ||
| }) | ||
|
|
||
| t.Run("no words in string with only spaces", func(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 что-то случилось с вашим форматированием кода. рекомендую перед коммитом в репозиторий |
||
| require.Len(t, Top10(" "), 0) | ||
| }) | ||
|
|
||
| t.Run("positive test", func(t *testing.T) { | ||
| if taskWithAsteriskIsCompleted { | ||
| expected := []string{ | ||
|
|
@@ -79,4 +85,16 @@ func TestTop10(t *testing.T) { | |
| require.Equal(t, expected, Top10(text)) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("additional positive test", func(t *testing.T) { | ||
| expected := []string{ | ||
| "a1", | ||
| "a2", | ||
| "a3", | ||
| "a4", | ||
| "a5", | ||
| "a6", | ||
| } | ||
| require.Equal(t, expected, Top10(additionalTest)) | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👀
В данный момент времени вы знаете сколько вам нужно элементов, поэтому лучше инициализировать слайс определенной длины.