HW3 is completed
parent
5b1035be8a
commit
6df4cf0882
|
@ -1,4 +1,4 @@
|
|||
module github.com/fixme_my_friend/hw03_frequency_analysis
|
||||
module github.com/tiburon-777/HW_OTUS/hw03_frequency_analysis
|
||||
|
||||
go 1.14
|
||||
|
||||
|
|
|
@ -1,6 +1,42 @@
|
|||
package hw03_frequency_analysis //nolint:golint,stylecheck
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Top10(_ string) []string {
|
||||
// Place your code here
|
||||
return nil
|
||||
func Top10(str string) []string {
|
||||
template := regexp.MustCompile(`[A-Za-zА-Яа-я\-]+`)
|
||||
tmpArr := template.FindAllString(str, -1)
|
||||
tmpMap := calculate(tmpArr)
|
||||
var result []string
|
||||
for i := 0; i < 12; i++ {
|
||||
count := 0
|
||||
word := ""
|
||||
for tmpWord, tmpCount := range tmpMap {
|
||||
if tmpCount > count {
|
||||
count = tmpCount
|
||||
word = tmpWord
|
||||
}
|
||||
}
|
||||
if word != "" {
|
||||
result = append(result, word)
|
||||
}
|
||||
delete(tmpMap, word)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func calculate(arr []string) map[string]int {
|
||||
result := make(map[string]int)
|
||||
for _, v := range arr {
|
||||
if v != "" && v != "-" {
|
||||
_, ok := result[strings.ToLower(v)]
|
||||
if ok {
|
||||
result[strings.ToLower(v)]++
|
||||
} else {
|
||||
result[strings.ToLower(v)] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
// Change to true if needed
|
||||
var taskWithAsteriskIsCompleted = false
|
||||
var taskWithAsteriskIsCompleted = true
|
||||
|
||||
var text = `Как видите, он спускается по лестнице вслед за своим
|
||||
другом Кристофером Робином, головой вниз, пересчитывая
|
||||
|
|
Loading…
Reference in New Issue