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

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

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

Резултати

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

Код

package main
import "fmt"
import "encoding/xml"
import "encoding/json"
import "errors"
const (
_ = iota
BorrowBook
ReturnBook
GetAvailability
)
type Author struct {
First_name string `xml:"first_name"`
Last_name string `xml:"last_name"`
}
func (author Author) ToString() string {
return author.First_name + " " + author.Last_name
}
type Book struct {
XMLNAME xml.Name `xml:"book"`
Isbn string `xml:"isbn,attr"`
Title string `xml:"title"`
Author Author `xml:"author"`
Genre string `xml:"genre"`
Pages int `xml:"pages"`
Ratings []int `xml:"ratings>rating"`
}
// Връща "отворена" библиотека
func NewLibrary(librarians int) Library {
var library Library
coll := make(map[string]*Book)
inv := make(map[string]*Record)
library = LocalLibrary{coll, librarians, inv}
return library
}
type Record struct {
total int
current int
}
type LocalLibrary struct {
collection map[string]*Book
librarians int
inventar map[string]*Record
}
func (library LocalLibrary) AddBook(book *Book) (int, error) {
if value, ok := library.inventar[book.Isbn]; ok {
if value.total > 3 {
return value.total, errors.New(fmt.Sprintf("Има 4 копия на книга %s", book.Isbn))
} else {
library.inventar[book.Isbn].total++
library.inventar[book.Isbn].current++
}
}
if _, ok := library.inventar[book.Isbn]; !ok {
library.collection[book.Isbn] = book
library.inventar[book.Isbn] = &Record{1, 1}
}
return library.inventar[book.Isbn].total, nil
}
func (library LocalLibrary) AddBookJSON(data []byte) (int, error) {
var book Book
json.Unmarshal(data, &book)
return library.AddBook(&book)
}
func (library LocalLibrary) AddBookXML(data []byte) (int, error) {
var book Book
xml.Unmarshal(data, &book)
return library.AddBook(&book)
}
func (library LocalLibrary) Hello() (chan<- LibraryRequest, <-chan LibraryResponse) {
request := make(chan LibraryRequest, library.librarians)
response := make(chan LibraryResponse, library.librarians)
go func(req chan LibraryRequest, res chan LibraryResponse) {
var newreq LibraryRequest
newreq = <-req
switch {
case newreq.GetType() == BorrowBook:
var asw LibraryResponse
if val, ok := library.inventar[newreq.GetISBN()]; ok {
book := library.collection[newreq.GetISBN()]
if val.current != 0 {
library.inventar[newreq.GetISBN()].current--
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString(),
)}, nil}
} else {
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString())},
errors.New(fmt.Sprintf("Няма наличност на книга %s)", book.Isbn)),
}
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN()))}
}
res <- asw
case newreq.GetType() == ReturnBook:
var asw LibraryResponse
if _, ok := library.inventar[newreq.GetISBN()]; ok {
library.inventar[newreq.GetISBN()].current++
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{""},
nil,
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
}
}
res <- asw
case newreq.GetType() == GetAvailability:
var asw LibraryResponse
if _, ok := library.inventar[newreq.GetISBN()]; ok {
book := library.collection[newreq.GetISBN()]
library.inventar[newreq.GetISBN()].current++
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString(),
)},
nil,
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
}
}
res <- asw
}
}(request, response)
return request, response
}
type Library interface {
AddBookJSON(data []byte) (int, error)
AddBookXML(data []byte) (int, error)
Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
}
type Request struct {
Type int
Isbn string
}
func (rq Request) GetISBN() string {
return rq.Isbn
}
func (rq Request) SetIsbn(isbn string) {
rq.Isbn = isbn
}
type LibraryRequest interface {
GetType() int
GetISBN() string
}
type stringer struct {
data string
}
func (s stringer) String() string { return s.data }
type Response struct {
registered, available int
bookInfo stringer
err error
}
func (rp Response) GetBook() (fmt.Stringer, error) {
return rp.bookInfo, rp.err
}
func (rp Response) GetAvailability() (available int, registered int) {
return rp.available, rp.registered
}
type LibraryResponse interface {
GetBook() (fmt.Stringer, error)
GetAvailability() (available int, registered int)
}

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

