Skip to content

Commit f8522c2

Browse files
authored
Merge pull request #1110 from 0xff-dev/1115
Add solution and test-cases for problem 1115
2 parents 2e193ea + 4ed2db1 commit f8522c2

File tree

3 files changed

+110
-63
lines changed

3 files changed

+110
-63
lines changed
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
1-
# [1. Add Sum][title]
1+
# [1115. Print FooBar Alternately][title]
22

33
## Description
44

5-
Given two binary strings, return their sum (also a binary string).
6-
7-
The input strings are both **non-empty** and contains only characters `1` or `0`.
8-
9-
**Example 1:**
10-
11-
```
12-
Input: a = "11", b = "1"
13-
Output: "100"
14-
```
15-
16-
**Example 2:**
17-
18-
```
19-
Input: a = "1010", b = "1011"
20-
Output: "10101"
5+
Suppose you are given the following code:
6+
7+
```cpp
8+
class FooBar {
9+
public void foo() {
10+
for (int i = 0; i < n; i++) {
11+
print("foo");
12+
}
13+
}
14+
15+
public void bar() {
16+
for (int i = 0; i < n; i++) {
17+
print("bar");
18+
}
19+
}
20+
}
2121
```
2222
23-
**Tags:** Math, String
23+
The same instance of `FooBar` will be passed to two different threads:
2424
25-
## 题意
26-
> 求2数之和
25+
- thread `A` will call `foo()`, while
26+
- thread `B` will call `bar()`.
2727
28-
## 题解
28+
Modify the given program to output "`foobar`" `n` times.
2929
30-
### 思路1
31-
> 。。。。
3230
33-
```go
31+
**Example 1:**
3432
33+
```
34+
Input: n = 1
35+
Output: "foobar"
36+
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
37+
"foobar" is being output 1 time.
3538
```
3639
37-
### 思路2
38-
> 思路2
39-
```go
40+
**Example 2:**
4041
42+
```
43+
Input: n = 2
44+
Output: "foobarfoobar"
45+
Explanation: "foobar" is being output 2 times.
4146
```
4247
4348
## 结语
4449
4550
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
4651
47-
[title]: https://leetcode.com/problems/two-sum/description/
52+
[title]: https://leetcode.com/problems/print-foobar-alternately
4853
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
package Solution
22

3-
func Solution_1(x bool) bool {
4-
return x
3+
type FooBar struct {
4+
n int
5+
a, b chan struct{}
56
}
67

7-
func Solution_2(x bool) bool {
8-
return x
8+
func Solution(n int) *FooBar {
9+
f := &FooBar{n: n, a: make(chan struct{}, 1), b: make(chan struct{}, 1)}
10+
f.b <- struct{}{}
11+
return f
12+
}
13+
14+
func (fb *FooBar) Foo(printFoo func()) {
15+
for i := 0; i < fb.n; i++ {
16+
// printFoo() outputs "foo". Do not change or remove this line.
17+
<-fb.b
18+
printFoo()
19+
fb.a <- struct{}{}
20+
}
21+
}
22+
23+
func (fb *FooBar) Bar(printBar func()) {
24+
for i := 0; i < fb.n; i++ {
25+
// printBar() outputs "bar". Do not change or remove this line.
26+
<-fb.a
27+
printBar()
28+
fb.b <- struct{}{}
29+
}
930
}
Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
11
package Solution
22

33
import (
4+
"bytes"
45
"fmt"
6+
"io"
57
"reflect"
6-
"runtime"
7-
"strings"
8+
"strconv"
9+
"sync"
810
"testing"
9-
10-
"github.com/stretchr/testify/assert"
1111
)
1212

13-
// Solution func Info
14-
type SolutionFuncType func(x bool) bool
15-
16-
var SolutionFuncList = []SolutionFuncType{
17-
Solution_1,
18-
Solution_2,
13+
func foo(output io.Writer) func() {
14+
return func() {
15+
fmt.Fprint(output, "foo")
16+
}
1917
}
2018

21-
// Test case info struct
22-
type Case struct {
23-
name string
24-
input bool
25-
expect bool
19+
func bar(output io.Writer) func() {
20+
return func() {
21+
fmt.Fprint(output, "bar")
22+
}
2623
}
27-
28-
// Test case
29-
var cases = []Case{
30-
{name: "TestCase 1", input: true, expect: true},
31-
{name: "TestCase 2", input: false, expect: false},
24+
func run(fb *FooBar) string {
25+
var wg sync.WaitGroup
26+
wg.Add(2)
27+
buf := bytes.NewBuffer([]byte{})
28+
go func() {
29+
defer wg.Done()
30+
fb.Foo(foo(buf))
31+
}()
32+
go func() {
33+
defer wg.Done()
34+
fb.Bar(bar(buf))
35+
}()
36+
wg.Wait()
37+
return buf.String()
3238
}
3339

34-
// TestSolution Run test case for all solutions
3540
func TestSolution(t *testing.T) {
36-
ast := assert.New(t)
41+
// 测试用例
42+
cases := []struct {
43+
name string
44+
inputs int
45+
expect string
46+
}{
47+
{"TestCase1", 1, "foobar"},
48+
{"TestCase2", 2, "foobarfoobar"},
49+
}
3750

38-
for _, f := range SolutionFuncList {
39-
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
40-
for _, c := range cases {
41-
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
42-
got := f(c.input)
43-
ast.Equal(c.expect, got,
44-
"func: %v case: %v ", funcName, c.name)
45-
})
46-
}
51+
// 开始测试
52+
for i, c := range cases {
53+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
54+
got := run(Solution(c.inputs))
55+
if !reflect.DeepEqual(got, c.expect) {
56+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
57+
c.expect, got, c.inputs)
58+
}
59+
})
4760
}
4861
}
62+
63+
// 压力测试
64+
func BenchmarkSolution(b *testing.B) {
65+
}
66+
67+
// 使用案列
68+
func ExampleSolution() {
69+
}

0 commit comments

Comments
 (0)