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