From fc014c2b662930823a706d81ab295c57d29de958 Mon Sep 17 00:00:00 2001 From: timersha Date: Sun, 15 Dec 2024 11:53:58 +0300 Subject: [PATCH] hw03_frequency_analysis --- .golangci.yml | 81 ++++-------------- hw02_unpack_string/.golangci.yml | 16 ---- hw03_frequency_analysis/.sync | 0 hw03_frequency_analysis/go.mod | 2 +- hw03_frequency_analysis/top.go | 140 ++++++++++++++++++++++++++++++- 5 files changed, 153 insertions(+), 86 deletions(-) delete mode 100644 hw02_unpack_string/.golangci.yml delete mode 100644 hw03_frequency_analysis/.sync diff --git a/.golangci.yml b/.golangci.yml index d38eca5..bb62c88 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 diff --git a/hw02_unpack_string/.golangci.yml b/hw02_unpack_string/.golangci.yml deleted file mode 100644 index bb62c88..0000000 --- a/hw02_unpack_string/.golangci.yml +++ /dev/null @@ -1,16 +0,0 @@ -linters-settings: - depguard: - rules: - Main: - files: - - $all - - "!$test" - allow: - - $gostd - - github.com/stretchr/testify/require - Test: - files: - - $test - allow: - - $gostd - - github.com/stretchr/testify 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 31a9a38..579008e 100644 --- a/hw03_frequency_analysis/go.mod +++ b/hw03_frequency_analysis/go.mod @@ -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 diff --git a/hw03_frequency_analysis/top.go b/hw03_frequency_analysis/top.go index aff6568..0ba44ba 100644 --- a/hw03_frequency_analysis/top.go +++ b/hw03_frequency_analysis/top.go @@ -1,6 +1,140 @@ +// package main + 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 len(*nums) >= 10 { + return + } + + 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", " ") + // str = strings.TrimRight(str, "\r\n") + // str = strings.TrimSuffix(str, "\r\n") + 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 +} + +// func main() { +// str := text_2 +// //// fmt.Println("input:", str) +// result := Top10(str) +// //// fmt.Println("result:", result) +// for k := range result { +// fmt.Println("r => :", result[k]) +// } +// }