PASS
ok  	_/tmp/d20151207-5667-hxvd07	0.003s
PASS
ok  	_/tmp/d20151207-5667-hxvd07	0.003s
PASS
ok  	_/tmp/d20151207-5667-hxvd07	0.004s
PASS
ok  	_/tmp/d20151207-5667-hxvd07	0.004s
--- FAIL: TestBookAvailability (0.00s)
	solution_test.go:82: [solution_test.go:217] Expected:
		2
		Got:
		3
		Context: should have 2 copies available
FAIL
exit status 1
FAIL	_/tmp/d20151207-5667-hxvd07	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(0x647460, 0x6e9ce0, 0xa, 0xa, 0x6ec901)
	/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-hxvd07/_test/_testmain.go:72 +0x116

goroutine 6 [chan receive]:
_/tmp/d20151207-5667-hxvd07.TestTakeBookRequest(0xc8200b4000)
	/tmp/d20151207-5667-hxvd07/solution_test.go:239 +0x55c
testing.tRunner(0xc8200b4000, 0x6e9d58)
	/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-hxvd07	1.009s
PASS
ok  	_/tmp/d20151207-5667-hxvd07	0.004s
--- FAIL: TestTakeMissingBook (0.00s)
	solution_test.go:82: [solution_test.go:303] Expected:
		Непозната книга 0954540018123
		Got:
		Непозната книга 0954540018123)
		Context: Error when getting unregistered book
FAIL
exit status 1
FAIL	_/tmp/d20151207-5667-hxvd07	0.004s
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(0x647460, 0x6e9ce0, 0xa, 0xa, 0x6ec901)
	/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-hxvd07/_test/_testmain.go:72 +0x116

goroutine 6 [chan receive]:
_/tmp/d20151207-5667-hxvd07.TestReturnSomeBooks(0xc8200b2000)
	/tmp/d20151207-5667-hxvd07/solution_test.go:325 +0x5ed
testing.tRunner(0xc8200b2000, 0x6e9da0)
	/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-hxvd07	1.011s
--- FAIL: TestReturnUnknownBook (0.00s)
	solution_test.go:82: [solution_test.go:378] Expected:
		Непозната книга 0954540018123
		Got:
		Непозната книга 0954540018123)
		Context: should get error when we return unknown book
FAIL
exit status 1
FAIL	_/tmp/d20151207-5667-hxvd07	0.004s

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

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

