Решение на Log Parsing от Ангел Цанев

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

Към профила на Ангел Цанев

Резултати

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

Код

package main
func Map(data []string, mapper func(string) string) (result []string) {
result = make([]string, len(data))
for index, value := range data {
result[index] = mapper(value)
}
return
}
func Filter(data []string, predicate func(string) bool) (result []string) {
result = make([]string, 0, len(data))
for _, value := range data {
if predicate(value) {
result = append(result, value)
}
}
return
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 1 {
return data[0]
}
if len(data) >= 2 {
result = combinator(data[0], data[1])
}
if len(data) > 3 {
for i := 2; i < len(data); i++ {
result = combinator(result, data[i])
}
}
return
}
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-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
--- FAIL: TestReduceFilterMap (0.00 seconds)
	solution_test.go:124: Expected [154] got [15]
FAIL
exit status 1
FAIL	_/tmp/d20141023-10368-qwohh8	0.011s
--- FAIL: TestReduceFilterMap (0.00 seconds)
	solution_test.go:124: Expected [154] got [15]
FAIL
exit status 1
FAIL	_/tmp/d20141023-10368-qwohh8	0.062s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s
PASS
ok  	_/tmp/d20141023-10368-qwohh8	0.011s

История (5 версии и 4 коментара)

Ангел обнови решението на 15.10.2014 17:03 (преди над 3 години)

+package main
+
+func Map(data []string, mapper func(string) string) (result []string) {
+ result = make([]string, len(data))
+ for index, value := range data {
+ result[index] = mapper(value)
+ }
+ return
+}
+
+func Filter(data []string, predicate func(string) bool) (result []string) {
+ result = make([]string, 0)
+ for _, value := range data {
+ if predicate(value) {
+ result = append(result, value)
+ }
+ }
+ return
+}
+
+func Reduce(data []string, combinator func(string, string) string) (result string) {
+ for _, value := range data {
+ result = combinator(result, value)
+ }
+ return
+}
+
+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
+}

Супер. Много добро решение.

Единствено мога да се заям за result = make([]string, 0). Изпълнява изискванията напълно, но дай да помислим дали има начин, каквото и да се подаде на Filter, никога да не се налага на Go да преоразмерява масива под result :)

Ангел обнови решението на 16.10.2014 20:12 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) (result []string) {
result = make([]string, len(data))
for index, value := range data {
result[index] = mapper(value)
}
return
}
func Filter(data []string, predicate func(string) bool) (result []string) {
- result = make([]string, 0)
+ result = make([]string, 0, len(data))
for _, value := range data {
if predicate(value) {
result = append(result, value)
}
}
return
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
for _, value := range data {
result = combinator(result, value)
}
return
}
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
}

Ангел обнови решението на 20.10.2014 12:03 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) (result []string) {
result = make([]string, len(data))
for index, value := range data {
result[index] = mapper(value)
}
return
}
func Filter(data []string, predicate func(string) bool) (result []string) {
result = make([]string, 0, len(data))
for _, value := range data {
if predicate(value) {
result = append(result, value)
}
}
return
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
+ if len(data) == 1 {
+ return combinator(data[0], "")
+ }
+ if len(data) == 2 {
+ return combinator(data[0], data[1])
+ }
for _, value := range data {
result = combinator(result, value)
}
return
}
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
}

Ангел обнови решението на 20.10.2014 23:58 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) (result []string) {
result = make([]string, len(data))
for index, value := range data {
result[index] = mapper(value)
}
return
}
func Filter(data []string, predicate func(string) bool) (result []string) {
result = make([]string, 0, len(data))
for _, value := range data {
if predicate(value) {
result = append(result, value)
}
}
return
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 1 {
- return combinator(data[0], "")
+ return data[0]
}
if len(data) == 2 {
return combinator(data[0], data[1])
}
for _, value := range data {
result = combinator(result, value)
}
return
}
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
}

Ангел обнови решението на 21.10.2014 15:28 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) (result []string) {
result = make([]string, len(data))
for index, value := range data {
result[index] = mapper(value)
}
return
}
func Filter(data []string, predicate func(string) bool) (result []string) {
result = make([]string, 0, len(data))
for _, value := range data {
if predicate(value) {
result = append(result, value)
}
}
return
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 1 {
return data[0]
}
- if len(data) == 2 {
- return combinator(data[0], data[1])
+
+ if len(data) >= 2 {
+ result = combinator(data[0], data[1])
}
- for _, value := range data {
- result = combinator(result, value)
+ if len(data) > 3 {
+ for i := 2; i < len(data); i++ {
+ result = combinator(result, data[i])
+ }
}
return
}
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
-}
+}