Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returns the longest word in that sentence #135

Merged
merged 17 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
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
65 changes: 65 additions & 0 deletions solutions/longest_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""
A module for Returns the longest word in the given sentence.

Module contents:
This module contains a function `longest_word`
that takes a sentence and returns the longest word in it.
It handles edge cases such as empty strings, non-string inputs, and punctuation.

Author: Özgür Özbek
Date: 11th January 2025
Group: ET6-foundations-group-16
"""

import string


def longest_word(sentence: str) -> str:
"""
Returns the longest word in the given sentence, ignoring punctuation.

Args:
sentence (str): A sentence from which to find the longest word.

Returns:
str: The longest word in the sentence. If there are multiple longest words,
the first one encountered is returned.

Raises:
AssertionError: If the input is not a string or if the string is empty.

Example:
>>> longest_word("I love programming with Python")
'programming'

>>> longest_word("apple banana cherry!")
'banana'

>>> longest_word("Hello, world!")
'Hello'

"""
# Defensive assertion to check if the sentence is a non-empty string
assert isinstance(sentence, str), "Input must be a string."
assert sentence, "Input string must not be empty."

# Split the sentence into words based on spaces
words = sentence.split()

# Initialize the variable to store the longest word
longest = ""

# Iterate through each word in the list
for word in words:
# Remove punctuation from the start and end of the word
word = word.strip(string.punctuation)

# Check if the current word is longer than the longest found so far
if len(word) > len(longest):
longest = word

return longest
71 changes: 71 additions & 0 deletions solutions/tests/test_longest_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""
This module contains unit tests for the `longest_word` function.

The tests verify that the `longest_word` function works as expected for a variety of
valid and invalid inputs, including boundary cases.

Tests include:
- Valid sentence inputs containing multiple words.
- Sentences with a single word.
- Empty strings to test for error handling.
- Non-string inputs to ensure a `ValueError` is raised.
- Sentences with multiple longest words to check if the first encountered word is returned.

The tests ensure that the function returns the correct longest word, and that it raises an
`AssertionError` when given invalid inputs such as non-string values or empty strings.
"""

import unittest
from solutions.longest_word import longest_word


class TestLongestWord(unittest.TestCase):
"""
Test the behavior of the longest_word function.
"""

def test_sentence_with_punctuation(self):
"""
Test case for sentences containing punctuation.
The function should return the longest word, ignoring punctuation.
"""
result = longest_word("Hello, world!")
self.assertEqual(result, "Hello")

def test_non_string_input(self):
"""
Test case for non-string input to ensure defensive assertion raises the error.
"""
with self.assertRaises(AssertionError):
longest_word(123) # Pass a non-string value

def test_empty_string(self):
"""
Test case for an empty string to ensure the correct error is raised.
"""
with self.assertRaises(AssertionError):
longest_word("") # Pass an empty string

def test_single_word(self):
"""
Test case for a sentence with a single word.
The function should return the single word.
"""
result = longest_word("Python")
self.assertEqual(result, "Python")

def test_multiple_longest_words(self):
"""
Test case for multiple words of the same maximum length.
The function should return the first encountered word.
"""
result = longest_word("apple banana cherry")
self.assertEqual(result, "banana")


if __name__ == "__main__":
unittest.main()
Loading