Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9baaaf5

Browse files
author
Shuo
committed
Add: Generate a String With Characters That Have Odd Counts
1 parent ad29f32 commit 9baaaf5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package problem1374
2+
3+
func generateTheString(n int) string {
4+
b := make([]byte, n)
5+
for i := 0; i < n; i++ {
6+
b[i] = 'x'
7+
}
8+
if n%2 == 0 {
9+
b[n-1] = 'y'
10+
}
11+
return string(b)
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem1374
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in int
7+
want string
8+
}
9+
10+
func TestGenerateTheString(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: 4,
14+
want: "xxxy",
15+
},
16+
{
17+
in: 2,
18+
want: "xy",
19+
},
20+
{
21+
in: 7,
22+
want: "xxxxxxx",
23+
},
24+
{
25+
in: 30,
26+
want: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxy",
27+
},
28+
}
29+
for _, tt := range tests {
30+
got := generateTheString(tt.in)
31+
if got != tt.want {
32+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /