-
Notifications
You must be signed in to change notification settings - Fork 32
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
Showing
3 changed files
with
118 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
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,55 @@ | ||
package day431 | ||
|
||
import "strings" | ||
|
||
// ValidSentences parses text and returns the valid sentences only | ||
// as defined by the problem. | ||
func ValidSentences(text string) []string { | ||
sentences := make([]string, 0) | ||
start := 0 | ||
|
||
spaceDelimited := strings.Split(text, " ") | ||
possibleSentence := false | ||
|
||
for i, part := range spaceDelimited { | ||
runes := []rune(part) | ||
if part == "" || !isValid(runes) { | ||
possibleSentence = false | ||
continue | ||
} | ||
|
||
if runes[0] >= 'A' && runes[0] <= 'Z' { | ||
possibleSentence = true | ||
start = i | ||
|
||
continue | ||
} | ||
|
||
if last := runes[len(runes)-1]; last == '.' || last == '!' || last == '?' { | ||
if possibleSentence { | ||
sentences = append(sentences, strings.Join(spaceDelimited[start:i+1], " ")) | ||
} | ||
} | ||
} | ||
|
||
return sentences | ||
} | ||
|
||
func isValid(runes []rune) bool { | ||
first := runes[0] | ||
if !((first >= 'a' && first <= 'z') || (first >= 'A' && first <= 'Z')) { | ||
return false | ||
} else if len(runes) == 1 { | ||
return true | ||
} | ||
|
||
for i := 1; i < len(runes)-1; i++ { | ||
if char := runes[i]; char < 'a' || char > 'z' { | ||
return false | ||
} | ||
} | ||
|
||
last := runes[len(runes)-1] | ||
|
||
return strings.ContainsRune(",.!?;:", last) || (last >= 'a' && last <= 'z') | ||
} |
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,62 @@ | ||
package day431 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// nolint | ||
var testcases = []struct { | ||
text string | ||
sentences []string | ||
}{ | ||
{ | ||
"this is not valid. However, this sentence is totally valid. Is it not?", | ||
[]string{ | ||
"However, this sentence is totally valid.", | ||
"Is it not?", | ||
}, | ||
}, | ||
{ | ||
"Numbers like 9 and extra spaces. A bee bit me!", | ||
[]string{ | ||
"A bee bit me!", | ||
}, | ||
}, | ||
{ | ||
"A bee!", | ||
[]string{ | ||
"A bee!", | ||
}, | ||
}, | ||
{ | ||
"The colors: red, white and blue. I can not : space punctuations.", | ||
[]string{ | ||
"The colors: red, white and blue.", | ||
}, | ||
}, | ||
{ | ||
"I do not allow CamelCase. It is not good for anybody, right?", | ||
[]string{ | ||
"It is not good for anybody, right?", | ||
}, | ||
}, | ||
} | ||
|
||
func TestValidSentences(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
if result := ValidSentences(tc.text); !reflect.DeepEqual(result, tc.sentences) { | ||
t.Errorf("Expected %v, got %v", tc.sentences, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkValidSentences(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
ValidSentences(tc.text) | ||
} | ||
} | ||
} |