-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50cb959
commit 4e219fa
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package strings | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func countValidWords(sentence string) int { | ||
words := strings.Fields(sentence) | ||
cunt := 0 | ||
CuntWord: | ||
for _, word := range words { | ||
sum := 0 | ||
n := len(word) | ||
for i := 0; i < n; i++ { | ||
if unicode.IsLetter(rune(word[i])) { | ||
continue | ||
} else if unicode.IsDigit(rune(word[i])) { | ||
continue CuntWord | ||
} else if word[i] == '-' { | ||
if sum == 1 { | ||
continue CuntWord | ||
} | ||
sum++ | ||
if i == 0 || i == n-1 || !unicode.IsLetter(rune(word[i-1])) || !unicode.IsLetter(rune(word[i+1])) { | ||
continue CuntWord | ||
break | ||
} | ||
} else if i != n-1 { | ||
continue CuntWord | ||
break | ||
} | ||
} | ||
cunt++ | ||
} | ||
return cunt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package strings | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCountValidWords(t *testing.T) { | ||
str := "cat and dog" | ||
// str := "!this 1-s b8d!" | ||
res := countValidWords(str) | ||
fmt.Println(res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### 字符串相关操作方法 | ||
|
||
* strings.Fields() | ||
|
||
* strings.ContainsRune() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### 字节相关操作方法 | ||
|
||
* unicode.IsLetter | ||
|
||
* unicode.IsDigit | ||
|
||
* unicode.IsLowe |