Skip to content

Commit 0ba8a7e

Browse files
Merge pull request #9 from wbalbo/longest-word-feature
feat: implement longest word finder with interactive mode and unit tests
2 parents 0552d6c + 72d1c60 commit 0ba8a7e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

longest_word.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
import string
3+
import sys
4+
5+
def find_longest_word(sentence):
6+
# Remove punctuation
7+
cleaned_sentence = sentence.translate(str.maketrans('', '', string.punctuation))
8+
words = cleaned_sentence.split()
9+
longest = ""
10+
11+
for word in words:
12+
if len(word) > len(longest):
13+
longest = word
14+
15+
return longest
16+
17+
class TestFindLongestWord(unittest.TestCase):
18+
19+
def test_regular_sentence(self):
20+
self.assertEqual(find_longest_word("Random sentences can also spur creativity"), "creativity")
21+
22+
def test_with_punctuation(self):
23+
self.assertEqual(find_longest_word("The quick, brown fox jumped over the lazy dog."), "jumped")
24+
25+
def test_tie_same_length(self):
26+
self.assertEqual(find_longest_word("Cat bat rat mat"), "Cat")
27+
28+
def test_single_word(self):
29+
self.assertEqual(find_longest_word("Supercalifragilisticexpialidocious!"), "Supercalifragilisticexpialidocious")
30+
31+
def test_with_numbers_and_punctuation(self):
32+
self.assertEqual(find_longest_word("Python 3.10 is awesome, right?"), "awesome")
33+
34+
def test_empty_input(self):
35+
self.assertEqual(find_longest_word(""), "")
36+
37+
def test_only_punctuation(self):
38+
self.assertEqual(find_longest_word("!!! ,,, ???"), "")
39+
40+
def interactive_mode():
41+
"""Provides interactive input for the user to test the function."""
42+
print("Enter sentences to find the longest word, or type 'q' to quit.")
43+
while True:
44+
sentence = input("Enter a sentence: ")
45+
if sentence.lower() == 'q':
46+
break
47+
print("Longest word:", find_longest_word(sentence))
48+
print("Goodbye!")
49+
50+
51+
if __name__ == '__main__':
52+
# If "test" is provided as an argument, run unit tests.
53+
if len(sys.argv) > 1 and sys.argv[1] == "test":
54+
# Remove the "test" argument so unittest doesn't process it.
55+
sys.argv.pop(1)
56+
unittest.main()
57+
else:
58+
interactive_mode()

0 commit comments

Comments
 (0)