File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11def longest_word (sentence : str ) -> str :
22 """
33 Finds the longest word in a sentence.
4-
54 >>> longest_word("The quick brown fox jumped over the lazy dog")
65 'jumped'
76 >>> longest_word("Python is amazing")
@@ -10,24 +9,20 @@ def longest_word(sentence: str) -> str:
109 ''
1110 >>> longest_word("a")
1211 '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'
2522 """
2623 words = sentence .split ()
27- return max (words , key = len ) if words else ""
28-
24+ return max (words , key = len ) if words else ''
2925
3026if __name__ == "__main__" :
3127 from doctest import testmod
32-
3328 testmod ()
You can’t perform that action at this time.
0 commit comments