Решение на Poor Man's Currying от Христо Христов

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

Към профила на Христо Христов

Резултати

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

Код

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

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

--- FAIL: TestExamplesInReadme (0.00s)
    --- FAIL: TestExamplesInReadme/Generator (0.00s)
        solution_test.go:58: At index 0 expected 0 but got 1
        solution_test.go:58: At index 1 expected 1 but got 2
        solution_test.go:58: At index 2 expected 2 but got 4
        solution_test.go:58: At index 3 expected 4 but got 16
        solution_test.go:58: At index 4 expected 2 but got 3
        solution_test.go:58: At index 5 expected 16 but got 256
        solution_test.go:58: At index 6 expected 3 but got 4
        solution_test.go:58: At index 7 expected 256 but got 65536
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-gcm17k	0.002s
PASS
ok  	_/tmp/d20181024-53-gcm17k	0.002s
PASS
ok  	_/tmp/d20181024-53-gcm17k	0.002s
--- FAIL: TestGenerator (0.00s)
    --- FAIL: TestGenerator/powers_of_two (0.00s)
        solution_test.go:204: At index 0 expected 1 but got 2
        solution_test.go:204: At index 1 expected 2 but got 4
        solution_test.go:204: At index 2 expected 4 but got 8
        solution_test.go:204: At index 3 expected 8 but got 16
        solution_test.go:204: At index 4 expected 16 but got 32
        solution_test.go:204: At index 5 expected 32 but got 64
        solution_test.go:204: At index 6 expected 64 but got 128
        solution_test.go:204: At index 7 expected 128 but got 256
        solution_test.go:204: At index 8 expected 256 but got 512
    --- FAIL: TestGenerator/fibbonaci (0.00s)
        solution_test.go:204: At index 1 expected 1 but got 2
        solution_test.go:204: At index 2 expected 2 but got 3
        solution_test.go:204: At index 3 expected 3 but got 5
        solution_test.go:204: At index 4 expected 5 but got 8
        solution_test.go:204: At index 5 expected 8 but got 13
        solution_test.go:204: At index 6 expected 13 but got 21
        solution_test.go:204: At index 7 expected 21 but got 34
        solution_test.go:204: At index 8 expected 34 but got 55
        solution_test.go:204: At index 9 expected 55 but got 89
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-gcm17k	0.003s
PASS
ok  	_/tmp/d20181024-53-gcm17k	0.002s
--- FAIL: TestMapReducerMultipleCalls (0.00s)
    solution_test.go:318: Expected 45 but got 58 after 2nd call
FAIL
exit status 1
FAIL	_/tmp/d20181024-53-gcm17k	0.002s

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

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

+package main
+
+import "fmt"
+
+func Repeater(s, sep string) func(int) string {
+ return func(numberOfRepeats int) string {
+ if numberOfRepeats < 1 {
+ return ""
+ }
+ var result string
+ for i := 0; i < numberOfRepeats-1; i++ {
+ result += s + sep
+ }
+ return result + s
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ return func() int {
+ initial = gen(initial)
+ return initial
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ return func(numbers ...int) int {
+ for i := range numbers {
+ numbers[i] = mapper(numbers[i])
+ }
+
+ for _, number := range numbers {
+ initial = reducer(initial, number)
+ }
+ return initial
+ }
+}
+
+func main() {
+ power := Generator(
+ func(v int) int { return v * v },
+ 2,
+ )
+ increaser := Generator(
+ func(v int) int { return v + 1 },
+ 0,
+ )
+ fmt.Printf("%d %d %d \n", power(), power(), power())
+ fmt.Printf("%d %d %d \n", increaser(), increaser(), increaser())
+
+ powerSum := MapReducer(
+ func(v int) int { return v * v },
+ func(a, v int) int { return a + v },
+ 0,
+ )
+
+ fmt.Printf("%d", powerSum(1, 2, 3, 4))
+}

Ако се пробвам да го предам с празна функция main ми казва, че не мога да го предам, защото имам синтактична грешка? Някъде бях чел, че едно от условията за предаване е да е без тази функция, нещо не съм разбрал правилно ли?

Христо обнови решението на 23.10.2018 17:32 (преди 10 месеца)

package main
-import "fmt"
-
func Repeater(s, sep string) func(int) string {
return func(numberOfRepeats int) string {
if numberOfRepeats < 1 {
return ""
}
var result string
for i := 0; i < numberOfRepeats-1; i++ {
result += s + sep
}
return result + s
}
}
func Generator(gen func(int) int, initial int) func() int {
return func() int {
initial = gen(initial)
return initial
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
return func(numbers ...int) int {
for i := range numbers {
numbers[i] = mapper(numbers[i])
}
for _, number := range numbers {
initial = reducer(initial, number)
}
return initial
}
-}
-
-func main() {
- power := Generator(
- func(v int) int { return v * v },
- 2,
- )
- increaser := Generator(
- func(v int) int { return v + 1 },
- 0,
- )
- fmt.Printf("%d %d %d \n", power(), power(), power())
- fmt.Printf("%d %d %d \n", increaser(), increaser(), increaser())
-
- powerSum := MapReducer(
- func(v int) int { return v * v },
- func(a, v int) int { return a + v },
- 0,
- )
-
- fmt.Printf("%d", powerSum(1, 2, 3, 4))
}