حالت آفلاین — محتوا از حافظهٔ کش نمایش داده می‌شود
همهٔ تمرین‌ها

طولانی ترین کلمه جمله

تمرین آسان 2659 visibility link download flag

برنامه ای بنویسید که یک جمله از ورودی دریافت کند و طولانی ترین و کوتاه ترین کلمه موجود در جمله را در خروجی چاپ کند

👨‍💻 1 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد

پاسخ‌ها 17

def longest_and_shortest_word(sentence):
    words = sentence.split()
    longest = max(words, key=len)
    shortest = min(words, key=len)
    return longest, shortest

sentence = input("Enter a sentence: ")
longest, shortest = longest_and_shortest_word(sentence)
print("Longest word:", longest)
print("Shortest word:", shortest)
پاسخ هوش مصنوعی Ai Python
sen = input ('Enter a sentence: ')
list_sen = sen.split ()

minimum = list_sen [0]
maximum = list_sen [0]

for word in list_sen:
    if len (word) < len (minimum):
        minimum = word
    if len (word) > len (maximum):
        maximum = word

print (f'Minimum word is: {minimum}')
print (f'Maximum word is: {maximum}')
def find_longest_and_shortest_words(sentence):

    # تقسیم جمله به کلمات
    words = sentence.split()
    
    # بررسی اگر هیچ کلمه‌ای وجود نداشته باشد
    if not words:
        return None, None

    # پیدا کردن طولانی‌ترین و کوتاه‌ترین کلمه
    longest_word = max(words, key=len)
    shortest_word = min(words, key=len)
    
    return longest_word, shortest_word

def main():
    # دریافت جمله از کاربر
    sentence = input("لطفاً یک جمله وارد کنید: ")
    
    longest_word, shortest_word = find_longest_and_shortest_words(sentence)
    
    if longest_word and shortest_word:
        print(f"طولانی‌ترین کلمه: '{longest_word}'")
        print(f"کوتاه‌ترین کلمه: '{shortest_word}'")
    else:
        print("هیچ کلمه‌ای در جمله وجود ندارد.")

if __name__ == "__main__":
    main()
def find_shortest_and_longest(sentence):
    words=sentence.split()
    shortest_word=min(words,key=len)
    longest_word=max(words,key=len)
    print(shortest_word)
    print(longest_word)
sentence=input('enter a sentence:')
find_shortest_and_longest(sentence)
The_string = input()
The_list = The_string.split()
max = ""
max_count = 0
for i in The_list :
     if len(i) >= max_count :
          max_count = len(i)
          max = i
min = max
min_count = max_count
for j in The_list :
     if len(j) <= min_count :
          min_count = len(j)
          min = j
print(max , min)

ارسال جواب

جوابت را با Markdown بنویس و ثبت کن

×
بستن