add new solutions
This commit is contained in:
parent
67cad91898
commit
51975f3386
24 changed files with 933 additions and 14 deletions
35
solutions/11/q1114/solution.go
Normal file
35
solutions/11/q1114/solution.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package q1114
|
||||
|
||||
type Foo struct {
|
||||
one, two chan struct{}
|
||||
}
|
||||
|
||||
func NewFoo() *Foo {
|
||||
return &Foo{
|
||||
one: make(chan struct{}),
|
||||
two: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Foo) First(printFirst func()) {
|
||||
// Do not change this line
|
||||
printFirst()
|
||||
|
||||
close(f.one)
|
||||
}
|
||||
|
||||
func (f *Foo) Second(printSecond func()) {
|
||||
<-f.one
|
||||
|
||||
/// Do not change this line
|
||||
printSecond()
|
||||
|
||||
close(f.two)
|
||||
}
|
||||
|
||||
func (f *Foo) Third(printThird func()) {
|
||||
<-f.two
|
||||
|
||||
// Do not change this line
|
||||
printThird()
|
||||
}
|
||||
36
solutions/11/q1115/solution.go
Normal file
36
solutions/11/q1115/solution.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package q1115
|
||||
|
||||
type FooBar struct {
|
||||
n int
|
||||
foo, bar chan struct{}
|
||||
}
|
||||
|
||||
func NewFooBar(n int) *FooBar {
|
||||
ret := &FooBar{
|
||||
n: n,
|
||||
foo: make(chan struct{}, 1),
|
||||
bar: make(chan struct{}, 1),
|
||||
}
|
||||
ret.bar <- struct{}{}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (fb *FooBar) Foo(printFoo func()) {
|
||||
for i := 0; i < fb.n; i++ {
|
||||
<-fb.bar
|
||||
// printFoo() outputs "foo". Do not change or remove this line.
|
||||
printFoo()
|
||||
|
||||
fb.foo <- struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
func (fb *FooBar) Bar(printBar func()) {
|
||||
for i := 0; i < fb.n; i++ {
|
||||
<-fb.foo
|
||||
// printBar() outputs "bar". Do not change or remove this line.
|
||||
printBar()
|
||||
|
||||
fb.bar <- struct{}{}
|
||||
}
|
||||
}
|
||||
53
solutions/11/q1116/solution.go
Normal file
53
solutions/11/q1116/solution.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package q1116
|
||||
|
||||
type void struct{}
|
||||
|
||||
type ZeroEvenOdd struct {
|
||||
n int
|
||||
c0, c1, c2 chan void
|
||||
}
|
||||
|
||||
func NewZeroEvenOdd(n int) *ZeroEvenOdd {
|
||||
zeo := &ZeroEvenOdd{
|
||||
n: n,
|
||||
c0: make(chan void, 1),
|
||||
c1: make(chan void, 1),
|
||||
c2: make(chan void, 1),
|
||||
}
|
||||
zeo.c0 <- void{}
|
||||
return zeo
|
||||
}
|
||||
|
||||
func (z *ZeroEvenOdd) Zero(printNumber func(int)) {
|
||||
for i := range z.n {
|
||||
<-z.c0
|
||||
printNumber(0)
|
||||
if (i+1)%2 == 0 {
|
||||
z.c2 <- void{}
|
||||
} else {
|
||||
z.c1 <- void{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (z *ZeroEvenOdd) Even(printNumber func(int)) {
|
||||
for i := range z.n {
|
||||
if (i+1)%2 != 0 {
|
||||
continue
|
||||
}
|
||||
<-z.c2
|
||||
printNumber(i + 1)
|
||||
z.c0 <- void{}
|
||||
}
|
||||
}
|
||||
|
||||
func (z *ZeroEvenOdd) Odd(printNumber func(int)) {
|
||||
for i := range z.n {
|
||||
if (i+1)%2 == 0 {
|
||||
continue
|
||||
}
|
||||
<-z.c1
|
||||
printNumber(i + 1)
|
||||
z.c0 <- void{}
|
||||
}
|
||||
}
|
||||
38
solutions/11/q1117/solution.go
Normal file
38
solutions/11/q1117/solution.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package q1117
|
||||
|
||||
type void struct{}
|
||||
|
||||
type H2O struct {
|
||||
h, o, hRel chan void
|
||||
}
|
||||
|
||||
func NewH2O() *H2O {
|
||||
h := &H2O{
|
||||
h: make(chan void, 2),
|
||||
o: make(chan void, 2),
|
||||
hRel: make(chan void, 2),
|
||||
}
|
||||
h.o <- void{}
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *H2O) Hydrogen(releaseHydrogen func()) {
|
||||
<-h.h
|
||||
// releaseHydrogen() outputs "H". Do not change or remove this line.
|
||||
releaseHydrogen()
|
||||
|
||||
h.hRel <- void{} // confirmation
|
||||
}
|
||||
|
||||
func (h *H2O) Oxygen(releaseOxygen func()) {
|
||||
<-h.o
|
||||
|
||||
// releaseOxygen() outputs "H". Do not change or remove this line.
|
||||
releaseOxygen()
|
||||
|
||||
h.h <- void{}
|
||||
h.h <- void{}
|
||||
<-h.hRel
|
||||
<-h.hRel
|
||||
h.o <- void{}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue