Решение на Concurrent Tasks от Станислав Траилов

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

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

Резултати

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

Код

package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type error interface {
Error() string
}
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
type Stringer interface {
String() string
}
type Book struct {
Title string `xml:"title" json:"title"`
Isbn string `xml:"isbn,attr" json:"isbn"`
Author map[string]string `xml:"author>first_name" "author>last_name" json:"author"`
Ratings [5]int `xml:"ratings>rating" json:"ratings"`
}
func (b Book) String() string {
return "[" + b.Isbn + "]" + " " + b.Title + " " + "от" + " " + b.Author["first_name"] + " " + b.Author["last_name"] + "\n"
}
type Librarian struct {
busy bool
}
type Mylib struct {
Collection map[string]int
workers []Librarian
}
type Request struct {
Isbn string
Type int
}
func (r *Request) GetType() int {
return r.Type
}
func (r *Request) GetIsbn() string {
return r.Isbn
}
type Response struct {
l Library
req Request
}
func (r *Response) GetBook() (fmt.Stringer, error) {
book := new(Book)
t := r.req.GetType()
var err error
switch {
//Borrow book
case t == 1:
x := r.l.GetCollection()
for key, _ := range x {
json.Unmarshal([]byte(key), book)
if r.req.Isbn == book.Isbn {
if x[key] == 0 {
return nil, &errorString{"Няма наличност на книга" + " " + r.req.Isbn}
} else {
x[key] = x[key] - 1
return book, nil
}
}
return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
}
// Return book
case t == 2:
x := r.l.GetCollection()
for key, _ := range x {
json.Unmarshal([]byte(key), book)
if r.req.Isbn == book.Isbn {
x[key] = x[key] + 1
break
}
return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
}
marshalledb, e := json.Marshal(book)
if e != nil {
return nil, err
}
_, err = r.l.AddBookJSON(marshalledb)
return nil, err
}
return book, err
}
func NewLibrary(librarians int) Library {
new_lib := new(Mylib)
var x = make([]Librarian, librarians)
new_lib.workers = x
return new_lib
}
func (l *Mylib) AddBookJSON(data []byte) (int, error) {
var count int
var err error
value, ok := l.Collection[string(data)]
if ok && value >= 0 && value < 4 {
count = value + 1
l.Collection[string(data)] = count
err = nil
} else {
if ok == false {
l.Collection = make(map[string]int)
count = 1
l.Collection[string(data)] = 1
err = nil
} else {
book := new(Book)
json.Unmarshal(data, &book)
return 0, &errorString{"Има 4 копия на книга" + " " + book.Isbn}
}
}
return count, err
}
func (l *Mylib) AddBookXML(data []byte) (int, error) {
b := new(Book)
xml.Unmarshal(data, &b)
mb, _ := json.Marshal(b)
x, e := l.AddBookJSON(mb)
return x, e
}
func (lib *Mylib) GetCollection() map[string]int {
return lib.Collection
}
func (lib *Mylib) GetWorkers() []Librarian {
return lib.workers
}
func (lib *Mylib) Hello() (chan<- LibraryRequest, <-chan LibraryResponse) {
req := make(chan<- LibraryRequest)
res := make(<-chan LibraryResponse)
return req, res
}
func (lib *Mylib) GetAvailability() (available int, registered int) {
return 0, 0
}
type Library interface {
AddBookJSON(data []byte) (int, error)
AddBookXML(data []byte) (int, error)
Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
GetCollection() map[string]int
GetWorkers() []Librarian
}
type LibraryRequest interface {
GetType() int
GetISBN() string
}
type LibraryResponse interface {
GetBook() (fmt.Stringer, error)
GetAvailability() (available int, registered int)
}

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

PASS
ok  	_/tmp/d20151207-5667-32tmq5	0.003s
panic: test timed out after 1s

