Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

word-count: check counting of empty words #1446

Merged
merged 3 commits into from
Jan 31, 2019
Merged

Conversation

tehsphinx
Copy link
Member

Ran across the following solution on the go track that will count empty words on some inputs:

package wordcount

import (
	"strings"
	"unicode"
)

// Frequency is the structure of words frequency
type Frequency map[string]int

// WordCount counts the occurrences of each word in a given phrase
func WordCount(input string) Frequency {
	counter := Frequency{}
	phrase := strings.FieldsFunc(input, split)

	for _, word := range phrase {
		word = normalize(word)
		counter[word]++
	}

	return counter
}

func normalize(word string) string {
	word = strings.ToLower(word)

	return strings.TrimFunc(word, func(r rune) bool {
		return !unicode.IsLetter(r) && !unicode.IsNumber(r)
	})
}

func split(r rune) bool {
	return r == ' ' || r == ','
}

The added test catches that behaviour.

Copy link
Member

@rpottsoh rpottsoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change at end of file also need to update "version", see Travis CI output.

exercises/word-count/canonical-data.json Show resolved Hide resolved
@rpottsoh
Copy link
Member

also need to update "version", see Travis CI output.

See https://github.com/exercism/problem-specifications#test-data-versioning

@rpottsoh rpottsoh merged commit d793398 into exercism:master Jan 31, 2019
@tehsphinx tehsphinx deleted the patch-1 branch January 31, 2019 20:09
tejasbubane added a commit to tejasbubane/haskell that referenced this pull request Mar 25, 2019
sshine pushed a commit to exercism/haskell that referenced this pull request Mar 26, 2019
marko213 added a commit to marko213/exercism-cpp that referenced this pull request Jul 30, 2019
Adds three test cases:

* substrings_from_the_beginning (exercism/problem-specifications#1557)
* multiple_spaces_not_detected_as_a_word (exercism/problem-specifications#1023)
* alternating_word_separators_not_detected_as_a_word (exercism/problem-specifications#1446)
arcuru pushed a commit to exercism/cpp that referenced this pull request Aug 5, 2019
* Word-count - update tests specification to version 1.4.0

Adds three test cases:

* substrings_from_the_beginning (exercism/problem-specifications#1557)
* multiple_spaces_not_detected_as_a_word (exercism/problem-specifications#1023)
* alternating_word_separators_not_detected_as_a_word (exercism/problem-specifications#1446)

* Word-count - change test cases to be more like the specification (1.4.0)

This reorders and removes some tests to match the specification in version 1.4.0

* Word-count - make empty lines uniform in test cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants