Update longest_word_in_sentence.py

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

View File

@ -10,6 +10,18 @@ def longest_word(sentence: str) -> str:
''
>>> longest_word("a")
'a'
>>> longest_word("A tie between words")
'between'
>>> longest_word("All words are same length")
'All'
>>> longest_word("Multiple words with the same longest length")
'Multiple'
>>> longest_word("Trailing spaces at the end ")
'Trailing'
>>> longest_word(" Leading spaces at the start")
'Leading'
>>> longest_word("Special characters !@#$%^&*() should be ignored")
'characters'
"""
words = sentence.split()
return max(words, key=len) if words else ""