mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-22 05:37:36 +00:00
Create longest_word_in_sentence.py
This commit is contained in:
parent
c1dc8e97f7
commit
1c7710de38
19
strings/longest_word_in_sentence.py
Normal file
19
strings/longest_word_in_sentence.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
def longest_word(sentence: str) -> str:
|
||||||
|
"""
|
||||||
|
Finds the longest word in a sentence.
|
||||||
|
|
||||||
|
>>> longest_word("The quick brown fox jumped over the lazy dog")
|
||||||
|
'jumped'
|
||||||
|
>>> longest_word("Python is amazing")
|
||||||
|
'amazing'
|
||||||
|
>>> longest_word("")
|
||||||
|
''
|
||||||
|
>>> longest_word("a")
|
||||||
|
'a'
|
||||||
|
"""
|
||||||
|
words = sentence.split()
|
||||||
|
return max(words, key=len) if words else ''
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
from doctest import testmod
|
||||||
|
testmod()
|
Loading…
x
Reference in New Issue
Block a user