Често срещани думи

Краен срок
19.10.2014 12:00

Срокът за предаване на решения е отминал

Да се направи функция getMostCommonWords, която приема 2 аргумента text string и wordCountLimit int и връща []string. Функцията трябва да обходи подадения ѝ text и да върне списък (инициализиран слайс) с всички думи, преобразувани в lower case, които се срещат поне wordCountLimit пъти. Върнатият слайс трябва да е сортиран по азбучен ред.

В подадения текст, думите са разделени с интервали и могат да съдържат всякакви символи.

Example:

text := "A Be Ce DE De a! oh? a oy! oY! OY!"
mostCommonWords := getMostCommonWords(text, 2)

mostCommonWords трябва да е ["a", "de", "oy!"]

Решения

Ясен Висулчев
  • Некоректно
  • 0 успешни тест(а)
  • 3 неуспешни тест(а)
Ясен Висулчев
// Written by Yasen Visulchev
// 17/10/2014
// http://fmi.golang.bg/users/18
package main
import (
"sort"
"strings"
)
// getMostCommonWords gets the most common substrings within text, occurring more than wordCountLimit times.
func getMostCommonWords(text string, wordCountLimit int) []string {
var result []string
text = strings.ToLower(text)
tokens := strings.Split(text, " ")
result = make([]string, 0, len(tokens))
for _, value := range tokens {
count := strings.Count(text, value)
if count >= wordCountLimit && !sliceContains(result, value) {
result = append(result, value)
}
}
sort.Strings(result)
return result
}
// sliceContains returns true if the slice s contains a string sub.
func sliceContains(s []string, sub string) bool {
for _, value := range s {
if value == sub {
return true
}
}
return false
}
--- FAIL: TestWithEmptyString (0.00 seconds)
	solution_test.go:21: Expected words [] but result was []
	solution_test.go:21: Expected words [] but result was []
FAIL
exit status 1
FAIL	_/tmp/d20141020-6354-1ypn4dh	0.012s
--- FAIL: TestWithExampleData (0.00 seconds)
	solution_test.go:21: Expected words [oy!] but result was [a oy!]
FAIL
exit status 1
FAIL	_/tmp/d20141020-6354-1ypn4dh	0.011s
--- FAIL: TestWithLoremIpsum (0.00 seconds)
	solution_test.go:21: Expected words [] but result was [do dolor id in ut]
	solution_test.go:21: Expected words [in ut] but result was [do dolor et ex id in qui ut]
	solution_test.go:21: Expected words [dolor dolore in ut] but result was [ad do dolor dolore et eu ex id in qui ut]
