From fbb622e0cbfae9598edf39ffbd87a2efc512fd9f Mon Sep 17 00:00:00 2001 From: Nathan Parsons Date: Thu, 26 Oct 2017 22:29:10 +0100 Subject: [PATCH 1/2] Update example to work with new tests --- exercises/word-count/example.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exercises/word-count/example.py b/exercises/word-count/example.py index 995a7d6ca83..bca341c5401 100644 --- a/exercises/word-count/example.py +++ b/exercises/word-count/example.py @@ -1,8 +1,10 @@ +import re + from collections import Counter +WORDS = re.compile("[a-zA-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).lower() for word in WORDS.finditer(text)) From 26e8a9321ba8539ac6dfcf9455c9fc90e5b66370 Mon Sep 17 00:00:00 2001 From: Nathan Parsons Date: Fri, 27 Oct 2017 12:03:26 +0100 Subject: [PATCH 2/2] word-count: Refactor example code slightly --- exercises/word-count/example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/word-count/example.py b/exercises/word-count/example.py index bca341c5401..9e93a159111 100644 --- a/exercises/word-count/example.py +++ b/exercises/word-count/example.py @@ -3,8 +3,8 @@ from collections import Counter -WORDS = re.compile("[a-zA-Z0-9]+(['][a-z]+)?") +WORDS = re.compile("[a-z0-9]+(['][a-z]+)?") def word_count(text): - return Counter(word.group(0).lower() for word in WORDS.finditer(text)) + return Counter(word.group(0) for word in WORDS.finditer(text.lower()))