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จากโจทย์ `find_highest_score` ที่ใช้ Dictionary (dict) หา “ชื่อคนที่ได้คะแนนสูงสุด” เราจะได้ฝึกหลายอย่างที่คนเริ่มเรียน Python มักค้นหาใน Google เลย คือ “โครงสร้าง Python”, “ตัวแปร Python” และ “กฎการตั้งชื่อตัวแปร” — เลยขอเสริมเป็นโน้ตสั้น ๆ แบบที่ฉันใช้จริงตอนสอน/ทำโจทย์แนวนี้ค่ะ 1) โครงสร้าง Python ที่เจอบ่อยในโจทย์ dict - ฟังก์ชัน (function): `def find_highest_score(scores_dict):` เป็นโครงสร้างหลักของการจัดชุดคำสั่งให้เรียกใช้ซ้ำได้ - การกำหนดตัวแปร: เรามักเริ่มด้วยตัวแปรช่วย เช่น `highest_score` และ `top_student` เพื่อ “จำสถานะ” ระหว่างวนลูป - เงื่อนไข (if): `if score > highest_score:` ใช้ตัดสินใจอัปเดตค่าที่ดีที่สุด - การวนซ้ำ dict: `for name, score in scores_dict.items():` เป็นแพตเทิร์นพื้นฐานมากสำหรับ dict เพราะได้ทั้ง key และ value พร้อมกัน 2) ตัวแปร Python: ใช้ให้ถูกชนิดและตั้งค่าเริ่มต้นให้ปลอดภัย ในโจทย์หาค่าสูงสุด หลายคนพลาดตรง “ค่าเริ่มต้น” ของ `highest_score` ค่ะ - ถ้าคะแนนเป็นจำนวนเต็มบวกทั้งหมด ตั้ง `highest_score = -1` ก็พอ - แต่ถ้าคะแนนอาจเป็นลบได้ หรือไม่มั่นใจ แนะนำให้ใช้ `float('-inf')` เพื่อให้เปรียบเทียบแล้วอัปเดตได้แน่นอน - อีกจุดคือกรณี dict ว่าง `{}` ควรกำหนดพฤติกรรมไว้ เช่น return `None` หรือ raise error (แล้วแต่โจทย์) ตัวอย่างแนวที่อ่านง่าย: - `highest_score = float('-inf')` - `top_student = None` 3) กฎการตั้งชื่อตัวแปร Python (ที่ควรรู้ไว้กันโค้ดพัง) กฎพื้นฐาน: - ขึ้นต้นด้วยตัวอักษรหรือ `_` ห้ามขึ้นต้นด้วยตัวเลข - ใช้ได้แค่ตัวอักษร ตัวเลข และ `_` (ห้ามมีเว้นวรรค/ขีดกลาง) - ห้ามใช้ชื่อชนกับ keyword เช่น `def`, `for`, `class`, `if` ข้อแนะนำเพื่อให้อ่านง่าย (สายทำงานจริงชอบมาก): - ใช้รูปแบบ `snake_case` เช่น `highest_score`, `top_student`, `scores_dict` - ตั้งชื่อให้สื่อความหมาย อย่าใช้สั้นเกินจำเป็น เช่น `s` หรือ `x` (ยกเว้นลูปสั้น ๆ) - อย่าตั้งชื่อตัวแปรทับของที่มีอยู่แล้ว เช่น `max = ...` เพราะจะทำให้เรียก `max()` ไม่ได้ในภายหลัง 4) ทำไมวิธี Pythonic ถึงนิยม (แต่ต้องเข้าใจพื้นฐานก่อน) บรรทัดเดียวอย่าง `max(scores_dict, key=scores_dict.get)` อ่านแล้วสั้นและเร็ว แต่ฉันยังแนะนำให้เข้าใจแบบ loop ก่อน เพราะจะช่วยเรื่องตรรกะ (logic) และดีบักง่าย โดยเฉพาะเวลาต้องเพิ่มเงื่อนไข เช่น “คะแนนเท่ากันให้เลือกชื่อเรียง A-Z” หรือ “ข้ามคนที่คะแนนเป็น None” ถ้าคุณกำลังฝึกเรื่อง dict อยู่ ลองเพิ่มเคสทดสอบให้ครบ: dict ว่าง, คะแนนติดลบ, คะแนนซ้ำกัน แล้วจะเข้าใจทั้งโครงสร้าง Python และการใช้ตัวแปรได้แน่นขึ้นมากค่ะ