FAIL
exit status 1
FAIL	_/tmp/d20141020-6354-1ypn4dh	0.012s
Иван Боршуков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Боршуков
package main
import (
"sort"
"strings"
)
func splitIntoLowerCaseWords(text string) []string {
lowered := strings.ToLower(text)
return strings.Fields(lowered)
}
func countWords(words []string) map[string]int {
counted := make(map[string]int)
for _, word := range words {
counted[word] += 1
}
return counted
}
func getMostCommonWords(text string, wordCountLimit int) []string {
words := splitIntoLowerCaseWords(text)
result := make([]string, 0, len(words))
for word, count := range countWords(words) {
if count >= wordCountLimit {
result = append(result, word)
}
}
sort.Sort(sort.StringSlice(result))
return result
}
PASS
ok  	_/tmp/d20141020-6354-1qgo2r	0.011s
PASS
ok  	_/tmp/d20141020-6354-1qgo2r	0.011s
PASS
ok  	_/tmp/d20141020-6354-1qgo2r	0.011s
Ангел Цанев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Ангел Цанев
package main
import (
"sort"
"strings"
)
func countWords(words []string) map[string]int {
wordCounts := make(map[string]int)
for _, word := range words {
wordCounts[word]++
}
return wordCounts
}
func getMostCommonWords(text string, wordCountLimit int) (result []string) {
words := strings.Fields(strings.ToLower(text))
wordOccurrences := countWords(words)
result = make([]string, 0, len(wordOccurrences))
for key, value := range wordOccurrences {
if wordCountLimit <= value {
result = append(result, key)
}
}
sort.Strings(result)
return
}
PASS
ok  	_/tmp/d20141020-6354-xftdhh	0.011s
PASS
ok  	_/tmp/d20141020-6354-xftdhh	0.011s
PASS
ok  	_/tmp/d20141020-6354-xftdhh	0.011s
Йосиф Цветков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Йосиф Цветков
package main
import (
"sort"
"strings"
)
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for index, value := range data {
mapped[index] = mapper(value)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0, cap(data))
for _, value := range data {
if predicate(value) {
filtered = append(filtered, value)
}
}
return filtered
}
func getMostCommonWords(text string, wordCountLimit int) []string {
words := Filter(Map(strings.Split(text, " "), strings.ToLower), func(word string) bool { return word != "" })
wordCounts := map[string]int{}
for _, value := range words {
wordCounts[value] += 1
}
for index, value := range wordCounts {
if value < wordCountLimit {
delete(wordCounts, index)
}
}
theMostCommonWords := make([]string, 0)
for index, _ := range wordCounts {
theMostCommonWords = append(theMostCommonWords, index)
}
sort.Strings(theMostCommonWords)
return theMostCommonWords
}
PASS
ok  	_/tmp/d20141020-6354-f8dje6	0.011s
PASS
ok  	_/tmp/d20141020-6354-f8dje6	0.011s
PASS
ok  	_/tmp/d20141020-6354-f8dje6	0.021s
Дойчин Атанасов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Дойчин Атанасов
package main
import (
"sort"
"strings"
)
func getMostCommonWords(text string, countLimit int) []string {
words := make(map[string]int)
result := []string{}
for _, word := range strings.Split(text, " ") {
if word == "" {
continue
}
word = strings.ToLower(word)
if _, ok := words[word]; !ok {
words[word] = 0
}
words[word]++
if words[word] == countLimit {
result = append(result, word)
}
// Ugly workaround for zero count
if countLimit == 0 && words[word] == 1 {
result = append(result, word)
}
}
sort.Strings(result)
return result
}
PASS
ok  	_/tmp/d20141020-6354-3gnxp4	0.011s
PASS
ok  	_/tmp/d20141020-6354-3gnxp4	0.011s
PASS
ok  	_/tmp/d20141020-6354-3gnxp4	0.011s
Любомир Коев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Любомир Коев
package main
import "strings"
import "sort"
func getMostCommonWords(text string, wordCountLimit int) (result []string) {
words := make(map[string]int)
for _, word := range strings.Split(text, " ") {
if len(word) >= 1 {
words[strings.ToLower(word)]++
}
}
result = make([]string, 0)
for word, hits := range words {
if hits >= wordCountLimit {
result = append(result, word)
}
}
sort.Sort(sort.StringSlice(result))
return result
}
PASS
ok  	_/tmp/d20141020-6354-1xbple3	0.011s
PASS
ok  	_/tmp/d20141020-6354-1xbple3	0.011s
PASS
ok  	_/tmp/d20141020-6354-1xbple3	0.011s
Александър Деспотов
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Деспотов
package main
import (
"sort"
"strings"
)
func splitAndLower(text string) ([]string, int) {
text = strings.ToLower(text)
resArr := strings.Fields(text)
resSize := len(resArr)
return resArr, resSize
}
func getMostCommonWords(text string, wordCountLimit int) []string {
result := make([]string, 0)
words, wordsLen := splitAndLower(text)
if wordsLen < wordCountLimit || wordCountLimit < 0 {
return result
}
sort.Strings(words)
counter := 1
for index := 0; index < wordsLen; index++ {
for ; index < wordsLen-1 && strings.EqualFold(words[index], words[index+1]); index++ {
counter++
}
if counter >= wordCountLimit {
result = append(result, words[index])
}
counter = 1
}
return result
}
PASS
ok  	_/tmp/d20141020-6354-7p0kmi	0.037s
PASS
ok  	_/tmp/d20141020-6354-7p0kmi	0.011s
PASS
ok  	_/tmp/d20141020-6354-7p0kmi	0.012s
Иван Главчев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Иван Главчев
package main
import (
"sort"
"strings"
)
// making it global, because perhaps there can be other methods, that need to know which characters are separators in text, also because if
// this array was local to IsSeparator method, then it would have to be initialized over and over with each call to IsSeparator, which is useless work
//var separators = []rune{' ', '.', ',', '?','!', ';'}
var separators = []rune{' ', '\t', '\n'}
func IsSeparator(sep rune) bool {
for _, i := range separators {
if sep == i {
return true
}
}
return false
}
func GetWordsToLower(text string) []string {
result := make([]string, 0)
// first change all separators to spaces, in order to easily split the text into words
runeText := []rune(text)
for i := 0; i < len(runeText); i++ {
if IsSeparator(runeText[i]) {
runeText[i] = ' '
}
}
modifiedText := string(runeText)
words := strings.Split(modifiedText, string(' '))
// words array can contain empty words, in case there are two words, separated with more than one separator
// add only non empty words to the result string, because we obviously dont need empty words
for _, word := range words {
if word != "" {
result = append(result, strings.ToLower(word))
}
}
return result
}
func GetWordsLMoreFrequentThan(wordCountsMap map[string]int, limit int) []string {
result := make([]string, 0)
for key, value := range wordCountsMap {
if value >= limit {
result = append(result, key)
}
}
return result
}
func getMostCommonWords(text string, limit int) []string {
wordCountsMap := make(map[string]int)
words := GetWordsToLower(text)
for _, word := range words {
wordCountsMap[word]++
}
wordsMoreFrequentThan := GetWordsLMoreFrequentThan(wordCountsMap, limit)
sort.Strings(wordsMoreFrequentThan)
return wordsMoreFrequentThan
}
PASS
ok  	_/tmp/d20141020-6354-11hsx8o	0.011s
PASS
ok  	_/tmp/d20141020-6354-11hsx8o	0.011s
PASS
ok  	_/tmp/d20141020-6354-11hsx8o	0.011s
Цветелина Борисова
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Цветелина Борисова
package main
import (
"reflect"
"sort"
"strings"
)
func Map(data []string, mapper func(string) string) []string {
for index, value := range data {
data[index] = mapper(value)
}
return data
}
func SliceCount(slice, elt interface{}) int {
v := reflect.ValueOf(slice)
counter := 0
for i := 0; i < v.Len(); i++ {
if reflect.DeepEqual(v.Index(i).Interface(), elt) {
counter++
}
}
return counter
}
func getMostCommonWords(text string, wordCountLimit int) []string {
result := make([]string, 0)
words := Map(strings.Fields(text), strings.ToLower)
sort.Strings(words)
for _, value := range words {
if SliceCount(words, value) >= wordCountLimit && SliceCount(result, value) == 0 {
result = append(result, value)
}
}
return result
}
PASS
ok  	_/tmp/d20141020-6354-1kn6di4	0.012s
PASS
ok  	_/tmp/d20141020-6354-1kn6di4	0.012s
PASS
ok  	_/tmp/d20141020-6354-1kn6di4	0.032s
Любомир Райков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Любомир Райков
package main
import (
"sort"
"strings"
)
func getMostCommonWords(text string, wordCountLimit int) []string {
lowerText := strings.ToLower(text)
splittedText := strings.Fields(lowerText)
wordCountMap := countWordsInSlice(splittedText)
wordsWithLargerCountThanN := getWordsWithLargerCountThanN(wordCountMap, wordCountLimit)
sort.Strings(wordsWithLargerCountThanN)
return wordsWithLargerCountThanN
}
func countWordsInSlice(text []string) map[string]int {
wordCountMap := make(map[string]int)
for _, word := range text {
if _, ok := wordCountMap[word]; !ok {
wordCountMap[word] = 0
}
wordCountMap[word] += 1
}
return wordCountMap
}
func getWordsWithLargerCountThanN(wordCount map[string]int, n int) []string {
words := []string{}
for key, value := range wordCount {
if value >= n {
words = append(words, key)
}
}
return words
}
PASS
ok  	_/tmp/d20141020-6354-1hfks6a	0.011s
PASS
ok  	_/tmp/d20141020-6354-1hfks6a	0.011s
PASS
ok  	_/tmp/d20141020-6354-1hfks6a	0.021s
Мартин Добрев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Добрев
package main
import (
"sort"
"strings"
)
func Reduce(words map[string]int, limit int) []string {
result := make([]string, 0)
for index, value := range words {
if value >= limit {
result = append(result, index)
}
}
return result
}
func getMostCommonWords(text string, wordCountLimit int) []string {
tokens := strings.Fields(strings.ToLower(text))
words := make(map[string]int)
for _, value := range tokens {
words[value]++
}
result := Reduce(words, wordCountLimit)
sort.Strings(result)
return result
}
PASS
ok  	_/tmp/d20141020-6354-13pk71j	0.046s
PASS
ok  	_/tmp/d20141020-6354-13pk71j	0.020s
PASS
ok  	_/tmp/d20141020-6354-13pk71j	0.012s
Калина Бухлева
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Калина Бухлева
package main
import (
"sort"
"strings"
)
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func getMostCommonWords(text string, wordCountLimit int) []string {
text = strings.ToLower(text)
inputText := strings.Split(text, " ")
result := make([]string, 0, 0)
for i := 0; i < len(inputText)-1; i++ {
count := 0
for j := i; j < len(inputText); j++ {
if inputText[i] == inputText[j] {
count++
}
}
if count >= wordCountLimit && !contains(result, inputText[i]) {
result = append(result, inputText[i])
}
}
sort.Strings(result)
return result
}
PASS
ok  	_/tmp/d20141020-6354-18l5bjq	0.011s
PASS
ok  	_/tmp/d20141020-6354-18l5bjq	0.011s
PASS
ok  	_/tmp/d20141020-6354-18l5bjq	0.011s
Илия Ячев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Илия Ячев
package main
import (
"sort"
"strings"
)
func getMostCommonWords(text string, wordCountLimit int) []string {
words := strings.Fields(text)
occurrences := map[string]int{}
for _, word := range words {
occurrences[strings.ToLower(word)]++
}
commonWords := []string{}
for word, count := range occurrences {
if count >= wordCountLimit {
commonWords = append(commonWords, word)
}
}
sort.Strings(commonWords)
return commonWords
}
PASS
ok  	_/tmp/d20141020-6354-13dkadu	0.011s
PASS
ok  	_/tmp/d20141020-6354-13dkadu	0.011s
PASS
ok  	_/tmp/d20141020-6354-13dkadu	0.011s
Евгений Бояджиев
  • Некоректно
  • 2 успешни тест(а)
  • 1 неуспешни тест(а)
