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

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

Към профила на Тодор Борисов

Резултати

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

Код

package main
//import ("fmt")
func Repeater(s, sep string) func(int) string {
repeatingSequence := s + sep
return func(repeatTimes int) string {
var result string
for repeatTimes > 0 {
result += repeatingSequence
repeatTimes--
}
return result[0 : len(result)-1]
}
}
func Generator(gen func(int) int, initial int) func() int {
var count *int = &initial
var flag bool
return func() int {
if !flag {
flag = true
return initial
} else {
*count = gen(*count)
return *count
}
}
}
func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
var counter *int = &initial
return func(nums ...int) int {
for _, num := range nums {
*counter = reducer(*counter, mapper(num))
}
return *counter
}
}
func main() {
}

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

PASS
ok  	_/tmp/d20181024-53-1dhcg3n	0.002s
panic: runtime error: slice bounds out of range [recovered]
	panic: runtime error: slice bounds out of range

goroutine 10 [running]:
testing.tRunner.func1(0xc0000b0500)
	/usr/local/go/src/testing/testing.go:792 +0x387
panic(0x512760, 0x61bff0)
	/usr/local/go/src/runtime/panic.go:513 +0x1b9
_/tmp/d20181024-53-1dhcg3n.Repeater.func1(0x0, 0x6, 0x53778b)
	/tmp/d20181024-53-1dhcg3n/solution.go:15 +0xa8
_/tmp/d20181024-53-1dhcg3n.TestRepeater.func1(0xc0000b0500)
	/tmp/d20181024-53-1dhcg3n/solution_test.go:126 +0x6c
testing.tRunner(0xc0000b0500, 0xc00004a570)
	/usr/local/go/src/testing/testing.go:827 +0xbf
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:878 +0x353
exit status 2
FAIL	_/tmp/d20181024-53-1dhcg3n	0.004s
PASS
ok  	_/tmp/d20181024-53-1dhcg3n	0.002s
PASS
ok  	_/tmp/d20181024-53-1dhcg3n	0.002s
PASS
ok  	_/tmp/d20181024-53-1dhcg3n	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-1dhcg3n	0.002s

История (1 версия и 0 коментара)

Тодор обнови решението на 23.10.2018 23:01 (преди 10 месеца)

+package main
+
+//import ("fmt")
+
+func Repeater(s, sep string) func(int) string {
+
+ repeatingSequence := s + sep
+
+ return func(repeatTimes int) string {
+ var result string
+ for repeatTimes > 0 {
+ result += repeatingSequence
+ repeatTimes--
+ }
+ return result[0 : len(result)-1]
+ }
+}
+
+func Generator(gen func(int) int, initial int) func() int {
+ var count *int = &initial
+ var flag bool
+ return func() int {
+ if !flag {
+ flag = true
+ return initial
+ } else {
+ *count = gen(*count)
+ return *count
+ }
+ }
+}
+
+func MapReducer(mapper func(int) int, reducer func(int, int) int, initial int) func(...int) int {
+ var counter *int = &initial
+ return func(nums ...int) int {
+ for _, num := range nums {
+ *counter = reducer(*counter, mapper(num))
+ }
+ return *counter
+ }
+}
+
+func main() {
+
+}