Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. โปรแกรม Python หมุนอาร์เรย์ไปทางขวา (Right Rotate)

    หมุนลิสต์ไปทางขวา n ตำแหน่ง โดยย้ายองค์ประกอบท้ายสุดมาหน้าสุด ทำซ้ำ n ครั้งตัวอย่างโค้ดarr = [31, 42, 13, 34, 85, 0, 99, 1, 3] n = 3 print(Original:, arr) print(fRight rotate by {n}) for _ in range(n): last = arr[-1] for i in range(len(arr)-1, 0, -1): arr[i] = arr[i-1] arr[0] =

  2. โปรแกรม Python เรียงลำดับอาร์เรย์จากน้อยไปมาก (Ascending)

    เรียงลำดับลิสต์จากน้อยไปมากด้วยเมธอด sort() (default ascending)ตัวอย่างโค้ดarr = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print(Original:, arr) arr.sort() print(Sorted ascending:, arr)ผลลัพธ์Original: [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] Sorted ascending: [0, 9, 11, 23, 31, 42, 44, 56, 78, 89]คำอธิ

  3. โปรแกรม Python เรียงลำดับอาร์เรย์จากมากไปน้อย (Descending)

    เรียงลำดับลิสต์จากมากไปน้อยด้วยเมธอด sort(reverse=True)ตัวอย่างโค้ดarr = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print(Original:, arr) arr.sort(reverse=True) print(Sorted descending:, arr)ผลลัพธ์Original: [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] Sorted descending: [89, 78, 56, 44, 42, 31, 23, 11, 9, 0

  4. โปรแกรม Python คำนวณ n + nn + nnn จากตัวเลข n

    รับตัวเลข n แล้วคำนวณ n + nn + nnn โดยที่ nn คือตัวเลข 2 หลักที่ซ้ำ n, nnn คือ 3 หลักตัวอย่างโค้ดn = int(input(Enter n: )) s = str(n) nn = int(s + s) nnn = int(s + s + s) result = n + nn + nnn print(f{n} + {nn} + {nnn} = {result})ผลลัพธ์Enter n: 4 4 + 44 + 444 = 492คำอธิบายแปลง n เป็นสตริง ss + s ต

  5. โปรแกรม Python พิมพ์ตัวเลขในช่วงที่หารด้วยตัวเลขกำหนดลงตัว

    แสดงตัวเลขทั้งหมดในช่วง [lower, upper] ที่หารด้วยตัวเลข divisor ลงตัวตัวอย่างโค้ดlower = int(input(Lower bound: )) upper = int(input(Upper bound: )) divisor = int(input(Divisor: )) print(fNumbers in [{lower}, {upper}] divisible by {divisor}:) for i in range(lower, upper + 1): if i % divisor ==

  6. โปรแกรม Python หาผลหารและเศษจากการหาร 2 จำนวน

    คำนวณผลหาร (Quotient) และเศษ (Remainder) จากการหารจำนวนเต็ม 2 จำนวน ด้วย // และ %ตัวอย่างโค้ดa = int(input(First number: )) b = int(input(Second number: )) print(First:, a) print(Second:, b) quotient = a // b remainder = a % b print(Quotient:, quotient) print(Remainder:, remainder)ผลลัพธ์First nu

  7. โปรแกรม Python รับ 3 หลักพิมพ์ Permutation ทั้งหมด

    รับตัวเลข 3 หลักจากผู้ใช้ แล้วแสดง Permutation ทั้งหมด 6 แบบ โดยใช้ Nested Loopตัวอย่างโค้ดa = int(input(First digit: )) b = int(input(Second digit: )) c = int(input(Third digit: )) digits = [a, b, c] print(All permutations:) for i in range(3): for j in range(3): for k in range(3):

  8. โปรแกรม Python หาตัวหารที่น้อยที่สุดของจำนวนเต็ม

    หาตัวหารที่น้อยที่สุด (มากกว่า 1) ของจำนวนเต็มโดยวนลูปจาก 2 ถึง nตัวอย่างโค้ดn = int(input(Enter a number: )) print(Number:, n) for i in range(2, n + 1): if n % i == 0: print(Smallest divisor:, i) breakผลลัพธ์Enter a number: 56 Number: 56 Smallest divisor: 2คำอธิบายวนลูป i = 2,

  9. พิมพ์จำนวนเต็ม 1-50 ที่ไม่หารด้วย 2 และ 3 ลงตัวใน Python

    แสดงจำนวนเต็มระหว่าง 1 ถึง 50 ที่ไม่หารด้วย 2 ลงตัว และไม่หารด้วย 3 ลงตัวตัวอย่างโค้ดprint(Integers 1-50 not divisible by 2 or 3:) for n in range(1, 51): if n % 2 != 0 and n % 3 != 0: print(n, end= )ผลลัพธ์Integers 1-50 not divisible by 2 or 3: 1 5 7 11 13 17 19 23 25 29 31 35 37 41 43 4

  10. โปรแกรม Python พิมพ์รูปแบบผลรวมจำนวนธรรมชาติ (Summation Pattern)

    พิมพ์รูปแบบผลรวมจำนวนธรรมชาติ 1, 1+2, 1+2+3, ... จนถึง n พร้อมแสดงผลรวมตัวอย่างโค้ดn = int(input(Enter n: )) for i in range(1, n + 1): terms = list(range(1, i + 1)) print( + .join(map(str, terms)), =, sum(terms))ผลลัพธ์Enter n: 5 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 +

  11. โปรแกรม Python ตรวจสอบวันที่ถูกต้องและเพิ่มวันอีก 1 วัน

    ตรวจสอบความถูกต้องของวันที่ (dd/mm/yyyy) แล้วคำนวณวันถัดไป โดยพิจารณาจำนวนวันในแต่ละเดือนและปีอธิกสุรทินตัวอย่างโค้ดdate_str = input(Enter date (dd/mm/yyyy): ) dd, mm, yy = map(int, date_str.split(/)) # Days in month if mm in [1,3,5,7,8,10,12]: max_day = 31 elif mm in [4,6,9,11]: max_day =

  12. โปรแกรม Python คำนวณดอกเบี้ยธรรมดา (Simple Interest)

    คำนวณดอกเบี้ยธรรมดา จากเงินต้น อัตราดอกเบี้ย และระยะเวลา ด้วยสูตร SI = P × T × R / 100ตัวอย่างโค้ดprincipal = float(input(Principal amount: )) time = int(input(Time (years): )) rate = float(input(Rate (%): )) si = (principal * time * rate) / 100 print(Simple Interest:, si)ผลลัพธ์Principal amount: 4

  13. โปรแกรม Python แปลงความสูงจากเซนติเมตรเป็นฟุตและนิ้ว

    แปลงความสูงจากเซนติเมตรเป็นนิ้วและฟุต โดยใช้ค่าคงที่การแปลง และปัดทศนิยม 2 ตำแหน่งตัวอย่างโค้ดcm = int(input(Enter height in cm: )) inches = cm * 0.394 feet = cm * 0.0328 print(Height in inches:, round(inches, 2)) print(Height in feet:, round(feet, 2))ผลลัพธ์Enter height in cm: 178 Height in inches

  14. สร้าง Matrix ขนาด N×N ใน Python ด้วย List Comprehension

    สร้าง Matrix สี่เหลี่ยมจัตุรัส N×N ที่มีตัวเลขเรียงต่อเนื่อง 1 ถึง N² โดยใช้ List Comprehensionตัวอย่างโค้ดN = 4 print(N =, N) matrix = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print(N×N Matrix:) for row in matrix: print(row)ผลลัพธ์N = 4 N×N Matrix: [1, 2, 3, 4] [5, 6, 7, 8]

  15. ดึงคอลัมน์ที่ N ของ Matrix ใน Python

    ตรวจสอบว่ามีค่าเฉพาะอยู่ในคอลัมน์ที่ N ของ Matrix หรือไม่ ใช้ any() กับ Generator Expressionตัวอย่างโค้ดmatrix = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print(Matrix:, matrix) N = 1 # 0-based index (คอลัมน์ที่ 2) target = 30 print(Column index:, N) print(Target value:, target) exists = any(row[

  16. ต่อ Matrix แบบแนวตั้ง (Vertical Concatenation) ใน Python

    เมื่อต้องการต่อคอลัมน์ของ Matrix (ลิสต์ของลิสต์) แนวตั้ง ใช้ zip_longest จาก itertoolsตัวอย่างโค้ดfrom itertools import zip_longest matrix = [[Hi, Rob], [how, are], [you]] print(Matrix:) print(matrix) result = [.join(col) for col in zip_longest(*matrix, fillvalue=)] print(Vertically concatenated:)

  17. โปรแกรม Python เรียงลำดับด้วย Binary Search Tree (BST)

    ใช้ Binary Search Tree เรียงลำดับตัวเลข: แทรกทุกตัวเลขเข้า BST แล้วทำ Inorder Traversal จะได้ลำดับน้อยไปมากตัวอย่างโค้ดclass BSTNode: def __init__(self, key): self.key = key self.left = None self.right = None def insert(self, node): if node.key < self.key

  18. Depth First Search บน Binary Tree แบบ Recursive ใน Python

    DFS บน Binary Tree แบบ Pre-order (Root, Left, Right) พร้อมแสดงสถานะ entering/at/leaving แต่ละโหนดตัวอย่างโค้ดclass Node: def __init__(self, key): self.key = key self.left = None self.right = None def dfs(self): print(fEntering {self.key}...) if se

  19. ลบตัวอักษรที่ Index คี่ (Odd Index) ออกจากสตริงใน Python

    ลบตัวอักษรที่อยู่ในตำแหน่ง index คี่ (1, 3, 5...) จากสตริง โดยสร้างสตริงใหม่เก็บเฉพาะ index คู่ตัวอย่างโค้ดdef remove_odd_indices(s): result = for i, ch in enumerate(s): if i % 2 == 0: # even index (0-based) result += ch return result my_string = Hi there Will prin

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:266/451  20-คอมพิวเตอร์/Page Goto:1 260 261 262 263 264 265 266 267 268 269 270 271 272