Automatically translated.View original post

🐍 Daily Python Problem: Find the Top Scorer! 🏆

Today, let's practice using Dictionaries (or judgments) Key-Value data structures to manage student data and scores!

📝 Problem & Task:

Write the find _ highest _ score function that receives the Dictionary that stores the name (Key) and score (Value) and returns the "Key" of the highest-rated person!

💡 2 god-level ways to solve this problem!

1. Basic (Use Loop) - Practice Logic! 🧱

• Create the highest _ score and top _ student to track the highest values ever found

• Use .items () to loop the Loop, pulling out both Key (name) and Value (score) simultaneously.

• In Loop, use if score > highest _ score to update the maximum.

• This method helps to understand the operation of the "Finding Maximum" algorithm.

2.Pythonic (use max () function with key) - short, pro, fast! ⭐

• We can use the max () function directly with the Dictionary!

• return max (scores _ judgment, key = scores _ judgment.get)

• We tell max () to find the highest value key using score _ judgment.get as a comparison criterion (is to look at value instead of key).

• This method is only one line short and the most beautiful (Pythonic)

💖 Use of .items () and max functions (..., key =...) is an important skill for Python Developer! Keep going!

# python # Programming # data # ai # learnonlemon8

2025/11/7 Edited to

... Read moreในบทความนี้นอกจากจะสอนการใช้ .items() เพื่อวนลูปแล้ว ยังอยากแนะนำว่าการใช้ Dictionary เป็นวิธีที่สะดวกมากในการเก็บข้อมูลคู่ชื่อและคะแนน เราสามารถเพิ่มข้อมูล เข้าใจและปรับปรุงโค้ดได้ไม่ยาก นอกจากนี้ เมื่อใช้ max() กับ key=scores_dict.get จะทำให้โค้ดสั้นลงมากและยังอ่านง่าย เหมาะกับงานที่ต้องการประสิทธิภาพและความสวยงามในการเขียนโปรแกรม สำหรับผู้เริ่มต้น แนะนำให้ลองเขียนฟังก์ชันทั้งสองแบบ เพื่อเข้าใจกลไกการทำงานของลูปและ built-in function ของ Python ที่ช่วยให้เราประหยัดเวลาในการเขียนโปรแกรม นอกจากนั้น การเรียนรู้วิธีใช้ Dictionary และการค้นหาค่าสูงสุดยังเป็นทักษะสำคัญที่ใช้ได้กับงานโปรแกรมมิ่งจริงในหลายด้าน ไม่ว่าจะเป็นการวิเคราะห์ข้อมูลหรือพัฒนาโปรแกรมที่ต้องจัดการข้อมูลแบบ Key-Value สุดท้าย การฝึกเขียนโค้ดตามโจทย์ปัญหาที่กำหนดอย่างนี้ช่วยให้เข้าใจคอนเซ็ปต์ของ Data Structure และ Algorithm เบื้องต้นได้ดีมาก แนะนำลองประยุกต์ใช้กับโจทย์อื่น ๆ เพื่อเพิ่มพูนทักษะของตัวเองค่ะ!