Решение на Poor Man's Currying от Аспарух Кожухаров

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

Към профила на Аспарух Кожухаров

Резултати

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

Код

package main
func Repeater(s, sep string) func(int) string {
return func(cnt int) string {
result := ""
for i := 0; i < cnt; i++ {
result += s
if i != cnt-1 {
result += sep
}
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
prev := initial
step := gen(initial)
initial = step
return prev
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
result := 0
return func(numbers ...int) int {
for _, number := range numbers {
result = reducer(mapper(number), result)
}
return reducer(initial,result)
}
}

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

PASS
ok  	_/tmp/d20181024-53-19wfm3i	0.002s
PASS
ok  	_/tmp/d20181024-53-19wfm3i	0.002s
PASS
ok  	_/tmp/d20181024-53-19wfm3i	0.002s
PASS
ok  	_/tmp/d20181024-53-19wfm3i	0.002s
--- FAIL: TestMapReducer (0.00s)
    --- FAIL: TestMapReducer/left_shift_all_the_way (0.00s)
        solution_test.go:291: Expected 88080384 but got 2688
    --- FAIL: TestMapReducer/initial_is_used_first (0.00s)
        solution_test.go:291: Expected 20 but got 0
    --- FAIL: TestMapReducer/left_foldedness (0.00s)
        solution_test.go:291: Expected 64 but got 0
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-19wfm3i	0.002s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:318: Expected 45 but got 81 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-19wfm3i	0.002s

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

Аспарух обнови решението на 18.10.2018 12:38 (преди 10 месеца)

+package main
+
+func Repeater(s, sep string) func(int) string {
+
+ return func(cnt int) string {
+ result := ""
+ for i := 0; i < cnt; i++ {
+ result += s
+ if i != cnt-1 {
+ result += sep
+ }
+ }
+ return result
+ }
+
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+
+ return func() int {
+ prev := initial
+ step := gen(initial)
+ initial = step
+ return prev
+ }
+
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+
+ result := 0
+ return func(numbers ...int) int {
+
+ for _, number := range numbers {
+ result = reducer(mapper(number), result)
+ }
+ return initial + result
+ }
+
+}

Порчети условието отново и по - внимателно. Има ли нещо, което да не си направил както изрично се казва? Тествай малко повече решението си с разнообразни типове от операции и функции.

Аспарух обнови решението на 20.10.2018 10:53 (преди 10 месеца)

package main
func Repeater(s, sep string) func(int) string {
return func(cnt int) string {
result := ""
for i := 0; i < cnt; i++ {
result += s
if i != cnt-1 {
result += sep
}
}
return result
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
prev := initial
step := gen(initial)
initial = step
return prev
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
result := 0
return func(numbers ...int) int {
for _, number := range numbers {
result = reducer(mapper(number), result)
}
- return initial + result
+ return reducer(initial,result)
}
}