Евгений Бояджиев
package main
import (
"sort"
"strings"
)
func main() {
return
}
func removeLowCountWordOccurences(stringArr []string, elementLimit int) (filteredArray []string) {
elementsCount := make(map[string]int)
for _, element := range stringArr {
elementsCount[element]++
}
filteredArray = make([]string, 0)
for element, count := range elementsCount {
if count >= elementLimit {
filteredArray = append(filteredArray, element)
}
}
return
}
func getMostCommonWords(text string, wordCountLimit int) (wordsArray []string) {
text = strings.ToLower(text)
wordsArray = strings.Split(text, " ")
wordsArray = removeLowCountWordOccurences(wordsArray, wordCountLimit)
sort.Strings(wordsArray)
return
}
--- FAIL: TestWithEmptyString (0.00 seconds)
	solution_test.go:21: Expected words [] but result was []
	solution_test.go:21: Expected words [] but result was []
FAIL
exit status 1
FAIL	_/tmp/d20141020-6354-1jbaefl	0.011s
PASS
ok  	_/tmp/d20141020-6354-1jbaefl	0.011s
PASS
ok  	_/tmp/d20141020-6354-1jbaefl	0.012s
Диян Димитров
  • Некоректно
  • 2 успешни тест(а)
  • 1 неуспешни тест(а)
