Skip to content

Commit

Permalink
HW03 main is complited
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas authored and DimVlas committed Apr 14, 2024
1 parent 14c3130 commit 17b5da1
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 4 deletions.
Empty file removed hw03_frequency_analysis/.sync
Empty file.
2 changes: 1 addition & 1 deletion hw03_frequency_analysis/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/fixme_my_friend/hw03_frequency_analysis
module github.com/DimVlas/otus_hw/hw03_frequency_analysis

go 1.19

Expand Down
77 changes: 74 additions & 3 deletions hw03_frequency_analysis/top.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,77 @@
package hw03frequencyanalysis

func Top10(_ string) []string {
// Place your code here.
return nil
import (
"fmt"
"sort"
"strings"
)

// Разбивает текст на слова, возвращает срез слов.
func GetWords(text string) []string {
if len(text) < 1 {
return []string{}
}

return strings.Fields(text)
}

// Считает частоту элементов в строковом срезе.
// Возвращает map, где key элемент среза,
// а value сколько раз встречался элемент.
func CountOfElements(s []string) map[string]int {
m := make(map[string]int)

for _, w := range s {
if cnt, ok := m[w]; ok {
cnt++
m[w] = cnt
} else {
m[w] = 1
}
}
return m
}

type wordWidth struct {
Word string
Width int
}

func (w wordWidth) GetKey() string {
return fmt.Sprintf("%04d%s", w.Width, w.Word)
}

func Top10(text string) []string {
if len(text) < 1 {
return []string{}
}

words := GetWords(text)
if len(words) < 1 {
return []string{}
}

countMap := CountOfElements(words)
countWords := make([]wordWidth, 0, len(countMap))

for k, v := range countMap {
countWords = append(countWords, wordWidth{Word: k, Width: v})
}

sort.Slice(countWords, func(i, j int) bool {
return countWords[i].Width > countWords[j].Width ||
(countWords[i].Width == countWords[j].Width && countWords[i].Word < countWords[j].Word)
})

res := make([]string, 0, 10)

ln := len(countWords)
if ln > 10 {
ln = 10
}

for _, ww := range countWords[:ln] {
res = append(res, ww.Word)
}
return res
}
76 changes: 76 additions & 0 deletions hw03_frequency_analysis/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,79 @@ func TestTop10(t *testing.T) {
}
})
}

func TestGetWords(t *testing.T) {
tests := []struct {
input string
expected []string
}{
{input: "", expected: []string{}},
{input: "alfa beta gamma ", expected: []string{"alfa", "beta", "gamma"}},
{
input: `Предложения складываются в абзацы —
и вы наслаждетесь очередным бредошедевром.`,
expected: []string{
"Предложения",
"складываются",
"в",
"абзацы",
"—",
"и",
"вы",
"наслаждетесь",
"очередным",
"бредошедевром.",
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.input, func(t *testing.T) {
result := GetWords(tc.input)
require.Equal(t, tc.expected, result)
})
}
}

func TestCountOfElements(t *testing.T) {
tests := []struct {
name string
input []string
expected map[string]int
}{
{
name: "empty",
input: []string{},
expected: map[string]int{},
},
{
name: "all_one",
input: []string{"alfa", "beta", "gamma"},
expected: map[string]int{
"alfa": 1,
"beta": 1,
"gamma": 1,
},
},
{
name: "second_two",
input: []string{"Мама", "мыла", "раму,", "раму", "мыла", "мама"},
expected: map[string]int{
"Мама": 1,
"мыла": 2,
"раму,": 1,
"раму": 1,
"мама": 1,
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
result := CountOfElements(tc.input)
require.Equal(t, tc.expected, result)
})
}
}

0 comments on commit 17b5da1

Please sign in to comment.