Решение на Log Parsing от Любомир Райков

Обратно към всички решения

Към профила на Любомир Райков

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 10 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
func Map(data []string, mapper func(string) string) []string {
mappedData := []string{}
for _, value := range data {
mappedData = append(mappedData, mapper(value))
}
return mappedData
}
func Filter(data []string, predicate func(string) bool) []string {
filteredData := []string{}
for _, value := range data {
if predicate(value) {
filteredData = append(filteredData, value)
}
}
return filteredData
}
func Reduce(data []string, combinator func(string, string) string) string {
if len(data) == 0 {
return ""
}
reducedData := data[0]
for i := 1; i < len(data); i++ {
reducedData = combinator(reducedData, data[i])
}
return reducedData
}
func Any(data []string, predicate func(string) bool) bool {
for _, value := range data {
if predicate(value) {
return true
}
}
return false
}
func All(data []string, predicate func(string) bool) bool {
for _, value := range data {
if !predicate(value) {
return false
}
}
return true
}

Лог от изпълнението

PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s
PASS
ok  	_/tmp/d20141023-10368-1mwsyy5	0.011s

История (3 версии и 2 коментара)

Любомир обнови решението на 18.10.2014 17:08 (преди над 3 години)

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

Любомир обнови решението на 18.10.2014 17:31 (преди над 3 години)

package main
-import (
- "sort"
- "strings"
-)
+func Map(data []string, mapper func(string) string) []string {
+ mappedData := []string{}
+ for _, value := range data {
+ mappedData = append(mappedData, mapper(value))
+ }
+ return mappedData
+}
-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 Filter(data []string, predicate func(string) bool) []string {
+ filteredData := []string{}
+ for _, value := range data {
+ if predicate(value) {
+ filteredData = append(filteredData, value)
+ }
+ }
+ return filteredData
}
-func countWordsInSlice(text []string) map[string]int {
- wordCountMap := make(map[string]int)
- for _, word := range text {
- if _, ok := wordCountMap[word]; !ok {
- wordCountMap[word] = 0
+func Reduce(data []string, combinator func(string, string) string) string {
+ reducedData := data[0]
+ for i := 1; i < len(data); i++ {
+ reducedData = combinator(reducedData, data[i])
+ }
+ return reducedData
+}
+
+func Any(data []string, predicate func(string) bool) bool {
+ isTrue := false
+ for _, value := range data {
+ if predicate(value) {
+ isTrue = true
+ break
}
- wordCountMap[word] += 1
}
- return wordCountMap
+ return isTrue
}
-func getWordsWithLargerCountThanN(wordCount map[string]int, n int) []string {
- words := []string{}
- for key, value := range wordCount {
- if value >= n {
- words = append(words, key)
+func All(data []string, predicate func(string) bool) bool {
+ for _, value := range data {
+ if !predicate(value) {
+ return false
}
}
- return words
+ return true
}

Любомир обнови решението на 19.10.2014 10:06 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mappedData := []string{}
for _, value := range data {
mappedData = append(mappedData, mapper(value))
}
return mappedData
}
func Filter(data []string, predicate func(string) bool) []string {
filteredData := []string{}
for _, value := range data {
if predicate(value) {
filteredData = append(filteredData, value)
}
}
return filteredData
}
func Reduce(data []string, combinator func(string, string) string) string {
+ if len(data) == 0 {
+ return ""
+ }
reducedData := data[0]
for i := 1; i < len(data); i++ {
reducedData = combinator(reducedData, data[i])
}
return reducedData
}
func Any(data []string, predicate func(string) bool) bool {
- isTrue := false
for _, value := range data {
if predicate(value) {
- isTrue = true
- break
+ return true
}
}
- return isTrue
+ return false
}
func All(data []string, predicate func(string) bool) bool {
for _, value := range data {
if !predicate(value) {
return false
}
}
return true
}