Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. โปรแกรม Python หา Intersection ของสองลิสต์

    หาส่วนร่วม (intersection) ของสองลิสต์ คืออิลิเมนต์ที่ปรากฏในทั้งสองลิสต์ ตัวอย่าง List1: [1,2,3] List2: [2,3,6] Intersection: [2,3] อัลกอริทึม รับสองลิสต์ List comprehension: [x for x in A if x in B] โค้ดตัวอย่าง def intertwolist(A, B): return [i for i in A if i in B] A = []; B = [] n = i

  2. โปรแกรม Python เรียงลำดับลิสต์ตามความยาวอิลิเมนต์

    เรียงลำดับลิสต์ตามความยาวของแต่ละอิลิเมนต์ (string) ด้วย sorted(key=len) ตัวอย่าง Input: [mona,pp,aaa] Lengths: [4,2,3] Output: [pp,aaa,mona] โค้ดตัวอย่าง def sortedlist(A): return sorted(A, key=len) A = [] n = int(input(Enter the size of the List: )) for _ in range(n): A.append(input(En

  3. หาคำที่ซ้ำอันดับ 2 ในลำดับด้วย Python Counter

    หาคำที่มีความถี่เป็นอันดับ 2 มากที่สุดในลิสต์คำ โดยใช้ Counter เรียงค่าความถี่ อัลกอริทึม สร้าง Counter จากลิสต์คำ เรียงค่าความถี่มากไปน้อย หาค่าอันดับ 2 (index 1) หา key ที่มีค่านั้น โค้ดตัวอย่าง from collections import Counter def secondrepeatation(A): con = Counter(A) res = sorted(c

  4. พิมพ์กำลังของตัวเลขด้วย Anonymous Function (Lambda) ใน Python

    ใช้ lambda function ภายใน map() คำนวณกำลังของตัวเลข อัลกอริทึม รับ n (จำนวนพจน์) และ p (ฐาน) cal = list(map(lambda i: p**i, range(n))) พิมพ์ผลลัพธ์ โค้ดตัวอย่าง n = int(input(Enter how many terms: )) p = int(input(Enter base number: )) cal = list(map(lambda i: p**i, range(n))) print(Total terms

  5. หาค่าเฉลี่ยของลิสต์ใน Python ด้วย sum()

    คำนวณค่าเฉลี่ยของลิสต์ตัวเลขด้วย sum() แบ่งจำนวนอิลิเมนต์ อัลกอริทึม รับขนาดและอิลิเมนต์ของลิสต์ คำนวณ sm = sum(A) ค่าเฉลี่ย = sm / n โค้ดตัวอย่าง A = [] n = int(input(Enter the size of the List: )) for _ in range(n): A.append(int(input())) sm = sum(A) avg = sm / n print(SUM =, sm) print(AV

  6. โปรแกรม Python หาค่ามากที่สุด น้อยที่สุด อันดับ 2 มาก/น้อย ในลิสต์

    หา max, min, second max, second min ในลิสต์โดย遍历ครั้งเดียว ตัวอย่าง Input: [12,30,2,34,90,67] Output: Max=90, 2nd Max=67, Min=2, 2nd Min=12 อัลกอริทึม เริ่มต้น max, second_max, min, second_min = อิลิเมนต์แรก วนลูปอัปเดตทั้ง 4 ค่า โค้ดตัวอย่าง maxi: secondsmax = maxi ma

  7. โปรแกรม Python ลบค่าซ้ำจากลิสต์

    สร้างลิสต์ใหม่ที่ไม่มีค่าซ้ำจากลิสต์เดิมโดยเช็คก่อน append ตัวอย่าง Input: [2,3,4,3,4,6,78,90] Output: [2,3,4,6,78,90] อัลกอริทึม สร้างลิสต์ว่าง newlist วนลูปอิลิเมนต์ในลิสต์เดิม ถ้ายังไม่มีใน newlist ให้ append โค้ดตัวอย่าง def removeduplicateele(A): newlist = [] for n in A: i

  8. โปรแกรม Python รวมและเรียงลำดับสองลิสต์ที่ไม่ได้เรียง

    รวมสองลิสต์ที่ไม่ได้เรียงลำดับแล้ว เรียงผลลัพธ์โดย merge sorted arrays ตัวอย่าง A = [100,50,150], B = [200,30,20] Output: [20,30,50,100,150,200] อัลกอริทึม เรียง A และ B แยกกัน Merge แบบ two-pointer (เหมือน merge sort) คัดลอกส่วนที่เหลือ โค้ดตัวอย่าง def unsortedarray(a, b, res, n, m): a.s

  9. โปรแกรม Python Cyclic Redundancy Check (CRC)

    CRC ใช้ตรวจสอบข้อผิดพลาดข้อมูลดิจิทัล โดยใช้ binary division เพิ่ม check bits ให้ข้อมูลหารด้วย divisor ลงตัว ฝั่งส่ง: คำนวณ remainder แนบเป็น codeword ฝั่งรับ: หาร codeword ด้วย divisor เดียวกัน ถ้า remainder=0 ข้อมูลถูกต้อง โค้ดตัวอย่าง (ใช้ pycrc library) from pycrc.crclib import * # Sender di

  10. โปรแกรม Python พิมพ์ตัวอักษรร่วมของสองสตริงเรียง A-Z

    หาตัวอักษรร่วมของสองสตริงและเรียงตามลำดับอัลฟาเบต ใช้ Counter และ intersection (&) ตัวอย่าง string1: python string2: program Output: op อัลกอริทึม สร้าง Counter จากทั้งสองสตริง หา intersection: c1 & c2 (เก็บ min count) ขยายเป็น list ด้วย elements() เรียงลำดับและ join โค้ดตัวอย่าง from collecti

  11. โปรแกรม Python ย้าย Space ไปหน้าสตริงในครั้งเดียว

    ย้าย space ทั้งหมดไปด้านหน้าสตริงโดย遍历เพียงครั้งเดียว ใช้ List Comprehension ตัวอย่าง Input: python program Output: pythonprogram อัลกอริทึม สร้างสตริงไม่มี space: [c for c in s if c != ] นับจำนวน space: len(s) - len(no_space) สร้างผลลัพธ์: * space_count + .join(no_space) โค้ดตัวอย่าง def

  12. String Slicing ใน Python สำหรับหมุนสตริง (Rotate)

    หมุนสตริง 2 ทิศทางด้วย slicing: Left rotate (anticlockwise) d ตัว: s[d:] + s[:d] Right rotate (clockwise) d ตัว: s[-d:] + s[:-d] ตัวอย่าง Input: pythonprogram, d=2 Left: thonprogrampy Right: ampythonprogr โค้ดตัวอย่าง def rotate(input_str, d): Lfirst = input_str[:d] Lsecond = input_str

  13. โปรแกรม Python Modular Exponentiation (x^y mod z)

    คำนวณ (x^y) % z โดยใช้ฟังก์ชัน pow(x, y, mod) ที่มี built-in รองรับ modular exponentiation ได้ตรง ตัวอย่าง 2^3 % 3 = 8 % 3 = 2 โค้ดตัวอย่าง x = int(input(Enter First Value: )) y = int(input(Enter Second Value: )) z = 10**9 + 7 # common modulus result = pow(x, y, z) print(Value Is:, result) ผลล

  14. โปรแกรม Python หาค่าสูงสุดของ 3 ตัวเลข

    หาค่าสูงสุดจาก 3 ตัวเลขโดยใส่ใน list แล้วใช้ max() ตัวอย่าง Input: a=2, b=4, c=3 Output: 4 โค้ดตัวอย่าง def maximum(a, b, c): return max([a, b, c]) x = int(input(Enter First number: )) y = int(input(Enter Second number: )) z = int(input(Enter Third number: )) print(Maximum Number is:, maximu

  15. โปรแกรม Python คูณเมทริกซ์สองตัว

    คูณเมทริกซ์สองตัวขนาด n×น ด้วย triple nested loops อัลกอริทึม รับเมทริกซ์ A, B สร้าง result n×n เติม 0 for i, j, k: result[i][j] += A[i][k] * B[k][j] โค้ดตัวอย่าง # Input A, B (omitted) result = [[0]*n for _ in range(n)] for i in range(n): for j in range(n): for k in range(n):

  16. โปรแกรม Python คำนวณ BMI (Body Mass Index)

    คำนวณดัชนีมวลกาย (BMI) จากส่วนสูง (เมตร) และน้ำหนัก (กิโลกรัม) สูตร BMI = weight / (height × height) โค้ดตัวอย่าง height = float(input(Enter your height (m): )) weight = float(input(Enter your weight (kg): )) bmi = weight / (height * height) print(Your BMI is:, round(bmi, 2)) ผลลัพธ์ Enter your

  17. โปรแกรม Python บวกเมทริกซ์สองตัว

    บวกเมทริกซ์สองตัวขนาด n×n โดยใช้ nested list comprehension หรือ nested loops อัลกอริทึม รับเมทริกซ์ A และ B สร้าง result matrix ขนาด n×n เติม 0 วนลูป i, j คำนวณ result[i][j] = A[i][j] + B[i][j] โค้ดตัวอย่าง # Input matrices A, B (omitted for brevity) result = [[0]*n for _ in range(n)] for i in

  18. โปรแกรม Python เรียงลำดับลิสต์ตามอิลิเมนต์ที่ 2 ใน sublist (เวอร์ชัน 2)

    บทความซ้ำกับ ID 1004106513 เรียงลำดับลิสต์ของ sublist ตาม index 1 ด้วย bubble sort โค้ดตัวอย่าง A[j+1][1]: A[j], A[j+1] = A[j+1], A[j] return A A = [[AAA,10], [CCC,15], [RRRR,2], [XXXX,150]] print(sortlist(A)) ผลลัพธ์ [[RRRR, 2], [AAA, 10], [CCC, 15], [XXXX, 150]]

  19. แปลง HTML เป็น PDF ด้วย Python (pdfkit, wkhtmltopdf)

    ใช้ pdfkit` (wrapper ของ `wkhtmltopdf`) แปลง HTML เป็น PDF การติดตั้ง pip install pdfkit # Ubuntu/Debian: sudo apt-get install wkhtmltopdf # Windows: ดาวน์โหลด wkhtmltopdf และเพิ่ม PATH 3 วิธีการแปลง จากไฟล์ HTML: pdfkit.from_file(input.html, output.pdf) จาก URL: pdfkit.from_url(https://example.

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:73/451  20-คอมพิวเตอร์/Page Goto:1 67 68 69 70 71 72 73 74 75 76 77 78 79