Диян Димитров
package main
import (
"sort"
"strings"
)
func sortString(text string) []string {
s := strings.Split(text, " ")
sort.Strings(s)
return s
}
func timesMet(text []string, word string) int {
counter := 0
for _, value := range text {
if word == value {
counter++
}
}
return counter
}
func exist(text []string, word string) bool {
for _, value := range text {
if word == value {
return true
}
}
return false
}
func removeRedundantSpaces(text string) string {
for i := 0; i < len(text); i++ {
text = strings.Replace(text, " ", " ", -1)
}
return text
}
func getMostCommonWords(text string, wordCountLimit int) []string {
lowerCaseSorted := sortString(strings.ToLower(removeRedundantSpaces(text)))
result := []string{}
for _, value := range lowerCaseSorted {
if timesMet(lowerCaseSorted, value) >= wordCountLimit {
if !exist(result, value) {
result = append(result, value)
}
}
}
return result
}
--- FAIL: TestWithEmptyString (0.00 seconds)
	solution_test.go:21: Expected words [] but result was []
	solution_test.go:21: Expected words [] but result was []
FAIL
exit status 1
FAIL	_/tmp/d20141020-6354-jxs26g	0.011s
PASS
ok  	_/tmp/d20141020-6354-jxs26g	0.011s
PASS
ok  	_/tmp/d20141020-6354-jxs26g	0.014s
Красимир Стойков
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Красимир Стойков
package main
import (
"sort"
"strings"
)
func getCount(array []string, text string) (result int) {
for _, v := range array {
if strings.EqualFold(v, text) {
result++
}
}
return
}
func getMostCommonWords(text string, wordCountLimit int) []string {
result := []string{}
text = strings.ToLower(text)
array := strings.Fields(text)
for _, v := range array {
if getCount(array, v) >= wordCountLimit {
if getCount(result, v) == 0 {
result = append(result, v)
}
}
}
sort.Strings(result)
return result
}
PASS
ok  	_/tmp/d20141020-6354-1auh1ka	0.011s
PASS
ok  	_/tmp/d20141020-6354-1auh1ka	0.011s
PASS
ok  	_/tmp/d20141020-6354-1auh1ka	0.013s
Станислав Гатев
  • Коректно
  • 3 успешни тест(а)
  • 0 неуспешни тест(а)
Станислав Гатев
package main
import (
"sort"
"strings"
)
func wordsCount(words []string) map[string]int {
counts := make(map[string]int)
for _, word := range words {
_, ok := counts[word]
if ok == true {
counts[word] += 1
} else {
counts[word] = 1
}
}
return counts
}
func getMostCommonWords(text string, wordCountLimit int) []string {
words := strings.Fields(text)
lowerCaseWords := make([]string, 0)
for _, word := range words {
lowerCaseWords = append(lowerCaseWords, strings.ToLower(word))
}
count := wordsCount(lowerCaseWords)
uniqueWords := make(map[string]bool)
for _, word := range lowerCaseWords {
if count[word] >= wordCountLimit {
uniqueWords[word] = true
}
}
commonWords := make([]string, 0, len(uniqueWords))
for word := range uniqueWords {
commonWords = append(commonWords, word)
}
sort.Strings(commonWords)
return commonWords
}
PASS
ok  	_/tmp/d20141020-6354-t82zix	0.011s
PASS
ok  	_/tmp/d20141020-6354-t82zix	0.011s
PASS
ok  	_/tmp/d20141020-6354-t82zix	0.011s