Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. Spiral Matrix II ใน Python - สร้างเมทริกซ์เกลียว

    สมมติว่าเรามีจำนวนเต็มบวก n เราต้องสร้างเมทริกซ์สี่เหลี่ยมจัตุรัสที่มีองค์ประกอบ n² เรียงตามลำดับเกลียว (spiral order) ตัวอย่างเช่น ถ้า n = 4 เมทริกซ์จะเป็นดังนี้: 1234 1213145 1116156 10987 ขั้นตอนการทำงาน กำหนด (row1, col1) := (0, 0) และ (row2, col2) := (n, n) สร้างเมทริกซ์ res เติมด้วย 0 ต

  2. ลดรูปเส้นทาง (Simplify Path) ใน Python

    สมมติว่าเรามีเส้นทางแบบสัมบูรณ์สำหรับไฟล์ (เช่นระบบไฟล์ Unix) เราต้องลดรูปมันให้อยู่ในรูปแบบมาตรฐาน (canonical path) ในระบบไฟล์แบบ UNIX จุด . หมายถึงไดเรกทอรีปัจจุบัน และจุดสองจุด .. จะย้ายขึ้นไปยังไดเรกทอรีผู้ปกครอง (parent directory) คุณสมบัติของ canonical paths มีดังนี้: เส้นทางต้องขึ้นต้นด้วยเ

  3. Set Matrix Zeroes (กำหนดแถว/คอลัมน์เป็นศูนย์) ใน Python

    ในเมทริกซ์ ถ้าตำแหน่งใดเป็น 0 ให้ทำให้แถวและคอลัมน์นั้นทั้งหมดเป็น 0 in-place (O(1) space) ตัวอย่าง Input: [[1,0,1], [1,1,1], [1,1,1]] Output: [[0,0,0], [1,0,1], [1,0,1]] อัลกอริทึม O(1) Space: ใช้แถวแรกและคอลัมน์แรกเป็น Marker เช็คว่าแถวแรกและคอลัมน์แรกมี 0 อยู่แล้วหรือไม่ (flags) ใช้เซลล์แรก

  4. Subsets (Power Set) ใน Python

    สร้างทุก subset (power set) ของชุดตัวเลขที่กำหนด ตัวอย่าง Input: [1,2,3] Output: [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]] วิธีที่ 1: Backtracking (Recursive) class Solution: def subsets(self, nums): res = [] def backtrack(start, path): res.append(path[:])

  5. Word Search (ค้นหาคำในกริด) ใน Python

    หาว่าคำ (word) มีอยู่ใน grid 2D หรือไม่ โดยเดินได้เฉพาะขึ้น/ลง/ซ้าย/ขวา ไม่ใช้เซลล์เดิมซ้ำ ตัวอย่าง Board: A B C E S F C S A D E E Word: SEE → True Word: ABCB → False อัลกอริทึม: DFS + Backtracking วนทุกเซลล์เป็นจุดเริ่มต้น DFS recursive: เช็ค bounds, ตรงตัวอักษร, mark visited (ตั้งค่าเป็น #) เรีย

  6. สร้าง Binary Tree จาก Preorder และ Inorder Traversal ใน Python

    กำหนด preorder และ inorder traversal สร้าง binary tree กลับมา หลักการ Preorder: Root, Left, Right → ตัวแรกคือ Root Inorder: Left, Root, Right → หา Root แล้วแบ่งซ้าย/ขวาได้ ตัวอย่าง Preorder: [3, 9, 20, 15, 7] Inorder: [9, 3, 15, 20, 7] Tree: 3 / \ 9 20

  7. สร้าง Binary Tree จาก Inorder และ Postorder Traversal ใน Python

    กำหนด inorder และ postorder traversal สร้าง binary tree กลับมา หลักการ Postorder: Left, Right, Root → ตัวสุดท้ายคือ Root Inorder: Left, Root, Right → หา Root แล้วแบ่งซ้าย/ขวาได้ ตัวอย่าง Inorder: [9, 15, 7, 20, 3] Postorder: [9, 3, 15, 20, 7] Tree: 7 / \ 9 20

  8. Surrounded Regions (ยึดครองพื้นที่) ใน Python

    ใน board 2D ของ X และ O ให้เปลี่ยน O ที่ถูกล้อมรอบโดย X ทั้ง 4 ทิศทาง เป็น X (ยกเว้น O ที่สัมผัสขอบ) ตัวอย่าง Input: X X X X X O O X X X O X X O X X Output: X X X X X X X X X X X X X O X X # O ล่างสุดไม่ถูกล้อม เพราะสัมผัสขอบ อัลกอริทึม: Flood Fill จากขอบ DFS/BFS จาก O ที่อยู่ขอบ 4 ด้าน → mark เป

  9. Maximum Product Subarray ใน Python

    หา subarray ที่ติดกัน (contiguous) ที่มีผลคูณมากที่สุด ตัวอย่าง Input: [2,3,-2,4,-5,-6,2] Output: 240 # subarray [-2,4,-5,-6] หรือ [2,3,-2,4,-5,-6]? # จริงๆ: 3 * -2 * 4 * -5 * -6 * 2 = -1440 (ผิด) # ถูกต้อง: 4 * -5 * -6 * 2 = 240 อัลกอริทึม Dynamic Programming (Kadane แบบ Product) เก็บ max_ending_

  10. วิเคราะห์ข้อมูลสำรวจประชากร (Census Data) ใน Python

    ใช้ Python วิเคราะห์ข้อมูลสำรวจประชากรอินเดีย (India Census 2011) พร้อมการแสดงผลแบบ Heatmap และ Bar Chart ข้อมูล 640 แขวง (districts), 118 คอลัมน์ ประกอบด้วย: demographic, economic, housing, education, assets ฯลฯ 1. เปรียบเทียบความคล้ายคลึงระหว่าง 2 รัฐ (Similarity Matrix) คำนวณ Euclidean distance

  11. Fast XML Parsing ด้วย Expat ใน Python

    xml.parsers.expat เป็น non-validating XML parser แบบ event-driven (SAX-style) เร็วและประหยัดหน่วยความจำ Handler Functions StartElementHandler(tag, attrs): เปิดแท็ก EndElementHandler(tag): ปิดแท็ก CharacterDataHandler(data): ข้อความภายในแท็ก ตัวอย่าง import xml.parsers.expat def start_element(tag,

  12. Fraud Detection (ตรวจจับการฉ้อโกง) ด้วย Machine Learning ใน Python

    ใช้ Decision Tree Classifier ตรวจจับธุรกรรมบัตรเครดิตที่เป็น Fraud จากข้อมูล Credit Card Fraud Detection ข้อมูล 284,807 transactions, 31 features (V1-V28, Time, Amount, Class) Class: 0=Normal, 1=Fraud Imbalance: Fraud เพียง 0.17% (492/284,315) ขั้นตอน โหลดข้อมูล, ตรวจสอบ imbalance แยก Features (X

  13. ทำนาย Customer Churn ด้วย Machine Learning ใน Python

    ใช้ Logistic Regression ทำนายลูกค้าที่จะเลิกใช้บริการ (Churn) จากข้อมูล Telecom ขั้นตอนหลัก Data Loading: โหลด CSV ด้วย pandas EDA: ดูสัดส่วน Churn, dropping คอลัมน์ไม่เกี่ยวข้อง (customerID) Preprocessing: Label Encoding ทุกคอลัมน์ categorical Train/Test Split: 80/20 Model Training: LogisticRegres

  14. Python สำหรับผู้ใช้ Spreadsheet (Excel) - ไลบรารี openpyxl, xlwt, xlsxwriter

    หลายไลบรารีสำหรับอ่าน/เขียน Excel ใน Python เลือกตามรุ่นไฟล์และฟีเจอร์ที่ต้องการ 1. openpyxl (สำหรับ .xlsx, .xlsm, .xltx, .xltm - Excel 2010+) from openpyxl import Workbook import datetime wb = Workbook() ws = wb.active ws[A1] = 50 ws.append([5, 15, 25, 35, 45, 55]) ws.append([9, 19, 29, 39, 49, 5

  15. Statistical Thinking ใน Python (สถิติและการวิเคราะห์ข้อมูล)

    ใช้ Python สำหรับสถิติเชิงวิเคราะห์และการวาดกราฟด้วย pandas, matplotlib, seaborn, numpy โหลดข้อมูล (Seeds Dataset) import pandas as pd import warnings warnings.filterwarnings(ignore) # สมมติไฟล์ seeds.csv อยู่ใน path # df = pd.read_csv(seeds.csv) # print(df.head()) 1. Histogram (แผนภาพแท่ง) แสดงการ

  16. การใช้งาน Asterisk (* และ **) ใน Python

    Python ใช้ * (single asterisk) และ ** (double asterisk) ในบริบทหลายอย่าง 1. Infix Operator (คูณ / ซ้ำ) # ตัวเลข print(20 * 10) # 200 print(2.5 * 5.1) # 12.75 print((4+5j) * (5+4j)) # 41j # Sequence repetition print(Point- * 4) # Point-Point-Point-Point- print([4,5,6] * 3)

  17. การทำงานกับวันที่และเวลา (Dates and Times) ใน Python

    โมดูล datetime ใน standard library ครอบคลุมการจัดการวันที่/เวลาครบถ้วน 1. วันที่/เวลาปัจจุบัน from datetime import datetime now = datetime.now() print(now) # 2024-01-15 14:30:45.123456 print(type(now)) # 2. แปลง String ↔ Datetime # String → datetime dt_str = 2024-01

  18. หาความยาว 1 ติดกันมากที่สุดใน Binary String ด้วย Python

    กำหนดสตริงบाइनนารี หาจำนวน 1 ที่ติดต่อกันมากที่สุด วิธีที่ 1: split + map data = 11110000111110000011111010101010101011111111 max_len = max(map(len, data.split(0))) print(fMaximum consecutive 1s: {max_len}) # 8 วิธีที่ 2: Regular Expression import re data = 11110000111110010011 ones_blocks = re.fi

  19. ตรวจสอบจำนวนเฉพาะ (Prime Number) ใน Python

    จำนวนเฉพาะคือจำนวนเต็มบวกที่มากกว่า 1 และไม่มีตัวหารอื่นนอกจาก 1 และตัวมันเอง วิธีที่ 1: Naive Trial Division (เข้าใจง่าย) def is_prime_naive(n): if n <= 1: return False for i in range(2, n): if n % i == 0: return False return True print(is_prime_naive(23))

  20. Permutations แบบไม่ซ้ำ (Distinct) ของสตริงด้วย itertools ใน Python

    หา permutations ที่ไม่ซ้ำกันของสตริงที่มีตัวอักษรซ้ำ ใช้ itertools.permutations + set ตัวอย่าง Input: xyxxz, k=12 Output: 12 permutations แรกที่ไม่ซ้ำ โค้ด from itertools import permutations def distinct_permutations(s, k): # เรียงตัวอักษรก่อน เพื่อให้ permutations เรียงลำดับ sorted_chars =

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