File tree 1 file changed +11
-16
lines changed
1 file changed +11
-16
lines changed Original file line number Diff line number Diff line change 1
1
def longest_word (sentence : str ) -> str :
2
2
"""
3
3
Finds the longest word in a sentence.
4
-
5
4
>>> longest_word("The quick brown fox jumped over the lazy dog")
6
5
'jumped'
7
6
>>> longest_word("Python is amazing")
@@ -10,24 +9,20 @@ def longest_word(sentence: str) -> str:
10
9
''
11
10
>>> longest_word("a")
12
11
'a'
13
- >>> longest_word("A tie between words")
14
- 'between'
15
- >>> longest_word("All words are same length")
16
- 'All'
17
- >>> longest_word("Multiple words with the same longest length")
18
- 'Multiple'
19
- >>> longest_word("Trailing spaces at the end ")
20
- 'Trailing'
21
- >>> longest_word(" Leading spaces at the start")
22
- 'Leading'
23
- >>> longest_word("Special characters !@#$%^&*() should be ignored")
24
- 'characters'
12
+ >>> longest_word("A journey of a thousand miles begins with a single step")
13
+ 'thousand'
14
+ >>> longest_word("To be or not to be that is the question")
15
+ 'question'
16
+ >>> longest_word("All that glitters is not gold")
17
+ 'glitters'
18
+ >>> longest_word("Beauty is in the eye of the beholder")
19
+ 'beholder'
20
+ >>> longest_word("A picture is worth a thousand words")
21
+ 'thousand'
25
22
"""
26
23
words = sentence .split ()
27
- return max (words , key = len ) if words else ""
28
-
24
+ return max (words , key = len ) if words else ''
29
25
30
26
if __name__ == "__main__" :
31
27
from doctest import testmod
32
-
33
28
testmod ()
You can’t perform that action at this time.
0 commit comments