Automatically translated.View original post

🔍 Daily Python Problem: Hunt for the Longest Word! 💖

Let's do some fun problems today and use some of Python's cool functions! 🐍

Problem: Write the function named find _ long _ word (sentence) that receives the String (that is a sentence) and must return the longest word in that sentence!

• Special Requirements: If there are many words of the same length, restore the first word found enough!

💡 Crucial Tip: The String Split!

The first thing we need to do is to "blow" long sentences into a list of words. We use the .split ("") method to separate the String with spaces. This is the list of words in the loop!

🌟 There are two great ways to show it:

1.Basic Type (Loop Use):

• Start by splitting the String with .split ("") first

• Define the variable max _ length = 0 and long _ word = ""

• Loop in the list of words to check the length of each word and update the max _ length and long _ word.

2.Pythonic (use max with key):

• This is very short and beautiful! We use the max function (iterable, key = function).

• By assigning key = len (len is the length function), Python will find the "word" in the list that makes the len () function the highest value! It also follows that Requirements, if equal, will restore the first word found!

• Don't forget: Check the case where the list is empty if not words: before returning the answer!

Example:

• Input: "Python is a popular programming language."

• Output: "programming"

Let's try to practice writing and know that Python makes String Management a lot easier! Keep going. 🥰

# python # data # ai # programmer # learnonlemon8

2025/11/12 Edited to

... Read moreการเขียนฟังก์ชันสำหรับหาคำที่ยาวที่สุดในประโยคถือเป็นโจทย์ที่ดีในการฝึกจัดการกับข้อความในภาษา Python โดยสิ่งสำคัญที่ต้องรู้คือการใช้เมธอด .split(" ") เพื่อแปลงประโยค (string) ให้ออกมาเป็นรายชื่อคำ (list of words) ซึ่งช่วยให้เราสามารถเข้าถึงคำแต่ละคำได้ง่ายขึ้น นอกจากนั้น การแก้ปัญหาด้วยวิธี Pythonic นั้นมีประสิทธิภาพและกระชับมากขึ้น โดยใช้ฟังก์ชัน max ที่รับพารามิเตอร์ key=len เพื่อให้ Python หาคำที่มีความยาวมากที่สุดในลิสต์ได้โดยตรง ซึ่งยังปฏิบัติตามข้อกำหนดที่ว่าหากมีคำหลายคำที่ยาวเท่ากัน ให้คืนค่าคำแรกที่พบในประโยค อย่างไรก็ตามในขั้นตอนการเขียนฟังก์ชันควรใส่เงื่อนไขเช็คลิสต์คำว่าว่างหรือไม่ (เช่น if not words:) เพราะถ้าประโยคที่รับมาเป็น string ว่างเปล่า จะทำให้การใช้ max() เกิดข้อผิดพลาดได้ ลองดูตัวอย่างเพิ่มเติมในการประยุกต์ใช้ฟังก์ชันนี้ เช่น การกรองคำที่ไม่ใช่ตัวอักษร เช่น เครื่องหมายวรรคตอน หรือเลขก่อน แล้วค่อยหาคำที่ยาวที่สุด เพื่อให้ผลลัพธ์แม่นยำมากขึ้น ตัวอย่างเช่น ```python def find_longest_word_clean(sentence): import re words = re.findall(r'\b\w+\b', sentence) if not words: return "" return max(words, key=len) ``` ในฟังก์ชันนี้ เราใช้ regular expression ช่วยแยกคำแบบไม่รวมสัญลักษณ์พิเศษ ซึ่งเป็นประโยชน์มากเมื่อประโยคมีเครื่องหมายวรรคตอนปะปน นอกจากนี้ การเข้าใจแก่นของการจัดการสตริงและ list ใน Python ยังเปิดโอกาสให้ผู้เรียนได้รู้จักกับฟังก์ชัน built-in และการเขียนโค้ดที่กระชับและมีประสิทธิภาพมากขึ้น เหมาะสำหรับผู้เริ่มต้นและผู้ที่ต้องการพัฒนาทักษะการเขียนโปรแกรมเพื่อจัดการข้อมูลข้อความหรือข้อมูลเชิงภาษา สุดท้าย ฝึกฝนการคิดเงื่อนไขและการจัดการกรณีพิเศษ เช่น ประโยคที่ว่าง หรือประโยคที่มีคำยาวเท่ากัน จะช่วยพัฒนาทักษะโจทย์ Programming ทั่วไปให้แข็งแกร่งขึ้นและพร้อมรับมือโจทย์ที่ซับซ้อนมากขึ้นในอนาคต