Skip to content

Commit

Permalink
Merge 15596af into c88d5fa
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 2, 2020
2 parents c88d5fa + 15596af commit 16e5ec4
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ problems from
* [Day 427](https://github.com/vaskoz/dailycodingproblem-go/issues/861)
* [Day 428](https://github.com/vaskoz/dailycodingproblem-go/issues/864)
* [Day 429](https://github.com/vaskoz/dailycodingproblem-go/issues/866)
* [Day 431](https://github.com/vaskoz/dailycodingproblem-go/issues/882)
* [Day 433](https://github.com/vaskoz/dailycodingproblem-go/issues/879)
* [Day 434](https://github.com/vaskoz/dailycodingproblem-go/issues/870)
* [Day 435](https://github.com/vaskoz/dailycodingproblem-go/issues/868)
Expand Down
55 changes: 55 additions & 0 deletions day431/problem.go
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')
}
62 changes: 62 additions & 0 deletions day431/problem_test.go
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)
}
}
}

0 comments on commit 16e5ec4

Please sign in to comment.