Skip to content

word-count: 'javascript!!&@$%^&' should be not accepted as a word #177

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

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 6 additions & 4 deletions word-count/example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from collections import Counter

import re

def word_count(text):
"""Return a Counter object that maps from the words contained in
the phrase to their respective counts
"""
return Counter(text.split())

count = {}
for w in re.split('\W',re.sub('[^A-Za-z0-9]',' ', text)):
if w:
count[w] = count.get(w, 0) + 1
return count
10 changes: 5 additions & 5 deletions word-count/word_count_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def test_count_multiple_occurences(self):
word_count('one fish two fish red fish blue fish')
)

def test_preserves_punctuation(self):
def test_ignores_punctuation(self):
self.assertEqual(
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, ':': 2, 'javascript!!&@$%^&': 1},
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1},
word_count('car : carpet as java : javascript!!&@$%^&')
)

Expand All @@ -39,16 +39,16 @@ def test_mixed_case(self):
{'go': 1, 'Go': 1, 'GO': 1},
word_count('go Go GO')
)

def test_multiple_spaces(self):
self.assertEqual(
{'wait': 1, 'for': 1, 'it': 1},
word_count('wait for it')
)

def test_newlines(self):
self.assertEqual(
{'rah': 2, 'ah': 3, 'roma': 2, 'ma': 1, 'ga': 2, 'oh': 1, 'la': 2,
{'rah': 2, 'ah': 3, 'roma': 2, 'ma': 1, 'ga': 2, 'oh': 1, 'la': 2,
'want': 1, 'your': 1, 'bad': 1, 'romance': 1},
word_count('rah rah ah ah ah\nroma roma ma\nga ga oh la la\nwant your bad romance')
)
Expand Down