From d80e1f88cdd29660251a921a282cbc528b1b14da Mon Sep 17 00:00:00 2001 From: Dorvil Lens Marc-Arthur Date: Tue, 7 Jan 2025 05:24:34 -0500 Subject: [PATCH 1/5] added documentation and unittest for word length counter --- solutions/tests/test_word.py | 34 ++++++++++++++++++++++++++++++++++ solutions/word/word.py | 24 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 solutions/tests/test_word.py create mode 100644 solutions/word/word.py diff --git a/solutions/tests/test_word.py b/solutions/tests/test_word.py new file mode 100644 index 000000000..9ba3daff2 --- /dev/null +++ b/solutions/tests/test_word.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Tests for the word_lengths function. +""" + +import unittest + +from solutions.word.word import word_lengths + + +class TestWordLengths(unittest.TestCase): + def test_regular_sentences(self): + self.assertEqual(word_lengths("Hello world!"), [5, 5]) + self.assertEqual(word_lengths("Python is awesome."), [6, 2, 7]) + + def test_single_word(self): + self.assertEqual(word_lengths("Amazing!"), [7]) + + def test_empty_string(self): + self.assertEqual(word_lengths(""), []) + + def test_only_punctuation(self): + self.assertEqual(word_lengths("...?!"), []) + + def test_mixed_content(self): + self.assertEqual(word_lengths("123 and 4567!"), [3, 3, 4]) + + def test_special_characters_in_words(self): + self.assertEqual(word_lengths("co-op re-enter."), [4, 7]) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/word/word.py b/solutions/word/word.py new file mode 100644 index 000000000..9b3b5e83c --- /dev/null +++ b/solutions/word/word.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +A module for calculating the lengths of words in a sentence. +created: 01/07/2025 +@author: Arthur (Mr-Glucose) + +""" + +import string + + +def word_lengths(input_sentence): + """ + Takes a sentence and returns a list of word lengths, ignoring punctuation. + + Args: + input_sentence (str): The input sentence. + + Returns: + list: A list of integers representing word lengths. + """ + words = input_sentence.translate(str.maketrans("", "", string.punctuation)).split() + return [len(word) for word in words] From 6de415aa6c9bd44fe9d582d9b012a65cf88d6fad Mon Sep 17 00:00:00 2001 From: Dorvil Lens Marc-Arthur Date: Thu, 9 Jan 2025 09:08:58 -0500 Subject: [PATCH 2/5] challenge_39 --- .vscode/settings.json | 11 ++++++++++- solutions/{word => challenge_39}/word.py | 0 solutions/tests/{ => challenge_39}/test_word.py | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) rename solutions/{word => challenge_39}/word.py (100%) rename solutions/tests/{ => challenge_39}/test_word.py (94%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 252022b48..712b75631 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -122,5 +122,14 @@ "source.fixAll.ruff": "explicit", "source.organizeImports.ruff": "explicit" } - } + }, + "python.testing.unittestArgs": [ + "-v", + "-s", + "./solutions", + "-p", + "*test.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true } diff --git a/solutions/word/word.py b/solutions/challenge_39/word.py similarity index 100% rename from solutions/word/word.py rename to solutions/challenge_39/word.py diff --git a/solutions/tests/test_word.py b/solutions/tests/challenge_39/test_word.py similarity index 94% rename from solutions/tests/test_word.py rename to solutions/tests/challenge_39/test_word.py index 9ba3daff2..8ad9056b3 100644 --- a/solutions/tests/test_word.py +++ b/solutions/tests/challenge_39/test_word.py @@ -6,7 +6,7 @@ import unittest -from solutions.word.word import word_lengths +from solutions.challenge_39.word import word_lengths class TestWordLengths(unittest.TestCase): From 93baefddc2be7cb418e441f93a0e4d74bc3be06d Mon Sep 17 00:00:00 2001 From: Dorvil Lens Marc-Arthur Date: Fri, 10 Jan 2025 02:05:35 -0500 Subject: [PATCH 3/5] challenge_39 --- solutions/challenge_39/{word.py => word_length_counter.py} | 0 .../{test_word.py => test_word_length_counter.py} | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename solutions/challenge_39/{word.py => word_length_counter.py} (100%) rename solutions/tests/challenge_39/{test_word.py => test_word_length_counter.py} (88%) diff --git a/solutions/challenge_39/word.py b/solutions/challenge_39/word_length_counter.py similarity index 100% rename from solutions/challenge_39/word.py rename to solutions/challenge_39/word_length_counter.py diff --git a/solutions/tests/challenge_39/test_word.py b/solutions/tests/challenge_39/test_word_length_counter.py similarity index 88% rename from solutions/tests/challenge_39/test_word.py rename to solutions/tests/challenge_39/test_word_length_counter.py index 8ad9056b3..237659efc 100644 --- a/solutions/tests/challenge_39/test_word.py +++ b/solutions/tests/challenge_39/test_word_length_counter.py @@ -6,11 +6,11 @@ import unittest -from solutions.challenge_39.word import word_lengths +from solutions.challenge_39.word_length_counter import word_lengths class TestWordLengths(unittest.TestCase): - def test_regular_sentences(self): + def test_multiple_words(self): self.assertEqual(word_lengths("Hello world!"), [5, 5]) self.assertEqual(word_lengths("Python is awesome."), [6, 2, 7]) From e078ce6f6db7fdfdaf4797bd57ec2b280ee11aa5 Mon Sep 17 00:00:00 2001 From: Dorvil Lens Marc-Arthur Date: Fri, 10 Jan 2025 02:23:34 -0500 Subject: [PATCH 4/5] challenge_39 --- solutions/challenge_39/word_length_counter.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/solutions/challenge_39/word_length_counter.py b/solutions/challenge_39/word_length_counter.py index 9b3b5e83c..e67baba99 100644 --- a/solutions/challenge_39/word_length_counter.py +++ b/solutions/challenge_39/word_length_counter.py @@ -4,13 +4,12 @@ A module for calculating the lengths of words in a sentence. created: 01/07/2025 @author: Arthur (Mr-Glucose) - """ import string -def word_lengths(input_sentence): +def word_lengths(input_sentence: str) -> list[int]: """ Takes a sentence and returns a list of word lengths, ignoring punctuation. @@ -19,6 +18,18 @@ def word_lengths(input_sentence): Returns: list: A list of integers representing word lengths. + + Examples: + >>> word_lengths("Hello world!") + [5, 5] + >>> word_lengths("Python is awesome.") + [6, 2, 7] + >>> word_lengths("co-op re-enter.") + [4, 7] + >>> word_lengths("") + [] + >>> word_lengths("...?!") + [] """ words = input_sentence.translate(str.maketrans("", "", string.punctuation)).split() return [len(word) for word in words] From 596990751b430e792fbd67b235c39af3bf88ec52 Mon Sep 17 00:00:00 2001 From: Dorvil Lens Marc-Arthur Date: Fri, 10 Jan 2025 02:37:50 -0500 Subject: [PATCH 5/5] challenge_39 --- solutions/challenge_39/word_length_counter.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/solutions/challenge_39/word_length_counter.py b/solutions/challenge_39/word_length_counter.py index e67baba99..cd55d969b 100644 --- a/solutions/challenge_39/word_length_counter.py +++ b/solutions/challenge_39/word_length_counter.py @@ -19,6 +19,11 @@ def word_lengths(input_sentence: str) -> list[int]: Returns: list: A list of integers representing word lengths. + Assumptions: + - If the input sentence is empty, the function will return an empty list. + - Punctuation is ignored when calculating word lengths. + - Case sensitivity does not affect word length calculation. + Examples: >>> word_lengths("Hello world!") [5, 5]