Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. จัดรูปแบบตัวเลขด้วยคอมม่า (Thousand Separator) ใน Python

    ใช้ f-string หรือ format() จัดรูปแบบตัวเลขให้มีคอมม่าคั่นพัน จำนวนเต็ม (Integer) print(f{1445:,d}) # 1,445 print(f{140045:,d}) # 140,045 ทศนิยม (Float) print({:,.2f}.format(3435.242563)) # 3,435.24 print(f{1234567.89:,.2f}) # 1,234,567.89 รูปแบบอื่นๆ Format Specตัวอย่างผลลัพธ์

  2. โปรแกรมคำนวณเกรดนักเรียน (Grade Calculator) ใน Python

    โปรแกรมรับคะแนน 5 วิชา คำนวณรวม เปอร์เซ็นต์ เกรด และผลสอบ (ผ่าน/เงื่อนไข/ไม่ผ่าน) เกณฑ์การให้เกรด คะแนนเฉลี่ยเกรด = 90O = 80A+ = 70A = 60B+ = 50B = 40C < 40F เกณฑ์ผลสอบ = 40 → PASS = 3 วิชา → COMP (เงื่อนไข) อื่นๆ → FAIL โค้ด (ปรับปรุงให้รันได้อัตโนมัติ ไม่ต้อง input) = 3: self.result = COMP

  3. สร้างธงชาติไทยด้วย Python (matplotlib + numpy)

    ใช้ matplotlib และ numpy วาดธงไทย 3 สี พร้อมธงชัย (Ashoka Chakra) 24 แฉก ขั้นตอน สร้างสี่เหลี่ยม 3 ชั้น (แดง, ขาว, น้ำเงิน) ด้วย matplotlib.patches.Rectangle วาดวงกลมกลางสี navy ด้วย Circle ใช้ numpy คำนวณพิกัด 24 แฉก (spokes) วาดด้วย Polygon โค้ด import numpy as np import matplotlib.pyplot as plt

  4. Find Peak Element ใน Python

    Peak element คือค่าที่มากกว่าเพื่อนบ้านทั้งสองข้าง (หรือข้างเดียวที่ขอบ) ให้คืน index ของ peak ใดก็ได้ ตัวอย่าง Input: [1,2,1,3,5,6,4] Peaks: index 1 (ค่า 2) หรือ index 5 (ค่า 6) อัลกอริทึม Binary Search (O(log n)) เนื่องจาก nums[i] ≠ nums[i+1] เราสามารถ binary search ได้: mid = (low+high)//2 ถ้า n

  5. Number of Islands (นับจำนวนเกาะ) ใน Python

    กำหนด grid 2D ของ 1 (บก) และ 0 (น้ำ) นับจำนวนเกาะ (connected component แบบ 4-direction) ตัวอย่าง grid = [ [1,1,0,0,0], [1,1,0,0,0], [0,0,1,0,0], [0,0,0,1,1] ] Output: 3 อัลกอริทึม DFS / BFS วนทุกเซลล์ ถ้าเจอ 1 → เพิ่ม count และ DFS/BFS ทำให้เป็น 0 (น้ำ) ทั้ง component DFS เรียกซ้ำ 4 ทิศทาง

  6. Implement Trie (Prefix Tree) ใน Python

    Trie หรือ Prefix Tree เป็นโครงสร้างข้อมูลสำหรับจัดเก็บสตริง รองรับ insert, search, startsWith การทำงาน แต่ละโหนดเป็น dictionary mapping ตัวอักษร → โหนดถัดไป เครื่องหมาย # (หรือ key ใดก็ได้) ใช้ระบุจบคำ ตัวอย่างการใช้งาน trie = Trie() trie.insert(apple) trie.search(apple) # True trie.search(app)

  7. Kth Largest Element ใน Array ด้วย Python

    หาค่าที่เป็นอันดับ k จากมากไปน้อยในอาร์เรย์ unsorted ตัวอย่าง Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 หลายวิธีแก้ วิธีTimeSpaceหมายเหตุ Sort แล้ว取 indexO(n log n)O(1) หรือ O(n)ง่ายที่สุด Min-Heap ขนาด kO(n log k)O(k)เหมาะ k << n QuickSelectO(n) averageO(1)ซับซ้อนกว่า โค้ดง่ายที่สุด (Sort) cla

  8. Kth Smallest Element ใน BST ด้วย Python

    หาค่าที่เป็นอันดับ k จากน้อยไปมากใน Binary Search Tree (BST) คุณสมบัติ BST Inorder traversal ให้ค่าจากน้อยไปมาก → ตัวที่ k ใน inorder คือคำตอบ ตัวอย่าง BST: 10 / \ 5 15 / \ / 2 7 13 k=3 → 7 วิธีที่ 1: Inorder Traversal เก็บทั้งหมด (O(n) space) class S

  9. Lowest Common Ancestor (LCA) ของ Binary Tree ใน Python

    หา common ancestor ต่ำสุดของโหนด p และ q ใน binary tree ตัวอย่าง Tree: 3 / \ 5 1 / \ / \ 6 2 0 8 / \ 7 4 LCA(5, 1) = 3 LCA(5, 4) = 5 อัลกอริทึม Recursive ถ้า root เป็น None หรือ root คือ p หรือ q → คืน root หา LCA ใน left subtree แล

  10. Product of Array Except Self ใน Python

    สร้างอาร์เรย์ output ที่ output[i] = ผลคูณของทุกตัวยกเว้น nums[i] ห้ามใช้หาร ตัวอย่าง Input: [1, 2, 3, 4] Output: [24, 12, 8, 6] แนวคิด: Prefix & Suffix Product คำนวณ prefix product (ผลคูณทางซ้าย) และ suffix product (ทางขวา) output[i] = prefix[i-1] * suffix[i+1] ทำได้ใน O(n) time, O(1) extra space

  11. ค้นหาในเมทริกซ์ 2D แบบ Sorted (Search a 2D Matrix II) ใน Python

    เมทริกซ์ m×n ที่แถวและคอลัมน์เรียงลำดับจากน้อยไปมาก ค้นหา target อย่างมีประสิทธิภาพ ตัวอย่างเมทริกซ์ [[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]] target=5 → True, target=20 → False อัลกอริทึม: เริ่มจากมุมขวาบน (หรือซ้ายล่าง) เริ่มที

  12. หาตัวเลขซ้ำ (Find Duplicate Number) ใน Python

    โจทย์: อาร์เรย์ขนาด n+1 มีค่าอยู่ในช่วง 1..n แน่นอนว่ามีซ้ำอย่างน้อย 1 ตัว (สมมติซ้ำเพียง 1 ค่า) ให้หาค่านั้น ตัวอย่าง Input: [3,1,3,4,2] Output: 3 อัลกอริทึม Floyds Tortoise and Hare (Cycle Detection) มองอาร์เรย์เป็น linked list โดย index ชี้ไปที่ค่า (nums[i] → index nums[i]) จะเกิด cycle ที่จุดซ้ำ

  13. Longest Increasing Subsequence (LIS) ใน Python

    หาความยาวของ longest increasing subsequence (ไม่จำเป็นติดกัน) ในอาร์เรย์ ตัวอย่าง Input: [10,9,2,5,3,7,101,18] Output: 4 # LIS คือ [2,3,7,101] อัลกอริทึม Patience Sorting (O(n log n)) สร้างอาร์เรย์ tails เก็บค่าสุดท้ายของ subsequence แต่ละความยาว สำหรับแต่ละตัวเลข x: Binary search หาตำแหน่งแร

  14. Odd Even Linked List ใน Python

    จัดกลุ่มโหนดตำแหน่งคี่ (odd) ไว้หน้า ตามด้วยตำแหน่งคู่ (even) ใน singly linked list โดยทำ in-place ตัวอย่าง Input: 1 → 22 → 13 → 14 → 25 Output: 1 → 13 → 25 → 22 → 14 แนวคิด ใช้ pointer สองตัว: odd และ even แยก list เป็นสองสาย แล้วต่อท้าย odd เข้ากับ head ของ even โค้ด class ListNode: def __i

  15. Increasing Triplet Subsequence ใน Python

    ตรวจสอบว่ามี subsequence เพิ่มขึ้นยาว 3 อยู่ในอาร์เรย์หรือไม่ (ไม่จำเป็นต้องติดกัน) ตัวอย่าง Input: [5,3,8,2,7,9,4] Output: True # เพราะมี 3,7,9 หรือ 2,7,9 อัลกอริทึม Greedy (O(n) time, O(1) space) เก็บค่า small และ big เริ่มต้นเป็น infinity วนอ่านแต่ละตัว: ถ้า ≤ small → อัปเดต small ถ้า ≤

  16. หา Top K Frequent Elements ใน Python

    โจทย์: กำหนดอาร์เรย์จำนวนเต็มและค่า k ให้คืนค่า k องค์ประกอบที่มีความถี่มากที่สุด ตัวอย่าง Input: nums = [1,1,1,1,2,2,3,3,3], k = 2 Output: [1, 3] แนวคิด (Bucket Sort) นับความถี่แต่ละตัวเลข → dict num_freq กลุ่มตัวเลขตามความถี่ → dict freq_list (key=ความถี่, value=ลิสต์ตัวเลข) วนจากความถี่สูงสุดลงม

  17. นับคำนำหน้า (Prefixes) ที่มีความถี่ตัวอักษรมากกว่าใน Python

    โจทย์: กำหนดสตริงและตัวอักษร 2 ตัว ให้หาคำนำหน้าทั้งหมดที่ตัวอักษรตัวแรกมีความถี่มากกว่าตัวที่สอง แล้วนับจำนวน ตัวอย่าง Input: string=apple, alphabets=p, e → Output: ap, app, appl, apple (4 prefixes) Input: string=apple, alphabets=e, p → Output: 0 อัลกอริทึม วนลูปสร้าง prefix ทีละตัวอักษร นับความ

  18. นับความถี่ของทุกองค์ประกอบในอาร์เรย์ด้วย Python

    มีหลายวิธีในการนับความถี่ (frequency) ของแต่ละค่าในลิสต์ นี่คือ 2 วิธีที่นิยมใช้ วิธีที่ 1: ใช้ Dictionary (dict) arr = [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3] elements_count = {} for element in arr: if element in elements_count: elements_count[element] += 1 else: elements_coun

  19. ตรวจสอบสภาพอากาศปัจจุบันด้วย OpenWeatherMap API ใน Python

    ใช้ OpenWeatherMap API ดึงข้อมูลสภาพอากาศของเมืองใดก็ได้ ฟรี 60 คำขอ/นาที ขั้นตอน สมัครสมาชิกและได้ API Key ติดตั้ง requests ส่ง request ไปที่ endpoint https://api.openweathermap.org/data/2.5/weather แปลง JSON response อ่านค่าอุณหภูมิ ความชื้น ความดัน และรายงานสภาพอากาศ ตัวอย่างโค้ด import request

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:133/451  20-คอมพิวเตอร์/Page Goto:1 127 128 129 130 131 132 133 134 135 136 137 138 139