From 1c7710de382cf9648dedfda740593b0440f8b1b1 Mon Sep 17 00:00:00 2001 From: Saurabh Mahapatra <98408932+its-100rabh@users.noreply.github.com> Date: Fri, 5 Jul 2024 10:29:48 +0530 Subject: [PATCH] Create longest_word_in_sentence.py --- strings/longest_word_in_sentence.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 strings/longest_word_in_sentence.py diff --git a/strings/longest_word_in_sentence.py b/strings/longest_word_in_sentence.py new file mode 100644 index 000000000..202c19175 --- /dev/null +++ b/strings/longest_word_in_sentence.py @@ -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()