+package main
+
+import "fmt"
+import "encoding/xml"
+import "encoding/json"
+import "errors"
+
+const (
+ _ = iota
+ BorrowBook
+ ReturnBook
+ GetAvailability
+)
+
+type Author struct {
+ First_name string `xml:"first_name"`
+ Last_name string `xml:"last_name"`
+}
+
+func (author Author) ToString() string {
+ return author.First_name + " " + author.Last_name
+}
+
+type Book struct {
+ XMLNAME xml.Name `xml:"book"`
+ Isbn string `xml:"isbn,attr"`
+ Title string `xml:"title"`
+ Author Author `xml:"author"`
+ Genre string `xml:"genre"`
+ Pages int `xml:"pages"`
+ Ratings []int `xml:"ratings>rating"`
+}
+
+// Връща "отворена" библиотека
+func NewLibrary(librarians int) Library {
+ var library Library
+ coll := make(map[string]*Book)
+ inv := make(map[string]*Record)
+ library = LocalLibrary{coll, librarians, inv}
+ return library
+}
+
+type Record struct {
+ total int
+ current int
+}
+
+type LocalLibrary struct {
+ collection map[string]*Book
+ librarians int
+ inventar map[string]*Record
+}
+
+func (library LocalLibrary) AddBook(book *Book) (int, error) {
+
+ if value, ok := library.inventar[book.Isbn]; ok {
+ if value.total > 3 {
+ return value.total, errors.New(fmt.Sprintf("Има 4 копия на книга %s", book.Isbn))
+ } else {
+ library.inventar[book.Isbn].total++
+ library.inventar[book.Isbn].current++
+ }
+ }
+
+ if _, ok := library.inventar[book.Isbn]; !ok {
+ library.collection[book.Isbn] = book
+ library.inventar[book.Isbn] = &Record{1, 1}
+ }
+
+ return library.inventar[book.Isbn].total, nil
+}
+
+func (library LocalLibrary) AddBookJSON(data []byte) (int, error) {
+ var book Book
+ json.Unmarshal(data, &book)
+
+ return library.AddBook(&book)
+}
+
+func (library LocalLibrary) AddBookXML(data []byte) (int, error) {
+ var book Book
+ xml.Unmarshal(data, &book)
+
+ return library.AddBook(&book)
+}
+
+func (library LocalLibrary) Hello() (chan<- LibraryRequest, <-chan LibraryResponse) {
+ request := make(chan LibraryRequest)
+ response := make(chan LibraryResponse)
+ go func(req chan LibraryRequest, res chan LibraryResponse) {
+ var newreq LibraryRequest
+ newreq = <-req
+ switch {
+ case newreq.GetType() == BorrowBook:
+ var asw LibraryResponse
+ if val, ok := library.inventar[newreq.GetISBN()]; ok {
+ book := library.collection[newreq.GetISBN()]
+ if val.current != 0 {
+ library.inventar[newreq.GetISBN()].current--
+ asw = Response{
+ library.inventar[newreq.GetISBN()].total,
+ library.inventar[newreq.GetISBN()].current,
+ stringer{fmt.Sprintf("[%s] %s от %s",
+ book.Isbn,
+ book.Title,
+ book.Author.ToString(),
+ )}, nil}
+ } else {
+ asw = Response{
+ library.inventar[newreq.GetISBN()].total,
+ library.inventar[newreq.GetISBN()].current,
+ stringer{fmt.Sprintf("[%s] %s от %s",
+ book.Isbn,
+ book.Title,
+ book.Author.ToString())},
+ errors.New(fmt.Sprintf("Няма наличност на книга %s)", book.Isbn)),
+ }
+ }
+ } else {
+ asw = Response{0, 0, stringer{""},
+ errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN()))}
+ }
+ res <- asw
+
+ case newreq.GetType() == ReturnBook:
+ var asw LibraryResponse
+ if _, ok := library.inventar[newreq.GetISBN()]; ok {
+ library.inventar[newreq.GetISBN()].current++
+ asw = Response{
+ library.inventar[newreq.GetISBN()].total,
+ library.inventar[newreq.GetISBN()].current,
+ stringer{""},
+ nil,
+ }
+ } else {
+ asw = Response{0, 0, stringer{""},
+ errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
+ }
+ }
+ res <- asw
+ case newreq.GetType() == GetAvailability:
+ var asw LibraryResponse
+ if _, ok := library.inventar[newreq.GetISBN()]; ok {
+ book := library.collection[newreq.GetISBN()]
+ library.inventar[newreq.GetISBN()].current++
+ asw = Response{
+ library.inventar[newreq.GetISBN()].total,
+ library.inventar[newreq.GetISBN()].current,
+ stringer{fmt.Sprintf("[%s] %s от %s",
+ book.Isbn,
+ book.Title,
+ book.Author.ToString(),
+ )},
+ nil,
+ }
+ } else {
+ asw = Response{0, 0, stringer{""},
+ errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
+ }
+ }
+ res <- asw
+ }
+ }(request, response)
+ return request, response
+}
+
+type Library interface {
+
+ AddBookJSON(data []byte) (int, error)
+
+ AddBookXML(data []byte) (int, error)
+
+ Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
+}
+
+type Request struct {
+ Type int
+ Isbn string
+}
+
+func (rq Request) GetISBN() string {
+ return rq.Isbn
+}
+
+func (rq Request) SetIsbn(isbn string) {
+ rq.Isbn = isbn
+}
+
+type LibraryRequest interface {
+
+ GetType() int
+
+ GetISBN() string
+}
+
+type stringer struct {
+ data string
+}
+
+func (s stringer) String() string { return s.data }
+
+type Response struct {
+ registered, available int
+ bookInfo stringer
+ err error
+}
+
+func (rp Response) GetBook() (fmt.Stringer, error) {
+ return rp.bookInfo, rp.err
+}
+
+func (rp Response) GetAvailability() (available int, registered int) {
+ return rp.available, rp.registered
+}
+
+type LibraryResponse interface {
+
+ GetBook() (fmt.Stringer, error)
+
+ GetAvailability() (available int, registered int)
+}

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

