From 9a1c1a3d88ab7245c0edc4d166285bd540cf1a48 Mon Sep 17 00:00:00 2001 From: "S.Kulish" Date: Sun, 22 May 2022 13:02:52 +0200 Subject: [PATCH] HW3 is completed --- hw03_frequency_analysis/.sync | 0 hw03_frequency_analysis/go.mod | 1 - hw03_frequency_analysis/go.sum | 7 ---- hw03_frequency_analysis/top.go | 58 +++++++++++++++++++++++++++-- hw03_frequency_analysis/top_test.go | 18 +++++++++ 5 files changed, 73 insertions(+), 11 deletions(-) delete mode 100644 hw03_frequency_analysis/.sync diff --git a/hw03_frequency_analysis/.sync b/hw03_frequency_analysis/.sync deleted file mode 100644 index e69de29..0000000 diff --git a/hw03_frequency_analysis/go.mod b/hw03_frequency_analysis/go.mod index 5f002a9..dec5b57 100644 --- a/hw03_frequency_analysis/go.mod +++ b/hw03_frequency_analysis/go.mod @@ -5,6 +5,5 @@ go 1.16 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.7.0 - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/hw03_frequency_analysis/go.sum b/hw03_frequency_analysis/go.sum index 62f8b7f..c221f64 100644 --- a/hw03_frequency_analysis/go.sum +++ b/hw03_frequency_analysis/go.sum @@ -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= diff --git a/hw03_frequency_analysis/top.go b/hw03_frequency_analysis/top.go index aff6568..0a751ce 100644 --- a/hw03_frequency_analysis/top.go +++ b/hw03_frequency_analysis/top.go @@ -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) + 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 { + if value.Value == word { + return value, i + } + } + + return Word{"", 0}, -1 } diff --git a/hw03_frequency_analysis/top_test.go b/hw03_frequency_analysis/top_test.go index e28c5cb..18b29be 100644 --- a/hw03_frequency_analysis/top_test.go +++ b/hw03_frequency_analysis/top_test.go @@ -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) { + 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)) + }) }