goroutine 17 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 6 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestAddBookJSON(0xc8200b2000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:123 +0x156e
testing.tRunner(0xc8200b2000, 0x6e5cf8)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.007s
panic: test timed out after 1s

goroutine 17 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 6 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestAddBookXML(0xc8200b2000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:153 +0x156e
testing.tRunner(0xc8200b2000, 0x6e5d10)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s
--- FAIL: TestAddBookCombined (0.00s)
	solution_test.go:82: [solution_test.go:172] Expected:
		2
		Got:
		1
		Context: first book added 2nd time
	solution_test.go:82: [solution_test.go:175] Expected:
		3
		Got:
		1
		Context: first book added 3rd time
	solution_test.go:82: [solution_test.go:179] Expected:
		4
		Got:
		1
		Context: first book added 4th time
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x4836aa]

goroutine 6 [running]:
testing.tRunner.func1(0xc8200b2000)
	/usr/local/go/src/testing/testing.go:450 +0x171
_/tmp/d20151207-5667-32tmq5.TestAddBookCombined(0xc8200b2000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:183 +0x148a
testing.tRunner(0xc8200b2000, 0x6e5d28)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	0.020s
panic: test timed out after 1s

goroutine 17 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 6 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestBookAvailability(0xc8200b2000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:211 +0xbb1
testing.tRunner(0xc8200b2000, 0x6e5d40)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s
panic: test timed out after 1s

goroutine 21 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 20 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestTakeBookRequest(0xc8200ce000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:230 +0x2fe
testing.tRunner(0xc8200ce000, 0x6e5d58)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s
panic: test timed out after 1s

goroutine 3 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 20 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestBookStringer(0xc8200cc000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:271 +0x2d1
testing.tRunner(0xc8200cc000, 0x6e5d70)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s
panic: test timed out after 1s

goroutine 17 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 6 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestTakeMissingBook(0xc8200b2000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:294 +0x340
testing.tRunner(0xc8200b2000, 0x6e5d88)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.007s
panic: test timed out after 1s

goroutine 21 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 20 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestReturnSomeBooks(0xc8200ce000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:322 +0x404
testing.tRunner(0xc8200ce000, 0x6e5da0)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s
panic: test timed out after 1s

goroutine 21 [running]:
testing.startAlarm.func1()
	/usr/local/go/src/testing/testing.go:703 +0x132
created by time.goFunc
	/usr/local/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x642e58, 0x6e5ce0, 0xa, 0xa, 0x6e8901)
	/usr/local/go/src/testing/testing.go:562 +0x8ad
testing.(*M).Run(0xc82003fef8, 0xc82003ff28)
	/usr/local/go/src/testing/testing.go:494 +0x70
main.main()
	_/tmp/d20151207-5667-32tmq5/_test/_testmain.go:72 +0x116

goroutine 20 [chan send]:
_/tmp/d20151207-5667-32tmq5.TestReturnUnknownBook(0xc8200ce000)
	/tmp/d20151207-5667-32tmq5/solution_test.go:369 +0x340
testing.tRunner(0xc8200ce000, 0x6e5db8)
	/usr/local/go/src/testing/testing.go:456 +0x98
created by testing.RunTests
	/usr/local/go/src/testing/testing.go:561 +0x86d
exit status 2
FAIL	_/tmp/d20151207-5667-32tmq5	1.006s

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

Станислав обнови решението на 06.12.2015 16:51 (преди над 2 години)

+package main
+
+import (
+ "encoding/json"
+ "encoding/xml"
+ "fmt"
+)
+
+type error interface {
+ Error() string
+}
+
+type errorString struct {
+ s string
+}
+
+func (e *errorString) Error() string {
+ return e.s
+}
+
+type Stringer interface {
+ String() string
+}
+
+type Book struct {
+ Title string `xml:"title" json:"title"`
+ Isbn string `xml:"isbn,attr" json:"isbn"`
+ Author map[string]string `xml:"author>first_name" "author>last_name" json:"author"`
+ Ratings [5]int `xml:"ratings>rating" json:"ratings"`
+}
+
+func (b Book) String() string {
+ return "[" + b.Isbn + "]" + " " + b.Title + " " + "от" + " " + b.Author["first_name"] + " " + b.Author["last_name"] + "\n"
+}
+
+type Librarian struct {
+ busy bool
+}
+
+type Mylib struct {
+ Collection map[string]int
+ workers []Librarian
+}
+
+type Request struct {
+ Isbn string
+ Type int
+}
+
+func (r *Request) GetType() int {
+ return r.Type
+}
+
+func (r *Request) GetIsbn() string {
+ return r.Isbn
+}
+
+type Response struct {
+ l Library
+ req Request
+}
+
+func (r *Response) GetBook() (fmt.Stringer, error) {
+ book := new(Book)
+ t := r.req.GetType()
+ var err error
+ switch {
+ //Borrow book
+ case t == 1:
+ x := r.l.GetCollection()
+ for key, _ := range x {
+ json.Unmarshal([]byte(key), book)
+ if r.req.Isbn == book.Isbn {
+ if x[key] == 0 {
+ return nil, &errorString{"Няма наличност на книга" + " " + r.req.Isbn}
+ } else {
+ x[key] = x[key] - 1
+ return book, nil
+ }
+ }
+ return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
+ }
+ // Return book
+ case t == 2:
+ x := r.l.GetCollection()
+ for key, _ := range x {
+ json.Unmarshal([]byte(key), book)
+ if r.req.Isbn == book.Isbn {
+ x[key] = x[key] + 1
+ break
+ }
+ return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
+ }
+ marshalledb, e := json.Marshal(book)
+ if e != nil {
+ return nil, err
+ }
+ _, err = r.l.AddBookJSON(marshalledb)
+ return nil, err
+ }
+ return book, err
+}
+
+func NewLibrary(librarians int) Library {
+ new_lib := new(Mylib)
+ var x = make([]Librarian, librarians)
+ new_lib.workers = x
+ return new_lib
+}
+
+func (l *Mylib) AddBookJSON(data []byte) (int, error) {
+ var count int
+ var err error
+ value, ok := l.Collection[string(data)]
+ if ok && value >= 0 && value < 4 {
+ count = value + 1
+ l.Collection[string(data)] = count
+ err = nil
+ } else {
+ if ok == false {
+ l.Collection = make(map[string]int)
+ count = 1
+ l.Collection[string(data)] = 1
+ err = nil
+ } else {
+ book := new(Book)
+ json.Unmarshal(data, &book)
+ return 0, &errorString{"Има 4 копия на книга" + " " + book.Isbn}
+ }
+ }
+ return count, err
+}
+
+func (l *Mylib) AddBookXML(data []byte) (int, error) {
+ b := new(Book)
+ xml.Unmarshal(data, &b)
+ mb, _ := json.Marshal(b)
+ x, e := l.AddBookJSON(mb)
+ return x, e
+}
+
+func (lib *Mylib) GetCollection() map[string]int {
+ return lib.Collection
+}
+
+func (lib *Mylib) GetWorkers() []Librarian {
+ return lib.workers
+}
+
+type Library interface {
+ AddBookJSON(data []byte) (int, error)
+ AddBookXML(data []byte) (int, error)
+ //Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
+ GetCollection() map[string]int
+ GetWorkers() []Librarian
+}
+
+type LibraryRequest interface {
+ GetType() int
+ GetISBN() string
+}
+
+type LibraryResponse interface {
+ GetBook() (fmt.Stringer, error)
+ //GetAvailability() (available int, registered int)
+}

Станислав обнови решението на 06.12.2015 17:28 (преди над 2 години)

package main
import (
"encoding/json"
"encoding/xml"
"fmt"
)
type error interface {
Error() string
}
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
type Stringer interface {
String() string
}
type Book struct {
Title string `xml:"title" json:"title"`
Isbn string `xml:"isbn,attr" json:"isbn"`
Author map[string]string `xml:"author>first_name" "author>last_name" json:"author"`
Ratings [5]int `xml:"ratings>rating" json:"ratings"`
}
func (b Book) String() string {
return "[" + b.Isbn + "]" + " " + b.Title + " " + "от" + " " + b.Author["first_name"] + " " + b.Author["last_name"] + "\n"
}
type Librarian struct {
busy bool
}
type Mylib struct {
Collection map[string]int
workers []Librarian
}
type Request struct {
Isbn string
Type int
}
func (r *Request) GetType() int {
return r.Type
}
func (r *Request) GetIsbn() string {
return r.Isbn
}
type Response struct {
l Library
req Request
}
func (r *Response) GetBook() (fmt.Stringer, error) {
book := new(Book)
t := r.req.GetType()
var err error
switch {
//Borrow book
case t == 1:
x := r.l.GetCollection()
for key, _ := range x {
json.Unmarshal([]byte(key), book)
if r.req.Isbn == book.Isbn {
if x[key] == 0 {
return nil, &errorString{"Няма наличност на книга" + " " + r.req.Isbn}
} else {
x[key] = x[key] - 1
return book, nil
}
}
return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
}
// Return book
case t == 2:
x := r.l.GetCollection()
for key, _ := range x {
json.Unmarshal([]byte(key), book)
if r.req.Isbn == book.Isbn {
x[key] = x[key] + 1
break
}
return nil, &errorString{"Непозната книга" + " " + r.req.Isbn}
}
marshalledb, e := json.Marshal(book)
if e != nil {
return nil, err
}
_, err = r.l.AddBookJSON(marshalledb)
return nil, err
}
return book, err
}
func NewLibrary(librarians int) Library {
new_lib := new(Mylib)
var x = make([]Librarian, librarians)
new_lib.workers = x
return new_lib
}
func (l *Mylib) AddBookJSON(data []byte) (int, error) {
var count int
var err error
value, ok := l.Collection[string(data)]
if ok && value >= 0 && value < 4 {
count = value + 1
l.Collection[string(data)] = count
err = nil
} else {
if ok == false {
l.Collection = make(map[string]int)
count = 1
l.Collection[string(data)] = 1
err = nil
} else {
book := new(Book)
json.Unmarshal(data, &book)
return 0, &errorString{"Има 4 копия на книга" + " " + book.Isbn}
}
}
return count, err
}
func (l *Mylib) AddBookXML(data []byte) (int, error) {
b := new(Book)
xml.Unmarshal(data, &b)
mb, _ := json.Marshal(b)
x, e := l.AddBookJSON(mb)
return x, e
}
func (lib *Mylib) GetCollection() map[string]int {
return lib.Collection
}
func (lib *Mylib) GetWorkers() []Librarian {
return lib.workers
}
+func (lib *Mylib) Hello() (chan<- LibraryRequest, <-chan LibraryResponse) {
+ req := make(chan<- LibraryRequest)
+ res := make(<-chan LibraryResponse)
+ return req, res
+}
+
+func (lib *Mylib) GetAvailability() (available int, registered int) {
+ return 0, 0
+}
+
type Library interface {
AddBookJSON(data []byte) (int, error)
AddBookXML(data []byte) (int, error)
- //Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
+ Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
GetCollection() map[string]int
GetWorkers() []Librarian
}
type LibraryRequest interface {
GetType() int
GetISBN() string
}
type LibraryResponse interface {
GetBook() (fmt.Stringer, error)
- //GetAvailability() (available int, registered int)
+ GetAvailability() (available int, registered int)
}