Решение на Concurrent Retry Executor от Станимир Митев

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

Към профила на Станимир Митев

Резултати

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

Код

package main
import "time"
import "fmt"
import "sync"
var Lock sync.Mutex
func SendData(buff chan string, result chan string, logNum int) {
Lock.Lock()
for message := range buff {
result <- fmt.Sprintf("%d\t%s", logNum, message)
}
Lock.Unlock()
}
func AcceptData(log chan string, result chan string, logNum int) {
buff := make(chan string, 100)
go SendData(buff, result, logNum)
for str := range log {
buff <- str
}
close(buff)
}
func ChainReaction(logs chan chan string, result chan string) {
var logNum int = 0
for log := range logs {
logNum++
go AcceptData(log, result, logNum)
time.Sleep(1000 * time.Nanosecond)
}
}
func OrderedLogDrainer(logs chan chan string) chan string {
result := make(chan string)
go ChainReaction(logs, result)
go func() {
time.Sleep(1500 * time.Millisecond)
close(result)
}()
return result
}

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

--- FAIL: TestWithOneMessage (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithExample1 (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithExample2 (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithNoLogs (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithTwoEmptyLogs (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithDelays (0.90s)
	solution_test.go:27: Test exceeded allowed time of 900 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.903s
--- FAIL: TestTheLimits (0.50s)
	solution_test.go:27: Test exceeded allowed time of 500 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.503s
--- FAIL: TestWithManyLogs (0.80s)
	solution_test.go:27: Test exceeded allowed time of 800 milliseconds
FAIL
exit status 1
FAIL	_/tmp/d20151110-19113-fp8was	0.804s

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

Станимир обнови решението на 09.11.2015 18:32 (преди над 2 години)

+package main
+
+import "time"
+import "fmt"
+import "sync"
+
+var WaitForIt sync.WaitGroup
+
+//Maybe i will use this in the next version and not the hacky time.Sleep :D
+
+var Lock sync.Mutex
+
+func SendData(buff chan string, result chan string, logNum int) {
+ Lock.Lock()
+ for message := range buff {
+ result <- fmt.Sprintf("%d\t%s", logNum, message)
+ }
+ Lock.Unlock()
+}
+
+func ExceptData(log chan string, result chan string, logNum int) {
+ buff := make(chan string, 100)
+ go SendData(buff, result, logNum)
+ for str := range log {
+ buff <- str
+ }
+ close(buff)
+}
+
+func ChainReaction(logs chan chan string, result chan string) {
+ var logNum int = 0
+ for log := range logs {
+ logNum++
+ go ExceptData(log, result, logNum)
+ time.Sleep(1000 * time.Nanosecond)
+ }
+}
+
+func OrderedLogDrainer(logs chan chan string) chan string {
+ result := make(chan string)
+ WaitForIt.Add(2)
+ go ChainReaction(logs, result)
+ go func() {
+ time.Sleep(1500 * time.Millisecond)
+ close(result)
+ }()
+ return result
+}

Станимир обнови решението на 09.11.2015 23:21 (преди над 2 години)

package main
import "time"
import "fmt"
import "sync"
var WaitForIt sync.WaitGroup
//Maybe i will use this in the next version and not the hacky time.Sleep :D
var Lock sync.Mutex
func SendData(buff chan string, result chan string, logNum int) {
Lock.Lock()
for message := range buff {
result <- fmt.Sprintf("%d\t%s", logNum, message)
}
Lock.Unlock()
}
-func ExceptData(log chan string, result chan string, logNum int) {
+func EcceptData(log chan string, result chan string, logNum int) {
buff := make(chan string, 100)
go SendData(buff, result, logNum)
for str := range log {
buff <- str
}
close(buff)
}
func ChainReaction(logs chan chan string, result chan string) {
var logNum int = 0
for log := range logs {
logNum++
- go ExceptData(log, result, logNum)
+ go EcceptData(log, result, logNum)
time.Sleep(1000 * time.Nanosecond)
}
}
func OrderedLogDrainer(logs chan chan string) chan string {
result := make(chan string)
WaitForIt.Add(2)
go ChainReaction(logs, result)
go func() {
time.Sleep(1500 * time.Millisecond)
close(result)
}()
return result
}

Станимир обнови решението на 09.11.2015 23:23 (преди над 2 години)

package main
import "time"
import "fmt"
import "sync"
var WaitForIt sync.WaitGroup
//Maybe i will use this in the next version and not the hacky time.Sleep :D
var Lock sync.Mutex
func SendData(buff chan string, result chan string, logNum int) {
Lock.Lock()
for message := range buff {
result <- fmt.Sprintf("%d\t%s", logNum, message)
}
Lock.Unlock()
}
-func EcceptData(log chan string, result chan string, logNum int) {
+func AcceptData(log chan string, result chan string, logNum int) {
buff := make(chan string, 100)
go SendData(buff, result, logNum)
for str := range log {
buff <- str
}
close(buff)
}
func ChainReaction(logs chan chan string, result chan string) {
var logNum int = 0
for log := range logs {
logNum++
- go EcceptData(log, result, logNum)
+ go AcceptData(log, result, logNum)
time.Sleep(1000 * time.Nanosecond)
}
}
func OrderedLogDrainer(logs chan chan string) chan string {
result := make(chan string)
WaitForIt.Add(2)
go ChainReaction(logs, result)
go func() {
time.Sleep(1500 * time.Millisecond)
close(result)
}()
return result
}

Доста необичаен и опасен подход, да разчиташ на sleep за това кой първи ще вземе lock-а... :) Също така, тази WaitGroup-а въобще ползва ли се за нещо?

Помисли още малко какво се иска в задачата, може да се реши само с предадения до момента материал (канали и горутини), без Mutex или WaitGroup или каквото и да е било друго нещо от sync пакета.

Станимир обнови решението на 10.11.2015 14:38 (преди над 2 години)

package main
import "time"
import "fmt"
import "sync"
-var WaitForIt sync.WaitGroup
-
-//Maybe i will use this in the next version and not the hacky time.Sleep :D
-
var Lock sync.Mutex
func SendData(buff chan string, result chan string, logNum int) {
Lock.Lock()
for message := range buff {
result <- fmt.Sprintf("%d\t%s", logNum, message)
}
Lock.Unlock()
}
func AcceptData(log chan string, result chan string, logNum int) {
buff := make(chan string, 100)
go SendData(buff, result, logNum)
for str := range log {
buff <- str
}
close(buff)
}
func ChainReaction(logs chan chan string, result chan string) {
var logNum int = 0
for log := range logs {
logNum++
go AcceptData(log, result, logNum)
time.Sleep(1000 * time.Nanosecond)
}
}
func OrderedLogDrainer(logs chan chan string) chan string {
result := make(chan string)
- WaitForIt.Add(2)
go ChainReaction(logs, result)
go func() {
time.Sleep(1500 * time.Millisecond)
close(result)
}()
return result
}