8000 word-count: Update example to work with new tests by N-Parsons · Pull Request #1059 · exercism/python · GitHub
[go: up one dir, main page]

Skip to content
Merged
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 exercises/word-count/example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import re

from collections import Counter


WORDS = re.compile("[a-z0-9]+(['][a-z]+)?")


def word_count(text):
def replace_nonalpha(char):
return char.lower() if char.isalnum() else ' '
text = ''.join(replace_nonalpha(c) for c in text)
return Counter(text.split())
return Counter(word.group(0) for word in WORDS.finditer(text.lower()))
0