HW3 is completed

pull/3/head
Andrey Ivanov 2020-06-15 11:54:18 +03:00
parent 5b1035be8a
commit 6df4cf0882
3 changed files with 41 additions and 5 deletions

View File

@ -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

View File

@ -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
}

View File

@ -7,7 +7,7 @@ import (
)
// Change to true if needed
var taskWithAsteriskIsCompleted = false
var taskWithAsteriskIsCompleted = true
var text = `Как видите, он спускается по лестнице вслед за своим
другом Кристофером Робином, головой вниз, пересчитывая