forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from MIT-Emerging-Talent/word-length-counter
This merge includes comprehensive updates to the project "word_length_counter," enhancing code quality, consistency, and reliability.
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/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: str) -> list[int]: | ||
""" | ||
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. | ||
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] | ||
>>> 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Tests for the word_lengths function. | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.challenge_39.word_length_counter import word_lengths | ||
|
||
|
||
class TestWordLengths(unittest.TestCase): | ||
def test_multiple_words(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() |