From ece775c129562dec83525a1a77d4f94c3a409643 Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 19 May 2019 23:48:02 -0600 Subject: [PATCH 1/2] day 263: valid sentence finder --- day263/problem.go | 46 +++++++++++++++++++++++++++++++ day263/problem_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 day263/problem.go create mode 100644 day263/problem_test.go diff --git a/day263/problem.go b/day263/problem.go new file mode 100644 index 0000000..5ec5fb6 --- /dev/null +++ b/day263/problem.go @@ -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') +} diff --git a/day263/problem_test.go b/day263/problem_test.go new file mode 100644 index 0000000..1db1bb6 --- /dev/null +++ b/day263/problem_test.go @@ -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) + } + } +} From 508f58992761479349423b8d84316fa65b02ab1e Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 19 May 2019 23:48:38 -0600 Subject: [PATCH 2/2] add day 263 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4c3e462..9af05f1 100644 --- a/README.md +++ b/README.md @@ -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)