Решение на Фабрика за регулярни изрази от Красимир Стойков

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

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

Резултати

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

Код

package main
import (
"errors"
"regexp"
"strconv"
)
const (
DOES_NOT_EXIST = iota
ENQUEUED
IN_PROGRESS
UNABLE_TO_PRODUCE
DONE
)
var IdCounter int
type Order struct {
Id string
Status int
Words []string
Result string
Channel chan *Order
}
func NewOrder(words []string, channel chan *Order) *Order {
IdCounter++
return &Order{strconv.Itoa(IdCounter), 1, words, "", channel}
}
type Factory struct {
freeWorkers uint8
orders []*Order
nextOrder int
storage []string
storageDoc []bool
}
func NewFactory(workers uint8) *Factory {
return &Factory{workers, make([]*Order, workers), 0, make([]string, workers*2), make([]bool, workers*2)}
}
func (f *Factory) Enqueue(order *Order) {
order.Status = ENQUEUED
f.orders = append(f.orders, order)
}
func (f *Factory) StartProducing() {
}
func (f *Factory) StopProducing() {
}
func (f *Factory) StorageAdd(materials map[string]uint16) {
for k, v := range materials {
for i := 0; i < int(v); i++ {
f.storage = append(f.storage, k)
f.storageDoc = append(f.storageDoc, true)
}
}
}
func (f *Factory) generateRegexp(words []string) (string, error) {
element := ""
for k, v := range f.storage {
if !f.storageDoc[k] {
continue
} else {
element = v
removeR(&element)
reg, err := regexp.Compile(element)
if err != nil {
continue
}
for _, w := range words {
if !reg.MatchString(w) {
continue
}
removeR(&element)
return "`" + "^" + element + "$" + "`", nil
}
}
}
return "", errors.New("Not enough materials")
}
func removeR(word *string) {
if (*word)[0] == '`' {
(*word) = (*word)[1:]
}
if (*word)[len(*word)] == '`' {
(*word) = (*word)[:len(*word)-1]
}
}
type Client struct {
Name string
Orders map[string]*Order
Channel chan *Order
}
func NewClient(name string) *Client {
return &Client{name, map[string]*Order{}, make(chan *Order)}
}
func (c *Client) Order(factory *Factory, words []string) string {
order := NewOrder(words, c.Channel)
c.Orders[order.Id] = order
factory.Enqueue(order)
return order.Id
}
func (c *Client) CheckStatus(id string) int {
for _, v := range c.Orders {
if v.Id == id {
return v.Status
}
}
return 0
}

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

