Skip to content

Commit

Permalink
hw03_frequency_analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
timersha committed Jan 1, 2025
1 parent da3bb9d commit 19b00bf
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 86 deletions.
81 changes: 15 additions & 66 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
run:
tests: true
build-tags:
- bench
- !bench

linters-settings:
funlen:
lines: 150
statements: 80

issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
- dupl
- gocyclo
- gosec

linters:
disable-all: true
enable:
- asciicheck
- depguard
- dogsled
- dupl
- bodyclose
- durationcheck
- errorlint
- exhaustive
- exportloopref
- funlen
- gci
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- gofumpt
- goheader
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- lll
- makezero
- misspell
- nestif
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- staticcheck
- stylecheck
- tagliatelle
- thelper
- typecheck
- unconvert
- unparam
- unused
- whitespace
depguard:
rules:
Main:
files:
- $all
- "!$test"
allow:
- $gostd
- github.com/stretchr/testify/require
Test:
files:
- $test
allow:
- $gostd
- github.com/stretchr/testify
16 changes: 0 additions & 16 deletions hw02_unpack_string/.golangci.yml

This file was deleted.

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/timersha/golang-tests/hw03_frequency_analysis

go 1.22

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

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

type NodeValue struct {
Word string
Count int
}

type Node struct {
Left *Node
Value NodeValue
Right *Node
}

func Insert(node *Node, value NodeValue) {
if value.Count > node.Value.Count {
if node.Right == nil {
node.Right = &Node{nil, value, nil}
} else {
Insert(node.Right, value)
}
}
if value.Count <= node.Value.Count {
if node.Left == nil {
node.Left = &Node{nil, value, nil}
} else {
Insert(node.Left, value)
}
}
}

func MakeSortedArray(node *Node, nums *[]NodeValue) {
if node.Right == nil {
*nums = append(*nums, node.Value)
} else {
MakeSortedArray(node.Right, nums)
*nums = append(*nums, node.Value)
}

if node.Left == nil {
lIndex := len(*nums)
if lIndex > 0 && node.Value.Word != ((*nums)[lIndex-1]).Word {
*nums = append(*nums, node.Value)
}
} else {
MakeSortedArray(node.Left, nums)
}
}

func SortSubslices(values *[]NodeValue) []string {
result := &[]string{}
sl := &[]string{}
var previous *NodeValue
for i := range *values {
nodeValue := (*values)[i]

if previous == nil {
previous = &nodeValue
*sl = append(*sl, nodeValue.Word)
continue
}

if previous.Count == nodeValue.Count {
*sl = append(*sl, nodeValue.Word)
previous = &nodeValue
} else {
sort.Strings(*sl)
*result = append(*result, *sl...)
previous = nil
sl = &[]string{}
*sl = append(*sl, nodeValue.Word)

if len(*result) >= 10 {
break
}
}
}
sort.Strings(*sl)
*result = append(*result, *sl...)

if len(*result) >= 10 {
return (*result)[0:10]
}
return *result
}

func CleanAll(str string) string {
str = strings.ReplaceAll(str, "\n", " ")
str = strings.ReplaceAll(str, "\t", " ")
return str
}

func Top10(str string) []string {
if len(str) == 0 {
return []string{}
}
// "Чистим" строку
str = CleanAll(str)
// Считаем частоту слов
wordsFrequency := make(map[string]int)
for _, word := range strings.Split(str, " ") {
if len(word) > 0 {
wordsFrequency[word] += 1
}
}
// Строим бинарное дерево
var root *Node
for k, v := range wordsFrequency {
value := NodeValue{k, v}
if root == nil {
root = &Node{nil, value, nil}
continue
}
Insert(root, value)
}
// Обход дерева в сортированный массив
maxNums := &[]NodeValue{}
MakeSortedArray(root, maxNums)
// Сортируем слова с одинаковой частотой
result := SortSubslices(maxNums)
return result
}
23 changes: 23 additions & 0 deletions hw03_frequency_analysis/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,34 @@ var text = `Как видите, он спускается по лестни
посидеть у огня и послушать какую-нибудь интересную сказку.
В этот вечер...`

var loremIpsumText = `
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
`

func TestTop10(t *testing.T) {
t.Run("no words in empty string", func(t *testing.T) {
require.Len(t, Top10(""), 0)
})

t.Run("extra test", func(t *testing.T) {
expected := []string{
"the", // 6
"Lorem", // 4
"of", // 4
"Ipsum", // 3
"and", // 3
"It", // 2
"a", // 2
"dummy", // 2
"has", // 2
"text", // 2
}
require.Equal(t, expected, Top10(loremIpsumText))
})

t.Run("positive test", func(t *testing.T) {
if taskWithAsteriskIsCompleted {
expected := []string{
Expand Down

0 comments on commit 19b00bf

Please sign in to comment.