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 031cea6

Browse files
author
Openset
committed
Add: Detect Capital
1 parent dfd0838 commit 031cea6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
package detect_capital
2+
3+
import "unicode"
4+
5+
func detectCapitalUse(word string) bool {
6+
count := 0
7+
for _, c := range word {
8+
if unicode.IsUpper(c) {
9+
count++
10+
}
11+
}
12+
return count == 0 || count == len(word) || (count == 1 && unicode.IsUpper(rune(word[0])))
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
package detect_capital
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected bool
8+
}
9+
10+
func TestDetectCapitalUse(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "USA",
14+
expected: true,
15+
},
16+
{
17+
input: "leetcode",
18+
expected: true,
19+
},
20+
{
21+
input: "Google",
22+
expected: true,
23+
},
24+
{
25+
input: "FlaG",
26+
expected: false,
27+
},
28+
}
29+
for _, tc := range tests {
30+
output := detectCapitalUse(tc.input)
31+
if output != tc.expected {
32+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

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