From 93a8cc169ee0b333838999631a4646f8218fff28 Mon Sep 17 00:00:00 2001 From: Cacahuete Date: Mon, 7 Dec 2020 23:11:38 +0100 Subject: [PATCH] =?UTF-8?q?count:=20r=C3=A9solution=20de=20l'exercice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- count/count.py | 20 ++++++++++++++++++++ count/test_count.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 count/count.py create mode 100644 count/test_count.py diff --git a/count/count.py b/count/count.py new file mode 100644 index 0000000..484f53f --- /dev/null +++ b/count/count.py @@ -0,0 +1,20 @@ +import re +from collections import defaultdict + + +def count_words_v1(sentence): + count = defaultdict(int) + for word in sentence.split(): + count[word.lower()] += 1 + return count + + +def count_words_v2(sentence): + regex = r"[\w|']+" + count = defaultdict(int) + for word in re.findall(regex, sentence): + count[word.lower()] += 1 + return count + + +count_words = count_words_v2 diff --git a/count/test_count.py b/count/test_count.py new file mode 100644 index 0000000..f7eeb96 --- /dev/null +++ b/count/test_count.py @@ -0,0 +1,37 @@ +import unittest + + +from count import count_words + + +class CountWordsTests(unittest.TestCase): + + """Tests for count_words.""" + + def test_simple_sentence(self): + actual = count_words("oh what a day what a lovely day") + expected = {"oh": 1, "what": 2, "a": 2, "day": 2, "lovely": 1} + self.assertEqual(actual, expected) + + def test_apostrophe(self): + actual = count_words("don't stop believing") + expected = {"don't": 1, "stop": 1, "believing": 1} + self.assertEqual(actual, expected) + + # To test the Bonus part of this exercise, comment out the following line + # @unittest.expectedFailure + def test_capitalization(self): + actual = count_words("Oh what a day what a lovely day") + expected = {"oh": 1, "what": 2, "a": 2, "day": 2, "lovely": 1} + self.assertEqual(actual, expected) + + # To test the Bonus part of this exercise, comment out the following line + # @unittest.expectedFailure + def test_symbols(self): + actual = count_words("¿Te gusta Python?") + expected = {"te": 1, "gusta": 1, "python": 1} + self.assertEqual(actual, expected) + + +if __name__ == "__main__": + unittest.main(verbosity=2)