package main
import "fmt"
import "encoding/xml"
import "encoding/json"
import "errors"
const (
_ = iota
BorrowBook
ReturnBook
GetAvailability
)
type Author struct {
First_name string `xml:"first_name"`
Last_name string `xml:"last_name"`
}
func (author Author) ToString() string {
return author.First_name + " " + author.Last_name
}
type Book struct {
XMLNAME xml.Name `xml:"book"`
Isbn string `xml:"isbn,attr"`
Title string `xml:"title"`
Author Author `xml:"author"`
Genre string `xml:"genre"`
Pages int `xml:"pages"`
Ratings []int `xml:"ratings>rating"`
}
// Връща "отворена" библиотека
func NewLibrary(librarians int) Library {
var library Library
coll := make(map[string]*Book)
inv := make(map[string]*Record)
library = LocalLibrary{coll, librarians, inv}
return library
}
type Record struct {
total int
current int
}
type LocalLibrary struct {
collection map[string]*Book
librarians int
inventar map[string]*Record
}
func (library LocalLibrary) AddBook(book *Book) (int, error) {
if value, ok := library.inventar[book.Isbn]; ok {
if value.total > 3 {
return value.total, errors.New(fmt.Sprintf("Има 4 копия на книга %s", book.Isbn))
} else {
library.inventar[book.Isbn].total++
library.inventar[book.Isbn].current++
}
}
if _, ok := library.inventar[book.Isbn]; !ok {
library.collection[book.Isbn] = book
library.inventar[book.Isbn] = &Record{1, 1}
}
return library.inventar[book.Isbn].total, nil
}
func (library LocalLibrary) AddBookJSON(data []byte) (int, error) {
var book Book
json.Unmarshal(data, &book)
return library.AddBook(&book)
}
func (library LocalLibrary) AddBookXML(data []byte) (int, error) {
var book Book
xml.Unmarshal(data, &book)
return library.AddBook(&book)
}
func (library LocalLibrary) Hello() (chan<- LibraryRequest, <-chan LibraryResponse) {
- request := make(chan LibraryRequest)
- response := make(chan LibraryResponse)
+ request := make(chan LibraryRequest, library.librarians)
+ response := make(chan LibraryResponse, library.librarians)
go func(req chan LibraryRequest, res chan LibraryResponse) {
var newreq LibraryRequest
newreq = <-req
switch {
case newreq.GetType() == BorrowBook:
var asw LibraryResponse
if val, ok := library.inventar[newreq.GetISBN()]; ok {
book := library.collection[newreq.GetISBN()]
if val.current != 0 {
library.inventar[newreq.GetISBN()].current--
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString(),
)}, nil}
} else {
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString())},
errors.New(fmt.Sprintf("Няма наличност на книга %s)", book.Isbn)),
}
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN()))}
}
res <- asw
case newreq.GetType() == ReturnBook:
var asw LibraryResponse
if _, ok := library.inventar[newreq.GetISBN()]; ok {
library.inventar[newreq.GetISBN()].current++
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{""},
nil,
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
}
}
res <- asw
case newreq.GetType() == GetAvailability:
var asw LibraryResponse
if _, ok := library.inventar[newreq.GetISBN()]; ok {
book := library.collection[newreq.GetISBN()]
library.inventar[newreq.GetISBN()].current++
asw = Response{
library.inventar[newreq.GetISBN()].total,
library.inventar[newreq.GetISBN()].current,
stringer{fmt.Sprintf("[%s] %s от %s",
book.Isbn,
book.Title,
book.Author.ToString(),
)},
nil,
}
} else {
asw = Response{0, 0, stringer{""},
errors.New(fmt.Sprintf("Непозната книга %s)", newreq.GetISBN())),
}
}
res <- asw
}
}(request, response)
return request, response
}
type Library interface {
AddBookJSON(data []byte) (int, error)
AddBookXML(data []byte) (int, error)
Hello() (chan<- LibraryRequest, <-chan LibraryResponse)
}
type Request struct {
Type int
Isbn string
}
func (rq Request) GetISBN() string {
return rq.Isbn
}
func (rq Request) SetIsbn(isbn string) {
rq.Isbn = isbn
}
type LibraryRequest interface {
GetType() int
GetISBN() string
}
type stringer struct {
data string
}
func (s stringer) String() string { return s.data }
type Response struct {
registered, available int
bookInfo stringer
err error
}
func (rp Response) GetBook() (fmt.Stringer, error) {
return rp.bookInfo, rp.err
}
func (rp Response) GetAvailability() (available int, registered int) {
return rp.available, rp.registered
}
type LibraryResponse interface {
GetBook() (fmt.Stringer, error)
GetAvailability() (available int, registered int)
}