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 332d334

Browse files
author
Sandy
authored
Merge pull request #174 from openset/develop
Add: Number of Segments in a String
2 parents 6d43ac3 + f3d9813 commit 332d334

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
package number_of_segments_in_a_string
2+
3+
import "strings"
4+
5+
func countSegments(s string) int {
6+
return len(strings.Fields(s))
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
package number_of_segments_in_a_string
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected int
8+
}
9+
10+
func TestCountSegments(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "Hello, my name is John",
14+
expected: 5,
15+
},
16+
{
17+
input: "",
18+
expected: 0,
19+
},
20+
{
21+
input: " ",
22+
expected: 0,
23+
},
24+
{
25+
input: " abc ",
26+
expected: 1,
27+
},
28+
{
29+
input: " abc def ",
30+
expected: 2,
31+
},
32+
{
33+
input: "love live! mu'sic forever",
34+
expected: 4,
35+
},
36+
}
37+
for _, tc := range tests {
38+
output := countSegments(tc.input)
39+
if output != tc.expected {
40+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
(0)

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