mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Create largest_smallest_words.py
This commit is contained in:
parent
59fc0cefef
commit
b345ea586f
29
strings/largest_smallest_words.py
Normal file
29
strings/largest_smallest_words.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
"""
|
||||||
|
To find the smallest and largest word in the sentence
|
||||||
|
Example
|
||||||
|
if the input string is
|
||||||
|
My name is abc
|
||||||
|
then the output will be
|
||||||
|
The smallest word in the given string is be
|
||||||
|
The largest word in the given string is output
|
||||||
|
"""
|
||||||
|
|
||||||
|
def find_smallest_and_largest_words(input_string):
|
||||||
|
words = input_string.split()
|
||||||
|
if not words:
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
smallest_word = min(words, key=len)
|
||||||
|
largest_word = max(words, key=len)
|
||||||
|
|
||||||
|
return smallest_word, largest_word
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
input_string = input("Enter a string here:\n").strip()
|
||||||
|
smallest, largest = find_smallest_and_largest_words(input_string)
|
||||||
|
|
||||||
|
if smallest is not None and largest is not None:
|
||||||
|
print(f"The smallest word in the given string is {smallest}")
|
||||||
|
print(f"The largest word in the given string is {largest}")
|
||||||
|
else:
|
||||||
|
print("No words found in the input string.")
|
Loading…
Reference in New Issue
Block a user