Skip to content

Commit cf8914a

Browse files
committed
Solve '412. Fizz Buzz'
1 parent c04b594 commit cf8914a

File tree

4 files changed

+110
-3
lines changed

4 files changed

+110
-3
lines changed

1-two-sum/sum_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ func Test_twoSum(t *testing.T) {
4444
{
4545
name: "no target sum",
4646
args: args{
47-
nums: []int{9,2,99},
47+
nums: []int{9, 2, 99},
4848
target: 1000,
4949
},
5050
want: nil,
5151
},
5252
{
5353
name: "3,2,4",
5454
args: args{
55-
nums: []int{3,2,4},
55+
nums: []int{3, 2, 4},
5656
target: 6,
5757
},
58-
want: []int{1,2},
58+
want: []int{1, 2},
5959
},
6060
}
6161

412.-fizz-buzz/fizz.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fizz
2+
3+
import "strconv"
4+
5+
func fizzBuzz(n int) []string {
6+
res := make([]string, n)
7+
for i := 1; i <= n; i++ {
8+
if i%15 == 0 {
9+
res[i-1] = "FizzBuzz"
10+
continue
11+
}
12+
13+
if i%3 == 0 {
14+
res[i-1] = "Fizz"
15+
continue
16+
}
17+
18+
if i%5 == 0 {
19+
res[i-1] = "Buzz"
20+
continue
21+
}
22+
23+
res[i-1] = strconv.Itoa(i)
24+
}
25+
26+
return res
27+
}

412.-fizz-buzz/fizz_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fizz
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_fizzBuzz(t *testing.T) {
10+
type args struct {
11+
n int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want []string
17+
}{
18+
{
19+
name: "",
20+
args: args{
21+
n: 15,
22+
},
23+
want: []string{
24+
"1",
25+
"2",
26+
"Fizz",
27+
"4",
28+
"Buzz",
29+
"Fizz",
30+
"7",
31+
"8",
32+
"Fizz",
33+
"Buzz",
34+
"11",
35+
"Fizz",
36+
"13",
37+
"14",
38+
"FizzBuzz",
39+
},
40+
},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
got := fizzBuzz(tt.args.n)
45+
assert.Equal(t, tt.want, got)
46+
})
47+
}
48+
}

412.-fizz-buzz/spec.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 412. Fizz Buzz
2+
3+
<https://leetcode.com/problems/fizz-buzz/>
4+
5+
Write a program that outputs the string representation of numbers from 1 to n.
6+
7+
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
8+
9+
Example:
10+
11+
```text
12+
n = 15,
13+
14+
Return:
15+
[
16+
"1",
17+
"2",
18+
"Fizz",
19+
"4",
20+
"Buzz",
21+
"Fizz",
22+
"7",
23+
"8",
24+
"Fizz",
25+
"Buzz",
26+
"11",
27+
"Fizz",
28+
"13",
29+
"14",
30+
"FizzBuzz"
31+
]
32+
```

0 commit comments

Comments
 (0)