Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ problems from
* [Day 257](https://github.com/vaskoz/dailycodingproblem-go/issues/529)
* [Day 258](https://github.com/vaskoz/dailycodingproblem-go/issues/530)
* [Day 260](https://github.com/vaskoz/dailycodingproblem-go/issues/533)
* [Day 263](https://github.com/vaskoz/dailycodingproblem-go/issues/539)
* [Day 266](https://github.com/vaskoz/dailycodingproblem-go/issues/542)
* [Day 268](https://github.com/vaskoz/dailycodingproblem-go/issues/544)
* [Day 269](https://github.com/vaskoz/dailycodingproblem-go/issues/547)
Expand Down
46 changes: 46 additions & 0 deletions day263/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package day263

import "strings"

// ValidSentences parses text and returns the valid sentences only
// as defined by the problem.
func ValidSentences(text string) []string {
var sentences []string
var start int
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')
}
61 changes: 61 additions & 0 deletions day263/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package day263

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)
}
}
}