PASS
ok  	_/tmp/d20150111-18989-1m0fcrw	0.003s
PASS
ok  	_/tmp/d20150111-18989-1m0fcrw	0.003s
PASS
ok  	_/tmp/d20150111-18989-1m0fcrw	0.003s
--- FAIL: TestUnableToProcess (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 22 [running]:
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:279 +0xe9
testing.func·006()
	/usr/local/go/src/pkg/testing/testing.go:416 +0x12d
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:248 +0x176
_/tmp/d20150111-18989-1m0fcrw.(*Factory).generateRegexp(0x1841a2a0, 0xb7e08f70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
	/tmp/d20150111-18989-1m0fcrw/solution.go:68 +0x43e
_/tmp/d20150111-18989-1m0fcrw.TestUnableToProcess(0x18404360)
	/tmp/d20150111-18989-1m0fcrw/solution_test.go:44 +0x202
testing.tRunner(0x18404360, 0x81e52e4)
	/usr/local/go/src/pkg/testing/testing.go:422 +0x87
created by testing.RunTests
	/usr/local/go/src/pkg/testing/testing.go:504 +0x720

goroutine 16 [chan receive]:
testing.RunTests(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x8116a01)
	/usr/local/go/src/pkg/testing/testing.go:505 +0x75c
testing.Main(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x81eb520, 0x0, 0x0, 0x81eb520, 0x0, 0x0)
	/usr/local/go/src/pkg/testing/testing.go:435 +0x6e
main.main()
	_/tmp/d20150111-18989-1m0fcrw/_test/_testmain.go:63 +0x86

goroutine 19 [finalizer wait]:
runtime.park(0x8059a90, 0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1369 +0x94
runtime.parkunlock(0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1385 +0x3f
runfinq()
	/usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xc5
runtime.goexit()
	/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2
FAIL	_/tmp/d20150111-18989-1m0fcrw	0.004s
--- FAIL: TestProcessOneWord (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 21 [running]:
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:279 +0xe9
testing.func·006()
	/usr/local/go/src/pkg/testing/testing.go:416 +0x12d
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:248 +0x176
_/tmp/d20150111-18989-1m0fcrw.(*Factory).generateRegexp(0x1841a270, 0xb7eeef70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
	/tmp/d20150111-18989-1m0fcrw/solution.go:68 +0x43e
_/tmp/d20150111-18989-1m0fcrw.TestProcessOneWord(0x18404240)
	/tmp/d20150111-18989-1m0fcrw/solution_test.go:58 +0x20c
testing.tRunner(0x18404240, 0x81e52f0)
	/usr/local/go/src/pkg/testing/testing.go:422 +0x87
created by testing.RunTests
	/usr/local/go/src/pkg/testing/testing.go:504 +0x720

goroutine 16 [chan receive]:
testing.RunTests(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x8116a01)
	/usr/local/go/src/pkg/testing/testing.go:505 +0x75c
testing.Main(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x81eb520, 0x0, 0x0, 0x81eb520, 0x0, 0x0)
	/usr/local/go/src/pkg/testing/testing.go:435 +0x6e
main.main()
	_/tmp/d20150111-18989-1m0fcrw/_test/_testmain.go:63 +0x86

goroutine 19 [finalizer wait]:
runtime.park(0x8059a90, 0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1369 +0x94
runtime.parkunlock(0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1385 +0x3f
runfinq()
	/usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xc5
runtime.goexit()
	/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2
FAIL	_/tmp/d20150111-18989-1m0fcrw	0.003s
--- FAIL: TestProcessWithoutEnoughMaterials (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 21 [running]:
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:279 +0xe9
testing.func·006()
	/usr/local/go/src/pkg/testing/testing.go:416 +0x12d
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:248 +0x176
_/tmp/d20150111-18989-1m0fcrw.(*Factory).generateRegexp(0x1841a300, 0xb7e4bf70, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
	/tmp/d20150111-18989-1m0fcrw/solution.go:68 +0x43e
_/tmp/d20150111-18989-1m0fcrw.TestProcessWithoutEnoughMaterials(0x18404240)
	/tmp/d20150111-18989-1m0fcrw/solution_test.go:73 +0x20c
testing.tRunner(0x18404240, 0x81e52fc)
	/usr/local/go/src/pkg/testing/testing.go:422 +0x87
created by testing.RunTests
	/usr/local/go/src/pkg/testing/testing.go:504 +0x720

goroutine 16 [chan receive]:
testing.RunTests(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x8116a01)
	/usr/local/go/src/pkg/testing/testing.go:505 +0x75c
testing.Main(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x81eb520, 0x0, 0x0, 0x81eb520, 0x0, 0x0)
	/usr/local/go/src/pkg/testing/testing.go:435 +0x6e
main.main()
	_/tmp/d20150111-18989-1m0fcrw/_test/_testmain.go:63 +0x86

goroutine 19 [finalizer wait]:
runtime.park(0x8059a90, 0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1369 +0x94
runtime.parkunlock(0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1385 +0x3f
runfinq()
	/usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xc5
runtime.goexit()
	/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2
FAIL	_/tmp/d20150111-18989-1m0fcrw	0.003s
--- FAIL: TestOrderWithSeveralWords (0.00 seconds)
panic: runtime error: index out of range [recovered]
	panic: runtime error: index out of range

goroutine 21 [running]:
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:279 +0xe9
testing.func·006()
	/usr/local/go/src/pkg/testing/testing.go:416 +0x12d
runtime.panic(0x81391c0, 0x81e6f9c)
	/usr/local/go/src/pkg/runtime/panic.c:248 +0x176
_/tmp/d20150111-18989-1m0fcrw.(*Factory).generateRegexp(0x1841a270, 0xb7e87f78, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0)
	/tmp/d20150111-18989-1m0fcrw/solution.go:68 +0x43e
_/tmp/d20150111-18989-1m0fcrw.TestOrderWithSeveralWords(0x18404240)
	/tmp/d20150111-18989-1m0fcrw/solution_test.go:100 +0x20c
testing.tRunner(0x18404240, 0x81e5308)
	/usr/local/go/src/pkg/testing/testing.go:422 +0x87
created by testing.RunTests
	/usr/local/go/src/pkg/testing/testing.go:504 +0x720

goroutine 16 [chan receive]:
testing.RunTests(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x8116a01)
	/usr/local/go/src/pkg/testing/testing.go:505 +0x75c
testing.Main(0x817d39c, 0x81e52c0, 0x9, 0x9, 0x81eb520, 0x0, 0x0, 0x81eb520, 0x0, 0x0)
	/usr/local/go/src/pkg/testing/testing.go:435 +0x6e
main.main()
	_/tmp/d20150111-18989-1m0fcrw/_test/_testmain.go:63 +0x86

goroutine 19 [finalizer wait]:
runtime.park(0x8059a90, 0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1369 +0x94
runtime.parkunlock(0x81e98c0, 0x81e8ca9)
	/usr/local/go/src/pkg/runtime/proc.c:1385 +0x3f
runfinq()
	/usr/local/go/src/pkg/runtime/mgc0.c:2644 +0xc5
runtime.goexit()
	/usr/local/go/src/pkg/runtime/proc.c:1445
exit status 2
FAIL	_/tmp/d20150111-18989-1m0fcrw	0.004s
PASS
ok  	_/tmp/d20150111-18989-1m0fcrw	0.003s
PASS
ok  	_/tmp/d20150111-18989-1m0fcrw	0.003s

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

Красимир обнови решението на 31.12.2014 23:59 (преди над 3 години)

+package main
+
+import (
+ "errors"
+ "regexp"
+ "strconv"
+)
+
+const (
+ DOES_NOT_EXIST = iota
+ ENQUEUED
+ IN_PROGRESS
+ UNABLE_TO_PRODUCE
+ DONE
+)
+
+var IdCounter int
+
+type Order struct {
+ Id string
+ Status int
+ Words []string
+ Result string
+ Channel chan *Order
+}
+
+func NewOrder(words []string, channel chan *Order) *Order {
+ IdCounter++
+ return &Order{strconv.Itoa(IdCounter), 1, words, "", channel}
+}
+
+type Factory struct {
+ freeWorkers uint8
+ orders []*Order
+ nextOrder int
+ storage []string
+ storageDoc []bool
+}
+
+func NewFactory(workers uint8) *Factory {
+ return &Factory{workers, make([]*Order, workers), 0, make([]string, workers*2), make([]bool, workers*2)}
+}
+func (f *Factory) Enqueue(order *Order) {
+ order.Status = ENQUEUED
+ f.orders = append(f.orders, order)
+}
+func (f *Factory) StartProducing() {
+
+}
+func (f *Factory) StopProducing() {
+
+}
+func (f *Factory) StorageAdd(materials map[string]uint16) {
+ for k, v := range materials {
+ for i := 0; i < int(v); i++ {
+ f.storage = append(f.storage, k)
+ f.storageDoc = append(f.storageDoc, true)
+ }
+ }
+}
+func (f *Factory) generateRegexp(words []string) (string, error) {
+ element := ""
+ for k, v := range f.storage {
+ if !f.storageDoc[k] {
+ continue
+ } else {
+ element = v
+ removeR(&element)
+ reg, err := regexp.Compile(element)
+ if err != nil {
+ continue
+ }
+ for _, w := range words {
+ if !reg.MatchString(w) {
+ continue
+ }
+ removeR(&element)
+ return "`" + "^" + element + "$" + "`", nil
+ }
+ }
+
+ }
+ return "", errors.New("Not enough materials")
+}
+func removeR(word *string) {
+ if (*word)[0] == '`' {
+ (*word) = (*word)[1:]
+ }
+ if (*word)[len(*word)] == '`' {
+ (*word) = (*word)[:len(*word)-1]
+ }
+}
+
+type Client struct {
+ Name string
+ Orders map[string]*Order
+ Channel chan *Order
+}
+
+func NewClient(name string) *Client {
+ return &Client{name, map[string]*Order{}, make(chan *Order)}
+}
+func (c *Client) Order(factory *Factory, words []string) string {
+ order := NewOrder(words, c.Channel)
+ c.Orders[order.Id] = order
+ factory.Enqueue(order)
+ return order.Id
+}
+func (c *Client) CheckStatus(id string) int {
+ for _, v := range c.Orders {
+ if v.Id == id {
+ return v.Status
+ }
+ }
+ return 0
+}