Update longest_word_in_sentence.py

This commit is contained in:
Saurabh Mahapatra 2024-07-07 10:08:56 +05:30 committed by GitHub
parent 8336e54e91
commit 1b9bde3e34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,7 +1,6 @@
def longest_word(sentence: str) -> str: def longest_word(sentence: str) -> str:
""" """
Finds the longest word in a sentence. Finds the longest word in a sentence.
>>> longest_word("The quick brown fox jumped over the lazy dog") >>> longest_word("The quick brown fox jumped over the lazy dog")
'jumped' 'jumped'
>>> longest_word("Python is amazing") >>> longest_word("Python is amazing")
@ -10,24 +9,20 @@ def longest_word(sentence: str) -> str:
'' ''
>>> longest_word("a") >>> longest_word("a")
'a' 'a'
>>> longest_word("A tie between words") >>> longest_word("A journey of a thousand miles begins with a single step")
'between' 'thousand'
>>> longest_word("All words are same length") >>> longest_word("To be or not to be that is the question")
'All' 'question'
>>> longest_word("Multiple words with the same longest length") >>> longest_word("All that glitters is not gold")
'Multiple' 'glitters'
>>> longest_word("Trailing spaces at the end ") >>> longest_word("Beauty is in the eye of the beholder")
'Trailing' 'beholder'
>>> longest_word(" Leading spaces at the start") >>> longest_word("A picture is worth a thousand words")
'Leading' 'thousand'
>>> longest_word("Special characters !@#$%^&*() should be ignored")
'characters'
""" """
words = sentence.split() words = sentence.split()
return max(words, key=len) if words else "" return max(words, key=len) if words else ''
if __name__ == "__main__": if __name__ == "__main__":
from doctest import testmod from doctest import testmod
testmod() testmod()