Решение на Log Parsing от Иван Боршуков

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

Към профила на Иван Боршуков

Резултати

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

Код

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0, len(data))
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
r := make([]string, len(filtered))
copy(r, filtered)
return r
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 0 {
return
}
result = data[0]
for i := 1; i < len(data); i++ {
result = combinator(result, data[i])
}
return
}
func All(data []string, predicate func(string) bool) bool {
for _, str := range data {
if !predicate(str) {
return false
}
}
return true
}
func Any(data []string, predicate func(string) bool) bool {
for _, str := range data {
if predicate(str) {
return true
}
}
return false
}

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

PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.045s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.012s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.019s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s
PASS
ok  	_/tmp/d20141023-10368-wi8gt6	0.011s

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

Иван обнови решението на 17.10.2014 13:26 (преди над 3 години)

+package main
+
+func Map(data []string, mapper func(string) string) []string {
+ mapped := make([]string, len(data))
+ for i, str := range data {
+ mapped[i] = mapper(str)
+ }
+ return mapped
+}
+
+func Filter(data []string, predicate func(string) bool) []string {
+ filtered := make([]string, 0)
+ for _, str := range data {
+ if predicate(str) {
+ filtered = append(filtered, str)
+ }
+ }
+ return filtered
+}
+
+func Reduce(data []string, combinator func(string, string) string) (result string) {
+ for _, str := range data {
+ result = combinator(result, str)
+ }
+ return
+}
+
+func All(data []string, predicate func(string) bool) bool {
+ result := true
+ for _, str := range data {
+ result = result && predicate(str)
+ }
+ return result
+}
+
+func Any(data []string, predicate func(string) bool) bool {
+ result := false
+ for _, str := range data {
+ result = result || predicate(str)
+ }
+ return result
+}

Иван обнови решението на 17.10.2014 13:53 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0)
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
return filtered
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
for _, str := range data {
result = combinator(result, str)
}
return
}
func All(data []string, predicate func(string) bool) bool {
- result := true
for _, str := range data {
- result = result && predicate(str)
+ if !predicate(str) {
+ return false
+ }
}
- return result
+ return true
}
func Any(data []string, predicate func(string) bool) bool {
- result := false
for _, str := range data {
- result = result || predicate(str)
+ if predicate(str) {
+ return true
+ }
}
- return result
+ return false
}

Браво че си сетил да ползваш наименуван резултат и че нулевата му стойност на string е "", но какво ще стане ако на Reduce му бъде подаден data с един елемент?

Също така Filter може би ще копира много пъти масива.

Иван обнови решението на 18.10.2014 11:32 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0)
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
return filtered
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
+ if len(data) == 1 {
+ return data[0]
+ }
for _, str := range data {
result = combinator(result, str)
}
return
}
func All(data []string, predicate func(string) bool) bool {
for _, str := range data {
if !predicate(str) {
return false
}
}
return true
}
func Any(data []string, predicate func(string) bool) bool {
for _, str := range data {
if predicate(str) {
return true
}
}
return false
}

Иван обнови решението на 18.10.2014 11:45 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
- filtered := make([]string, 0)
+ filtered := make([]string, 0, len(data))
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
return filtered
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 1 {
return data[0]
}
for _, str := range data {
result = combinator(result, str)
}
return
}
func All(data []string, predicate func(string) bool) bool {
for _, str := range data {
if !predicate(str) {
return false
}
}
return true
}
func Any(data []string, predicate func(string) bool) bool {
for _, str := range data {
if predicate(str) {
return true
}
}
return false
}

Иван обнови решението на 18.10.2014 17:13 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0, len(data))
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
return filtered
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
- if len(data) == 1 {
- return data[0]
+ if len(data) == 0 {
+ return
}
- for _, str := range data {
- result = combinator(result, str)
+ result = data[0]
+
+ for i := 1; i < len(data); i++ {
+ result = combinator(result, data[i])
}
return
}
func All(data []string, predicate func(string) bool) bool {
for _, str := range data {
if !predicate(str) {
return false
}
}
return true
}
func Any(data []string, predicate func(string) bool) bool {
for _, str := range data {
if predicate(str) {
return true
}
}
return false
}

Иван обнови решението на 21.10.2014 13:37 (преди над 3 години)

package main
func Map(data []string, mapper func(string) string) []string {
mapped := make([]string, len(data))
for i, str := range data {
mapped[i] = mapper(str)
}
return mapped
}
func Filter(data []string, predicate func(string) bool) []string {
filtered := make([]string, 0, len(data))
for _, str := range data {
if predicate(str) {
filtered = append(filtered, str)
}
}
- return filtered
+ r := make([]string, len(filtered))
+ copy(r, filtered)
+ return r
}
func Reduce(data []string, combinator func(string, string) string) (result string) {
if len(data) == 0 {
return
}
result = data[0]
-
for i := 1; i < len(data); i++ {
result = combinator(result, data[i])
}
return
}
func All(data []string, predicate func(string) bool) bool {
for _, str := range data {
if !predicate(str) {
return false
}
}
return true
}
func Any(data []string, predicate func(string) bool) bool {
for _, str := range data {
if predicate(str) {
return true
}
}
return false
}