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 fc014c2
Show file tree
Hide file tree
Showing 5 changed files with 153 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
140 changes: 137 additions & 3 deletions hw03_frequency_analysis/top.go
Original file line number Diff line number Diff line change
@@ -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])
// }
// }

0 comments on commit fc014c2

Please sign in to comment.